1. 程式人生 > >nginx虛擬主機配置優化

nginx虛擬主機配置優化

linux

優化

[[email protected] conf]# mkdir extra
[[email protected] conf]# vim nginx.conf
[[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;
}
[[email protected] conf]# cp nginx.conf.20170820 extra/a
[[email protected] conf]# cd extra/
[[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
[[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;
        }
    }
[[email protected]
*/ extra]# sed -n "10,17p" a >www.conf
[[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
[[email protected] extra]# rm -f a

這樣就生成了3個虛擬主機

[[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;
        }
    }

虛擬主機已包含在配置文件裏面了

[[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;
}

檢查語法:

[[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啟動)

[[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.conf配置文件,就在配置文件中用*,但是沒有優先順序了。優點是每次新增站點不用修改配置文件了。

技術分享


本文出自 “sandshell” 博客,請務必保留此出處http://sandshell.blog.51cto.com/9055959/1957795

nginx虛擬主機配置優化