nginx虛擬主機三種模式的簡單實現

main配置段:html

user nginx;  #指定用於運行worker進程的用戶和組node

worker_processes 4;    #worker的進程數;一般應該爲CPU的核心數或核心數減1nginx

worker_cpu_affinity 0001 0010 0100 1000;  進程綁定在CPU上的指定核上正則表達式

error_log /var/log/nginx/error.log; #錯誤日誌存放路徑服務器

pid /run/nginx.pid;   #nginx運行進程路徑併發

worker_rlimit_nofile 51200;   #單個worker進程所可以打開的最大文件數量app

daemon on;   #是否以守護進程方式啓動nginx進程負載均衡

events配置段:curl

events {tcp

    worker_connections 50000;   #每一個worker進程所可以打開的最大併發鏈接數量;可是不能大於worker_rlimit_nofile 數量

    use epoll;   #指明併發鏈接請求的處理方法

    accept_mutex on; #是否打開負載均衡鎖,處理新的鏈接請求的方法;on意味着由worker輪流處理新請求,off意味着每一個新請求的到達都會通知worker進程。默認須要開啓


}

http配置段:包含server配置段,其server配置段也能夠放置於/etc/nignx/conf.d/目錄下

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/conf.d/*.conf;

}


server配置段:能夠再主配置文件/etc/nginx/nginx.conf中的http中配置,也能夠在/erc/nginx/conf.d/分別建立本身的conf文件。

一、先在Nginx的默認路徑下建立三個目錄,也能夠在其餘指定路徑下存放html文件:

mkdir /usr/share/nginx/{yuming,port,IP}

mkdir -p /lufei/root/{yuming,port,IP}

二、分別在各自的路徑下面寫入以下index.html的文件

域名網頁:

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基於域名的虛擬主機-lufei-yuming</h2>

<h3>www.lufei-yuming.com</h3>

</body>  

</html>



Port網頁

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基於端口的虛擬主機-lufei-port</h2>

<h3>www.lufei-port.com</h3>

</body>  

</html>



IP網頁

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基於IP的虛擬主機-lufei-IP</h2>

<h3>www.lufei-IP.com</h3>

</body>  

</html>

說明:當服務器都配置完成後,須要在本地主機上作地址和域名解析

0一、以win10爲例

C:\WINDOWS\system32\drivers\etc路徑下的hosts文件添加以下內容:192.168.1.72    www.lufei.com

0二、若要在Linux中訪問:

在/etc/hosts中配置 192.168.1.72    www.lufei.com

三、基於域名的配置:在/etc/nginx/conf.d/下建立一個文件yuming.conf,配置完成後nginx -s reload 從新加載

server {        

listen 80;      #監聽端口 80                                 

server_name www.lufei.com *.lufei.com lufei.com *.lufei.*;   #域名的配置,其書寫格式可使用正則表達式。

location / {                    

#root    /lufei/root/yuming;      #根目錄的的絕對路徑配置  

root yuming;   # 相對路徑,相對nginx根目錄。nginx的默認路徑:/usr/share/nginx(yum安裝方式的)    

index index.html;     #默認主文件。默認跳轉到index.html頁面                 

}    

}

訪問測試:

[root@localhost yuming]# curl www.lufei.com

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基於域名的虛擬主機-lufei-yuming</h2>

<h3>www.lufei-yuming.com</h3>

</body>  

</html>

四、基於port的配置:在/etc/nginx/conf.d/下建立一個文件port.conf,配置完成後nginx -s reload 從新加載

[root@localhost yuming]# cat /etc/nginx/conf.d/port.conf 

server {        

listen 2225;      #監聽端口 2225                                 

server_name www.lufei.com *.lufei.com lufei.com *.lufei.*;   #域名的配置,其書寫格式可使用正則表達式。

location / {                

#root    /lufei/root/port;      #根目錄的的絕對路徑配置  

root port;   # 相對路徑,相對nginx根目錄。nginx的默認路徑:/usr/share/nginx(yum安裝方式的)    

index index.html;     #默認主文件。默認跳轉到index.html頁面                 

}    

}

訪問測試:

[root@localhost yuming]# curl www.lufei.com:2225

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基於端口的虛擬主機-lufei-port</h2>

<h3>www.lufei-port.com</h3>

</body>  

</html>

五、基於IP的配置:在/etc/nginx/conf.d/下建立一個文件IP.conf  ,配置完成後nginx -s reload 從新加載

server {        

listen 80;      #監聽端口 80                                 

server_name 192.168.1.72;   #域名的配置,其書寫格式可使用正則表達式。

location / {                

#root    /lufei/root/IP;      根目錄的的絕對路徑配置  

root IP;   # 相對路徑,相對nginx根目錄。nginx的默認路徑:/usr/share/nginx(yum安裝方式的)    

index index.html;     #默認主文件。默認跳轉到index.html頁面                 

}    

}

訪問測試:

[root@localhost yuming]# curl 192.168.1.72

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

</head>  

<body>  

<h2>基於IP的虛擬主機-lufei-IP</h2>

<h3>www.lufei-IP.com</h3>

</body>  

</html>

相關文章
相關標籤/搜索