一、 nginx配置步骤
配置代理如下
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