nginx 多站點配置方法


關於nginx的多站設置,其實和apache很相似噠。

假設我們已經有兩個域名,分別是:www.websuitA.comwww.websuitB.com。並且這兩個域名已經映射給了IP192.168.1.1的服務器。

1、為我們的站點創建配置文件

我是這么做的,在nginx的配置文件conf目錄下創建一個專門存放VirtualHost的目錄,命名為vhosts_conf,可以把虛擬目錄的配置全部放在這里。在里面創建名為vhosts_modoupi_websuitA.conf的配置文件並打開,我們在這里做配置,往里面寫:

server {

listen 80;               #監聽的端口號

server_name websuitA.com;        #域名

#access_log logs/host.access.log main;

location / {

root X:/wnmp/www/websuitA;    #站點的路徑

index default.php index.php index.html index.htm;

#站點的rewrite在這里寫

rewrite ^/(\w+)\.html$ /$1.php;

rewrite ^/(\w+)/(\w+)$ /$1/$2.php;

}

#錯誤頁的配置

error_page 404 /error.html;

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ \.php$ {

root X:/wnmp/www/websuitA;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

location ~ /\.ht {

deny all;

}

}

 

這樣就做好了,站點A的配置,同樣的方法,做websuitB的配置,這里我命名為vhosts_modoupi_websuitB.conf

server {

     listen 80;               #監聽的端口號

     server_name websuitB.com;        #域名

     #access_log logs/host.access.log main;

     location / {

        root X:/wnmp/www/websuitB;    #站點的路徑

       index default.php index.php index.html index.htm;

#站點的rewrite在這里寫

       rewrite ^/(\w+)\.html$ /$1.php;

       rewrite ^/(\w+)/(\w+)$ /$1/$2.php;

     }

  #錯誤頁的配置

     error_page 404 /error.html;

     error_page 500 502 503 504 /50x.html;

     location = /50x.html {

       root html;

     }

     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

     location ~ \.php$ {

        root X:/wnmp/www/websuitB;

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include fastcgi_params;

     }

     location ~ /\.ht {

        deny all;

     }

}

 

這樣,兩個站點的配置就OK了。 
2、在nginx的主配置文件里,包含這兩個站點的配置文件。 
  我們打開conf目錄下的nginx.conf文件,很容易做,只要在http{...}段輸入以下代碼: 

#包含所有的虛擬主機的配置文件

include X:/wnmp/nginx/conf/vhosts_conf/*.conf;

 

這樣,nginx的多站點配置就做好了,怎么樣打開瀏覽器測試一下吧


免責聲明!

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



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