一、 nginx配置步驟
- 首先在官網http://nginx.org/en/download.html下載nginx,下載它的穩定版本
- 然後在nginx-1.16.1\conf下修改配置文件nginx.conf
配置代理如下
server {
listen 8089;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
proxy_intercept_errors on;
recursive_error_pages on;
root "D:\workCode\\resources"; //項目目錄
# ~* \.(html|js|css|png|jpg)$
location / {
index index_dev.htm; //首頁文件
if (!-e $request_filename) { //訪問的路徑文件不存在就進行代理轉發
proxy_pass http://10.118.65.168:8082;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
除了上面這種方式配置外,還可以進行對特定的後臺接口進行代理轉發,如下:
server {
listen 8089;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
proxy_intercept_errors on;
recursive_error_pages on;
root "E:\work\vr\VRfront\dist";
# ~* \.(html|js|css|png|jpg)$
location / {
index index.html;
}
location /api{ //對api後面的接口進行轉發
proxy_pass http://10.11.150.73:57682;
proxy_set_header Host $host;
#proxy_redirect off;
#proxy_set_header X-Forwarded-For #$proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
這樣就可以配置成了一個簡單的前端服務器,用於靜態資源的容器和接口的代理轉發。
二、啓動腳本
每次都去點擊niginx.exe的啓動和關閉也很麻煩,可以寫一個簡單的啓動腳本.bat來啓動nginx
@echo kill the ngix
taskkill /pid nginx.exe /f
@echo start the ngix
d:
cd/work/nginx-1.16.1
start nginx.exe
@echo start the ngix successful!
Pause
- 常見問題
- 在寫目錄的時候一定要注意是不含有n,t,r這些鍵盤字符,存在的即用//代替/
- 代理轉發配置的兩種方式,後臺接口請求沒有設置基礎統一的baseURL,就可以用第一種方式