nginx虚拟主机配置优化

优化

1
2
[[email protected] conf]# mkdir extra
[[email protected] conf]# vim nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[[email protected] 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
[[email protected] conf]# cp nginx.conf.20170820 extra/a
[[email protected] conf]# cd extra/
1
2
3
4
5
6
7
8
9
10
[[email protected] extra]# sed -n "18,25p" a    
    server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
[[email protected] extra]# sed -n "18,25p" a>bbs.conf
1
2
3
4
5
6
7
8
9
[[email protected] extra]# sed -n "10,17p" a 
    server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
1
[[email protected] extra]# sed -n "10,17p" a >www.conf
1
2
3
4
5
6
7
8
9
10
[[email protected] extra]# sed -n "26,33p" a   
    server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
[[email protected] extra]# sed -n "26,33p" a >blog.conf
1
[[email protected] 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
[[email protected] extra]# cat www.conf 
    server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
[[email protected] extra]# cat bbs.conf 
    server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
[[email protected] 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
[[email protected] 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
[[email protected] 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
[[email protected] 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

nginx虚拟主机配置优化

nginx虚拟主机配置优化

nginx虚拟主机配置优化


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

nginx虚拟主机配置优化

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

sandshell