http {
include mime.types;
default_type application/octet-stream;
# upstream 负载
upstream swagger-upstream {
server localhost:9003; # 可以负载权重等参数
server localhost:9004;
}
server {
# 监听端口
listen 8888;
server_name localhost;
location /gateway {
# 负载的地址(/gateway和上面location地址要相同)
# 此处的gateway也和springboot的 server.servlet.context-path 相同
proxy_pass http://swagger-upstream/gateway;
proxy_redirect off;
# $server_port 是关键
# 非80端口不加$server_port或者用$proxy_port 在请求接口的时候都不会请求到监听端口上
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 配置在响应的头部
add_header X-Route-Ip $upstream_addr;
add_header X-Route-Status $upstream_status;
}
}
}
server:
port: 9003
servlet:
context-path: /gateway