Nginx 虛擬主機配置

1、什麼是虛擬主機

簡單的理解就是如果我們沒有虛擬主機,我們每有一個新的站點之後,我們就需要再起一個web服務,但是有了虛擬主機後,我們可以通過比如域名來區分訪問的頁面,這樣可以很簡單的新建站點,在apache和nginx兩個服務中都有相應的配置方法,但是apache可能會稍微麻煩點,nginx就比較簡單了,而且nginx的高併發的能力加持,現在個人感覺nginx比apache火。


2、配置nginx虛擬主機

Nginx虛擬主機個人總結有一個要點就是一個server標籤一個站點,或者一個服務(反向代理後端服務)


修改配置之前我們需要先備份主配置文件:

cp nginx.conf nginx.conf.bak


個人感覺這是一種好習慣,一旦配置錯誤還可以回滾。


由於nginx配置文件本身有很多配置被註釋的,而且我們這裏可能也用不到,所以這裏去掉註釋內容和空行

egrep -v "#|^$" nginx.conf.bak > nginx.conf

egrep :是grep的擴展,如果想使用grep調用擴展正則表達式可以使用-e參數,-v是去除、排除的意思


默認配置:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections  1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/nginx.conf.d/*.conf;
}



添加server標籤來配置虛擬主機:

# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections  1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/nginx.conf.d/*.conf;
#blog======================================
    server {
	listen 80;
	server_name blog.example.com;
	location / {
		root /opt/html/blog;
		index index.html index.htm;
		}
	}
#bbs=======================================
    server {
        listen 80;
        server_name bbs.example.com;
        location / {
                root /opt/html/bbs;
                index index.html index.htm;
                }
        }

}

上面的配置說明:

listen:監聽的端口

server_name:監聽的域名

root:表示站點的目錄

index:表示解析的首頁文件,比如網頁除了html 還有php、asp等。


創建站點文件:

# cd /opt/
# mkdir html/{blog,bbs} -p
# ll html/
總用量 8
drwxr-xr-x. 2 root root 4096 12月 23 08:46 bbs
drwxr-xr-x. 2 root root 4096 12月 23 08:46 blog
# echo "bbs" > html/bbs/index.html
# echo "blog" > html/blog/index.html
# /etc/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# /etc/init.d/nginx restart
停止 nginx:                                               [確定]
正在啓動 nginx:                                           [確定]


如果是編譯安裝檢查配置文件是否正確的方法是-t

參考:

/usr/local/nginx/sbin/nginx -t


注意:大括號必須是成對的。


測試:

提示:由於這裏測試涉及到域名解析問題,最簡單的方法就是利用本地hosts,解析優先級:緩存> hosts>dns



3、優化

一般生產中,是絕對不會把虛擬主機都放在一個配置文件中的,因爲這樣極易引起混亂,管理很困難。所以管理配置文件一般都會分類管理:分項目、前後端、網站類型、服務等方法。

# pwd
/etc/nginx

# mkdir nginx.conf.d/{blog,bbs} -p

#sed -n "23,30p" nginx.conf > nginx.conf.d/blog/blog.conf

# sed -n "23,30p" nginx.conf > nginx.conf.d/blog/blog.conf

# sed -i "22,30d" nginx.conf

# sed -n "23,31p" nginx.conf > nginx.conf.d/bbs/bbs.conf

# sed -i "23,31d" nginx.conf

修改主配置文件爲如下結果:

# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections  1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include  /etc/nginx/nginx.conf.d/blog/*.conf;
    include  /etc/nginx/nginx.conf.d/bbs/*.conf;
}


最後的配置文件所在路徑用include包含進來即可。

重啓服務:

# /etc/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# /etc/init.d/nginx restart
停止 nginx:                                               [確定]
正在啓動 nginx:                                           [確定]



最後測試成功即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章