1. 程式人生 > >nginx.conf詳解和Nginx的虛擬主機

nginx.conf詳解和Nginx的虛擬主機

主機配置 ima 配置 請求 允許 acc 成功 進行 就是

cat /etc/nginx/nginx.conf worker_processes 1; #定義有多少個工作的子進程,可自行修改,太大無益,因為要爭奪CPU,一般設置為核心總數(lscpu中CPU(S)可看) events { worker_connections 1024; #一個worker允許同時最大產生多少個鏈接 } http { #Web功能的標簽 include conf/mime.types; #設定mime類型,類型由conf/mime.types決定 default_type application/octet-stream; sendfile on; #sendfile指令指定 nginx 是否調用sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,必須設 為on。如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡IO處理速度,降低系統uptime。 keepalive_timeout 65; #http長連接的斷開時間,(長連接:一次建立TCP鏈接多次請求)從最後一次請求開始計算時間 server { #虛擬主機的標簽 listen 80; #偵聽的端口 server_name localhost; #服務器的名稱,別人可以通過這個來訪問你的網站 location / { root html; #網站的根目錄,如果是絕對路徑,就是對於nginx服務器的家目錄來說的 index index.html index.htm; #網站默認的查找首頁,從左向右依次查找 } error_page 500 502 503 504 /50x.html; #如果碰到上述狀態碼,則轉交給後面的50x.html頁面 location = /50x.html { root html; } } }

基於IP地址的虛擬主機

在http標簽裏面添加上一對server標簽
    server {
        listen 80;
        server_name 10.0.0.7;
        access_log logs/ip_access.log main;
        location / {
            root html/server_ip;
            index index.html
        }
    }
保存退出,檢查語法
[root@WNginx01_7 nginx]# nginx  -c /usr/local/nginx/conf/nginx.conf -t 
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
平滑重啟(重讀配置文件)
[root@WNginx01_7 nginx]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
創建虛擬主機的網站根目錄,剛才在配置文件裏面定義的
[root@WNginx01_7 nginx]# mkdir /usr/local/nginx/html/server_ip
進入到這個裏面編輯網站的主頁
[root@WNginx01_7 nginx]# cat /usr/local/nginx/html/server_ip/index.html
<html>
<center><h1>Welcome to 10.0.0.7 server test</h1></center>
</html>
#然後在瀏覽器上輸入IP地址,發現內容正確顯示,基於IP的虛擬主機配置完成

技術分享圖片

基於端口的虛擬主機(生產環境中可以通過這種方法給網站單獨開個後臺,增加安全性)

在http標簽裏面添加上一對server標簽
    server {
        listen 8888;
        server_namelocalhost;
        access_log logs/8888_port_access.log main;
        location / {
            root html/port;
            index index.html
        }
    }
保存退出,檢查語法
[root@WNginx01_7 nginx]# nginx  -c /usr/local/nginx/conf/nginx.conf -t 
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
平滑重啟(重讀配置文件)
[root@WNginx01_7 nginx]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
創建虛擬主機的網站根目錄,剛才在配置文件裏面定義的
[root@WNginx01_7 nginx]# mkdir /usr/local/nginx/html/port
進入到這個裏面編輯網站的主頁
[root@WNginx01_7 nginx]# cat /usr/local/nginx/html/port/index.html
<html>
<center><h1>Welcome to 8888 server test</h1></center>
</html>
然後在瀏覽器上輸入IP地址加端口號,發現內容正確顯示,基於port的虛擬主機配置完成
![](http://i2.51cto.com/images/blog/201809/29/40383a0d5dd2e15c944f4c73b40206f8.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
查看服務器偵聽的端口
![](http://i2.51cto.com/images/blog/201809/29/93977800f00dc1a94d45e3598e9e2c70.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

基於域名的虛擬主機

在http標簽裏面添加上一對server標簽
    server {
        listen 80;
        server_name www.sentinel.com;
        access_log logs/sentinel_access.log main;
        location / {
            root html/sentinel;
            index index.html
        }
    }
保存退出,檢查語法
[root@WNginx01_7 nginx]# nginx  -c /usr/local/nginx/conf/nginx.conf -t 
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2018/08/24 11:15:36 [info] 9888#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
平滑重啟(重讀配置文件)
[root@WNginx01_7 nginx]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
創建虛擬主機的網站根目錄,剛才在配置文件裏面定義的
[root@WNginx01_7 nginx]# mkdir /usr/local/nginx/html/sentinel
進入到這個裏面編輯網站的主頁
[root@WNginx01_7 nginx]# cat /usr/local/nginx/html/sentinel/index.html
<html>
<center><h1>Welcome to www.sentinel.com server test</h1></center>
</html>
然後在本機的hosts文件裏面寫上www.sentinel.com和他的ip地址的對應關系,因為我們沒有DNS服務器,雖然他是基於域名的,但是實際上還是通過ip地址來進行通信的。
[root@sentinel ~]# echo "10.0.0.7 www.sentinel.com" >>/etc/hosts
訪問成功
![](http://i2.51cto.com/images/blog/201809/29/ca86a0dd52fa527a25c7cc26ba7c2020.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

nginx.conf詳解和Nginx的虛擬主機