Nginx 监听同一端口号配置多个域名
<p>同一台nginx服务器通过配置多个server块实现在同一端口号下监听多个域名。</p><p>需要注意的是:端口号(listen)+主机名(server_name) 需要在多个server中唯一,否则会报错。</p>
<p>实现效果:分别访问one.lyj.com 和 two.lyj.com获取不同的资源 </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#usernobody;
# 工作进程数量
worker_processes</span>1<span style="color: rgba(0, 0, 0, 1)">;
#error_loglogs</span>/<span style="color: rgba(0, 0, 0, 1)">error.log;
#error_loglogs</span>/<span style="color: rgba(0, 0, 0, 1)">error.lognotice;
#error_loglogs</span>/<span style="color: rgba(0, 0, 0, 1)">error.loginfo;
#pid logs</span>/<span style="color: rgba(0, 0, 0, 1)">nginx.pid;
# 每个worker创建连接数
events {
worker_connections</span>1024<span style="color: rgba(0, 0, 0, 1)">;
}
http {
# 引入文件 mime.types里配置的是文件会以何种方式返回给客户端
include mime.types;
# 默认的返回方式
default_typeapplication</span>/octet-<span style="color: rgba(0, 0, 0, 1)">stream;
# 数据0拷贝
sendfile on;
#tcp_nopush on;
# 保持长链接时间
keepalive_timeout</span>65<span style="color: rgba(0, 0, 0, 1)">;
# 虚拟主机 vhost一台nginx可以配置多个server
server {
# 监听端口
listen </span>80<span style="color: rgba(0, 0, 0, 1)">;
# 配置域名、主机名域名需要备案和配置dns解析
server_nameone.lyj.com;
# location用来匹配uri(资源)eg: url为: http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">liyijun.com/learnnginx/index.html ; uri就是:/learnnginx/index.html</span>
<span style="color: rgba(0, 0, 0, 1)"> # 一个server可以配置多个location
location </span>/<span style="color: rgba(0, 0, 0, 1)"> {
# 资源的相对路径 eg:html就是在nginx主目录下的html文件夹下
root </span>/www/<span style="color: rgba(0, 0, 0, 1)">one;
# 默认展示页面
indexindex.html index.htm;
}
# 遇到错误页面码转到 </span>/50x.html下 下面的location会将/<span style="color: rgba(0, 0, 0, 1)">50x.html转到html文件夹下去寻找50x.html
error_page </span>500 502 503 504/<span style="color: rgba(0, 0, 0, 1)">50x.html;
location </span>= /<span style="color: rgba(0, 0, 0, 1)">50x.html {
root html;
}
}
# 监听多个端口
server {
# 监听端口
listen </span>80<span style="color: rgba(0, 0, 0, 1)">;
# 配置域名、主机名 多个域名之间用空格间隔开
server_nametwo.lyj.com three.lyj.com;
# location用来匹配uri(资源)eg: url为: http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">liyijun.com/learnnginx/index.html ; uri就是:/learnnginx/index.html</span>
<span style="color: rgba(0, 0, 0, 1)"> # 一个server可以配置多个location
location </span>/<span style="color: rgba(0, 0, 0, 1)"> {
# 资源的相对路径 eg:html就是在nginx主目录下的html文件夹下
root </span>/www/<span style="color: rgba(0, 0, 0, 1)">two;
# 默认展示页面
indexindex.html index.htm;
}
# 遇到错误页面码转到 </span>/50x.html下 下面的location会将/<span style="color: rgba(0, 0, 0, 1)">50x.html转到html文件夹下去寻找50x.html
error_page </span>500 502 503 504/<span style="color: rgba(0, 0, 0, 1)">50x.html;
location </span>= /<span style="color: rgba(0, 0, 0, 1)">50x.html {
root html;
}
}
}</span></pre>
</div>
<p> 参考:16-基本使用-Nginx虚拟主机域名配置_哔哩哔哩_bilibili<br></p><br><br>
来源:https://www.cnblogs.com/liyijun-blog/p/16749628.html
頁:
[1]