1. 程式人生 > 實用技巧 >Windows下Nginx配置

Windows下Nginx配置

一、安裝Nginx

官方下載地址:http://nginx.org/en/download.html
下載後是一個壓縮檔案,解壓縮後,將nginx-1.xx.x資料夾移動到不含有中文字元和空格的目錄中。

二、配置Nginx

因為nginx一般都是在伺服器上使用,所以這裡以C://nginx-1.18.0目錄為例。
找到此路徑,在路徑欄中輸入cmd然後enter,開啟命令。輸入start nignx開啟nginx。
瀏覽器訪問 localhost,看到Nginx 歡迎頁即成功。如果啟動失敗,可能因為80埠已經被佔用,接觸佔用後重新訪問即可。

接下來開始正式配置。
在C://nginx-1.18.0中找到conf目錄中的nginx.conf檔案,以記事本形式開啟。
找到檔案中的server節點,一般情況只配置server節點就能夠滿足需求。

`
server {
listen 8036; #監聽8036埠
server_name localhost;

    client_max_body_size 10m; #請求最大位元組限制

proxy_connect_timeout 300;#代理連線超時時間

proxy_read_timeout 300;#代理接收超時時間

    location / {
	root /publish/YanwenEpAnt/dist;#前端資料夾路徑
	index index.html,index.htm;#初始頁面
	proxy_pass http://localhost:8037;#未攜帶api代表前端訪問,8036重定向到8037,8037埠指前端釋出到伺服器的埠
	try_files  $uri $uri/ /index.html;
}
    location /api {
	proxy_pass http://localhost:8003;#攜帶api代表訪問後端,8036重定向到8003,8003埠指後端釋出到伺服器的埠
}  
      #error_page  404              /404.html;

    redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
}

`

按照這種方法配置可直接使用。現在還是回到cmd命令中,輸入nginx -s reload重新啟動nginx配置。
通過瀏覽器檢視localhost:8036地址是否可正常進入到8037埠釋出的前端頁面即可。

通常使用nginx時會出現上傳檔案,或請求超時的問題。這裡都需要對nginx進行額外配置。檔案大小以及請求超時配置上上面程式碼中已經配置。
接下來時一些常用配置程式碼

還要一些關於驗證的配置等等,這裡不一一列出。
接下來是一些常用的nginx命令:

nginx -t #測試配置檔案是否有語法錯誤 nginx -s reopen #重啟Nginx nginx -s reload #重新載入Nginx配置檔案,然後以優雅的方式重啟Nginx nginx -s stop #強制停止Nginx服務 nginx -s quit #優雅地停止Nginx服務(即處理完所有請求後再停止服務) nginx -h:檢視幫助 nginx -v:檢視nginx的版本 nginx -V:檢視版本和nginx的配置選項 nginx -t:測試配置檔案的正確性 Nginx -T: 測試配置檔案,並顯示配置檔案(這個命令可以快速檢視配置檔案) nginx -q:測試配置檔案,但是隻顯示錯誤資訊 nginx -s:傳送訊號,下面詳細介紹 nginx -p:設定字首 nginx -c:設定配置檔案 nginx -g:附加配置檔案路徑