1. 程式人生 > >windows下搭建nginx服務並實現基於埠與路徑轉發

windows下搭建nginx服務並實現基於埠與路徑轉發

windows下搭建nginx

nginx下載

nginx使用

  • nginx不能通過雙擊nginx.exe啟動,需要在cmd命令列中通過命令啟動。
  • 啟動cmd命令列,進入nginx所在路徑

啟動命令

  • start nginx

停止命令

  • nginx -s stop 直接退出
  • nigix -s quit 儲存並退出

重新載入配置檔案

  • nginx -s reload

nginx配置

  • nginx.conf 基於埠與基於路徑轉發的配置已在註釋中說明
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    # 這個server配置表示訪問localhost:8081地址 將請求轉發到這個server下定義的location位置上
	server {
        listen       8081;
        server_name  localhost;
        # 這個location表示將localhost:8081地址轉發到html資料夾下的index2.html首頁上
        location / {
            root   html;
            index  index2.html index.htm;
        }
    }
    # 這個server表示訪問localhost:80地址 將請求轉發到這個server下定義的location位置上
    server {
        listen       80;
        server_name  localhost;
        # 這個location表示將localhost:80請求 轉發到index.html檔案上
		location / {
			root   html;
            index  index.html index.htm;
		}
		#這個location表示將localhost:80/demo/開頭的請求 轉發到http://localhost:8080/服務上
        location /demo {
			proxy_pass http://localhost:8080/;
		}
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
# 在做請求轉發時,主要注意proxy_pass後/符號的新增,不加/表示訪問 http://localhost:8080/demo+  加/表示訪問http://localhost:8080/+