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