配置Nginx反向代理转发至后端服务器遇问题求助
Nginx反向代理后端请求故障排查方案
核心问题:路径拼接错误
你的配置中location /petclinic/api/对应的proxy_pass结尾缺少斜杠,导致Nginx转发请求时路径拼接异常。比如客户端请求/petclinic/api/owners,会被错误转发到http://10.0.0.6:8080/petclinic/apipetclinic/api/owners,而非预期的http://10.0.0.6:8080/petclinic/api/owners。
修正后的配置
server { listen 80 default_server; root /usr/share/nginx/html; index index.html; location /petclinic/ { alias /usr/share/nginx/html/petclinic/dist/; try_files $uri$args $uri$args/ /petclinic/index.html; } location /petclinic/api/ { # 末尾添加斜杠,确保路径正确拼接 proxy_pass http://10.0.0.6:8080/petclinic/api/; proxy_set_header Host $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; # 可选优化配置 proxy_connect_timeout 30s; proxy_send_timeout 30s; proxy_read_timeout 30s; proxy_http_version 1.1; proxy_set_header Connection ""; } }
额外排查方向
- 确认Spring Boot应用的
server.servlet.context-path是否为/petclinic,若不一致需调整proxy_pass中的路径。 - 在Nginx服务器上执行
curl http://10.0.0.6:8080/petclinic/api/[具体接口],验证后端服务本身是否正常响应。 - 查看Nginx错误日志(通常路径为
/var/log/nginx/error.log),排查是否存在连接超时、404等具体错误信息。
内容的提问来源于stack exchange,提问作者Darek
相关产品推荐
相关产品推荐

