nginx配置之虛擬主機功能


虛擬主機功能: 一個nginx下運行多個網址(站點域名)

  方式一:nginx.conf中的http{}中的每一個server{}就是一個站點(相同端口):        

          #虛擬主機1 

          server { 

            listen 80; 

            server_name www.nginx1.com; 

            location / { 

              #deny 192.168.160.0/24; 

              root /opt/nginxtest/html; 

              index index.html index.htm;

             } 

             location /pic {

               alias /opt/nginxtest/pic; 

            }  

            location /status{ 

              stub_status on; 

            }  

            error_page 404 /404.html; 

          } 

          #虛擬主機2 

          server { 

            listen 80; 

            server_name www.nginx2.com; 

            location / { 

              root /opt/nginxtest/html2; 

              index index.html;

             }

           }

      (同一默認端口:需要在客戶端設hi在hosts文件的dnsj解析)----C:\Windows\System32\drivers\etc\hosts

         -----------192.168.160.132 www.nginx1.com

         -----------192.168.160.132 www.nginx2.com

        訪問時直接輸入域名,默認80

  方式二:nginx.conf中的http{}中的每一個server{}就是一個站點(不同端口指定訪問)        

          #虛擬主機1

          server {

            listen 80;

            server_name www.nginx1.com;

            location / {

              #deny 192.168.160.0/24;

              root /opt/nginxtest/html;

              index index.html index.htm;

            } 

            location /pic {

              alias /opt/nginxtest/pic;

            } 

            location /status{

              stub_status on;

            } 

            error_page 404 /404.html;

          } 

          #虛擬主機2

          server {

            listen 81;

            server_name www.nginx1.com;

            location / {

              root /opt/nginxtest/html2;

              index index.html;

            }

          }

     (不同端口:需要在客戶端設hi在hosts文件的dnsj解析)

         ----------192.168.160.132 www.nginx1.com:80

          ----------192.168.160.132 www.nginx1.com:81

      訪問時輸入域名並指定端口


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM