1
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 容器
4

0 回答 0