標籤:
Windows下Nginx Virtual Host多網站配置詳解
此教程適用於Windows系統已經配置好Nginx+Php+Mysql環境的同學。
如果您還未搭建WNMP環境,請查看 windows7配置Nginx+php+mysql教程。
先說明一下配置多網站的目的:在生產環境中,如果將系統所有代碼檔案都放在公開目錄中,則很容易被查看到系統源碼,這樣是很不安全的,所以需要只公開index.php的入口檔案目錄。而同一個伺服器中,可能運行多個系統,這樣就必須公開多個入口檔案目錄,以便用不同的網域名稱訪問不同的系統。所以這就需要使用virtual host實現多網站。
下面直接進入主題:
一.配置virtualhost多網站
以www.lee.com和www.lee1.com為兩個栗子。
1. 定義網站網域名稱。
首先修改系統hosts檔案(hosts檔案位於C:\Windows\System32\drivers\etc檔案夾內)。在修改hosts檔案之前要先確定有修改此檔案的許可權,滑鼠右鍵hosts檔案,點擊屬性,如所示點擊編輯修改使用者的許可權為可以寫入。
然後在hosts檔案底部,仿照如下添加:(根據需求可隨意添加)
127.0.0.1 www.lee.com
127.0.0.1 www.lee1.com
2. 建立網站公開檔案目錄,並建立測試檔案
我設定的檔案目錄:
nginx檔案夾為nginx相關內容,php為php相關內容。
其中lee和lee1位公開的兩個檔案目錄,檔案目錄path和檔案夾名可以根據網站網域名稱做任意更改。
在lee和lee1檔案夾中添加兩個php檔案用於測試。
在lee檔案夾中添加index.php,並編輯內容為:
<?php echo "www.lee.com<br/>"; echo phpinfo(); ?>
在lee1檔案夾中添加index.php,並編輯內容為:
<?php echo "www.lee1.com<br/>"; echo phpinfo(); ?>
3. 修改nginx.conf設定檔
在該設定檔中如下代碼位置進行修改:(nginx.conf配置位於nginx/conf/檔案夾內)
# another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #}
將上述配置代碼修改為:
# another virtual host using mix of IP-, name-, and port-based configuration # #modify by lee 20160902 for virtual host www.lee.com -s server { listen 80; access_log logs/lee.access.log; error_log logs/lee.error.log; server_name www.lee.com; location / { root C:/wnmp/lee; index index.html index.htm index.php; } location ~ \.php$ { root C:/wnmp/lee; fastcgi_pass 127.0.0.1:9001; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } #modify by lee 20160902 for virtual host www.lee.com -e #modify by lee 20160902 for virtual host www.lee1.com -s server { listen 80; access_log logs/lee1.access.log; error_log logs/lee1.error.log; server_name www.lee1.com; location / { root C:/wnmp/lee1; index index.html index.htm index.php; } location ~ \.php$ { root C:/wnmp/lee1; fastcgi_pass 127.0.0.1:9001; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } #modify by lee 20160902 for virtual host www.lee1.com -e
其中server_name為hosts檔案中設定的網站網域名稱,access_log和error_log為記錄檔,檔案名稱做響應更改。
root為 步驟2設定的網站公開檔案目錄。
4. 測試
重啟Nginx和php-cgi服務,啟動方法詳見我的上一篇文章------windows7配置Nginx+php+mysql教程 (步驟4(5))
開啟瀏覽器,訪問 www.lee.com
訪問 www.lee1.com
VirtualHost多網站配置成功!
下一篇文章會是: Windows下Nginx配置Openssl實現Https訪問(包含認證產生)
參考:http://www.jb51.net/article/27533.htm
Windows下Nginx Virtual Host多網站配置詳解