在docker网络上设置nginx反向代理服务器出现502错误

localhost(web-browser) - docker_nginx(frontend static files) - docker_api_server 

我设置的docker网络如下

nginx server : port mapped with localhost {PORT_NGINX_SERVER}:{PORT_NGINX_SERVER} nginx server has static files set as root : nginx is frontend server nginx server should proxy path /api to API_SERVER 

以下是网络浏览器控制台的502错误信息

domain: localhost:{PORT_NGINX_SERVER} request: GET /api/path 

以下是nginx容器的502错误信息

connect() failed (111: Connection refused) while connecting to upstream, client: 172.25.0.1 (from browser - docker network gateway), server: localhost (nginx container), request: "GET /api/path HTTP/1.1", upstream: "http://172.25.0.6:{API_SERVER_PORT}/api/path", host: "localhost:{PORT_NGINX_SERVER}", referrer: "http://localhost:{PORT_NGINX_SERVER}/react_route_path" 

这里我设置了{PORT_NGINX_SERVER}, {PORT_API_SERVER}来快速理解(不是在真正的配置文件上)。

server { listen {PORT_NGINX_SERVER}; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html; } location /api { proxy_pass http://{API_SERVER_DOMAIN}:{PORT_API_SERVER}/api; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Host $http_host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_cache_bypass $http_upgrade; #proxy_set_header Origin ""; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-Pcol HTTP; proxy_set_header X-NginX-Proxy true; proxy_buffering on; proxy_buffers 64 4k; proxy_buffer_size 128k; proxy_busy_buffers_size 128k; } # ignore cache frontend location ~* (service-worker\.js)$ { add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'; expires off; proxy_no_cache 1; } } 

我的设置正确吗? 为什么显示502错误?

  • 我在API服务器上也做了CORS设置
  • 我还正确设置了nginx名称服务器作为docker的嵌入式服务器。
  • 同一个docker网络上的所有docker容器

原文链接:https://www.qiniu.com/qfans/qnso-66728014

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享