user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
underscores_in_headers on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Destination $fixed_destination;
#后端服务集群配置
upstream mysite {
server 192.168.1.22:9999 weight=1;
server 192.168.1.22:9998 weight=1;
}
server {
#端口
listen 80;
#IP
server_name 192.168.1.22;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT,OPTIONS';
#反向代理前端项目project
location / {
root /usr/local/project/dist;
index index.html;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
#解决Sorry, Page Not Found
try_files $uri $uri/ @router;
}
#解决Sorry, Page Not Found
location @router {
rewrite ^.*$ /index.html last;
}
#反向代理前端项目printProject 前端路径需加前缀 SelfPrint 不然访问不到静态文件
location /SelfPrint {
alias /usr/local/project/printProject/dist/;
index index.html;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
#反向代理后端服务集群
location /api {
proxy_pass http://mysite/;
}
#反向代理后端服务
location /print {
proxy_pass http://192.168.1.22:8888/;
}
#发生错误时指定错误页
error_page 404 /index.html;
#发生错误时指定错误页
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
nginx配置反向代理,负载均衡,解决Sorry, Page Not Found
于 2020-08-12 10:10:16 首次发布