一、準備工作
1、在官網(https://nginx.org/en/download.html)下載最新版的Nginx for Windows壓縮包。
2、選擇一個合適的目錄,如「C:\nginx」,將下載好的壓縮包解壓到該目錄下。
3、打開命令行工具,進入「C:\nginx」目錄。
二、安裝過程
1、運行命令「start nginx」,啟動Nginx服務器。
C:\nginx>start nginx
2、打開瀏覽器,在地址欄中輸入「http://localhost」,如果頁面顯示「Welcome to nginx!」則說明Nginx已經安裝成功。
3、如果頁面無法正常顯示,可以在命令行中執行「nginx -t」檢查Nginx配置文件是否正確。
C:\nginx>nginx -t
三、基本配置
1、修改Nginx默認監聽的端口
默認情況下,Nginx監聽的是80端口。在「C:\nginx\conf\nginx.conf」配置文件中,找到以下代碼:
server { listen 80; server_name localhost; ... }
將「listen」後的端口號改為自定義的端口號,如8080。
server { listen 8080; server_name localhost; ... }
2、添加虛擬主機
在「C:\nginx\conf\nginx.conf」配置文件中,找到以下代碼:
http { ... server { listen 80; server_name localhost; ... location / { root html; index index.html index.htm; } ... } }
將以下代碼複製到該代碼塊的外面,作為一個新的server塊,修改其中的server_name和root配置項,即可添加一個虛擬主機。
server { listen 8080; server_name example.com; root C:\web\example; ... }
四、其他配置
1、開啟gzip壓縮
在「C:\nginx\conf\nginx.conf」配置文件中,找到以下代碼:
http { ... gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ... }
將「gzip」和「gzip_types」配置項的值改為「on」和需要進行壓縮的文件類型,即可開啟gzip壓縮。
http { ... gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ... }
2、修改默認主頁
在「C:\nginx\conf\nginx.conf」配置文件中,找到以下代碼:
http { ... server { listen 8080; server_name localhost; ... location / { root html; index index.html index.htm; } ... } }
將「index」配置項中的默認主頁名稱,如從「index.html index.htm」修改為「index.php index.html index.htm」,即可修改默認主頁。
五、總結
以上就是Nginx在Windows平台下的安裝和基本配置教程。通過閱讀本教程,您可以輕鬆地在Windows系統上安裝和配置Nginx服務器,進行網站開發和測試。
原創文章,作者:CFNZ,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/137260.html