nginx虚拟主机配置优化

简介:

优化

1
2
[root@web01 conf] # mkdir extra
[root@web01 conf] # vim nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@web01 conf] # cat nginx.conf
worker_processes  1;
events {
     worker_connections  1024;
}
http {
     include       mime.types;
     default_type  application /octet-stream ;
     sendfile        on;
     keepalive_timeout  65;
     include extra /www .conf;
     include extra /bbs .conf;
     include extra /blog .conf;
}
1
2
[root@web01 conf] # cp nginx.conf.20170820 extra/a
[root@web01 conf] # cd extra/
1
2
3
4
5
6
7
8
9
10
[root@web01 extra] # sed -n "18,25p" a    
     server {
         listen       80;
         server_name  bbs.etiantian.org;
         location / {
             root   html /bbs ;
             index  index.html index.htm;
         }
     }
[root@web01 extra] # sed -n "18,25p" a>bbs.conf
1
2
3
4
5
6
7
8
9
[root@web01 extra] # sed -n "10,17p" a 
     server {
         listen       80;
         server_name  www.etiantian.org;
         location / {
             root   html /www ;
             index  index.html index.htm;
         }
     }
1
[root@web01 extra] # sed -n "10,17p" a >www.conf
1
2
3
4
5
6
7
8
9
10
[root@web01 extra] # sed -n "26,33p" a   
     server {
         listen       80;
         server_name  blog.etiantian.org;
         location / {
             root   html /blog ;
             index  index.html index.htm;
         }
     }
[root@web01 extra] # sed -n "26,33p" a >blog.conf
1
[root@web01 extra] # rm -f a

   

     这样就生成了3个虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@web01 extra] # cat www.conf 
     server {
         listen       80;
         server_name  www.etiantian.org;
         location / {
             root   html /www ;
             index  index.html index.htm;
         }
     }
[root@web01 extra] # cat bbs.conf 
     server {
         listen       80;
         server_name  bbs.etiantian.org;
         location / {
             root   html /bbs ;
             index  index.html index.htm;
         }
     }
[root@web01 extra] # cat blog.conf 
     server {
         listen       80;
         server_name  blog.etiantian.org;
         location / {
             root   html /blog ;
             index  index.html index.htm;
         }
     }

虚拟主机已包含在配置文件里面了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@web01 extra] # cat ../nginx.conf
worker_processes  1;
events {
     worker_connections  1024;
}
http {
     include       mime.types;
     default_type  application /octet-stream ;
     sendfile        on;
     keepalive_timeout  65;
     include extra /www .conf;
     include extra /bbs .conf;
     include extra /blog .conf;
}

    检查语法:

1
2
3
[root@web01 extra] # ../../sbin/nginx -t
nginx: the configuration  file  /application/nginx-1 .6.3 //conf/nginx .conf syntax is ok
nginx: configuration  file  /application/nginx-1 .6.3 //conf/nginx .conf  test  is successful

    优雅平滑重启(如果平滑重启不生效,那么就-s stop 再nginx启动)

1
[root@web01 extra] # ../../sbin/nginx -s reload

    配置本地dns解析:C:\Windows\System32\drivers\etc\hosts    增加如下dns解析记录

10.0.0.8 www.etiantian.org bbs.etiantian.org blog.etiantian.org

    在windows的ie中验证是否可以打开虚拟主机的站点

www.etiantian.org    bbs.etiantian.org    blog.etiantian.org

wKiom1mZGq6CA1ANAABuj4uZock599.jpg

wKioL1mZGqnRgEGAAABxLTxuzeI873.jpg

wKiom1mZGrCBCWwhAABxjZYH_Ro547.jpg


如果不想每次都修改nginx.conf配置文件,就在配置文件中用*,但是没有优先顺序了。优点是每次新增站点不用修改配置文件了。

wKiom1mZHJ_y8IFoAABCIKJB8GI037.jpg

本文转自sandshell博客51CTO博客,原文链接http://blog.51cto.com/sandshell/1957795如需转载请自行联系原作者

sandshell
相关文章
|
24天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
27 0
|
1月前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
43 0
|
23天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
92 0
|
3天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
12 0
|
5天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
6天前
|
前端开发 JavaScript 应用服务中间件
修改Jeecg-boot context-path(附加图片+Nginx配置)
修改Jeecg-boot context-path(附加图片+Nginx配置)
12 0
|
16天前
|
域名解析 Ubuntu 应用服务中间件
Nginx实现虚拟主机
Nginx实现虚拟主机
|
16天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
24 0
|
17天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
29 0
|
26天前
|
应用服务中间件 nginx
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
9 0