问题:如何在 Docker for Windows 上使用 nginx 作为 localhost 的反向代理?

我正在使用 Docker for Windows 并希望将 nginx 设置为反向代理。一切正常,但如果我想为我的本地主机定义一个代理,我总是会收到 502 或 504 错误。我认为设置一个extra_host可以解决我的问题,但没有。我可以尝试将其他 IP 设置为主机还是有其他问题?

码头工人-compose.yml:

version: '3'

volumes:
  etc:
      driver: local

services:
  nginx:
      container_name: nginx
      image: nginx:latest
      volumes:
        - ./etc:/etc/nginx
      ports:
        - 8088:80
      extra_hosts:
        - localhost:127.0.0.1

nginx.conf:

user  nginx;
worker_processes  1;

events { 
}

http {
  server {
    listen 80;
    server_name localhost;

    location /auth {
      proxy_pass http://localhost:8082/auth;
    }

    location /graphql {

        proxy_pass http://localhost:8080/graphql;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_http_version 1.1;

    }

    location ^~ / {
        proxy_pass http://localhost:8082/auth;
    }

    location /sso/login {
        proxy_pass http://localhost:8082/auth;
    }

  }

} 

PS: 所有引用的路径都是 docker 容器,例如/auth是keycloak认证服务器

解答

我自己解决了这个问题。如果您打开 docker 设置(右键单击 docker 图标),那么您有以下网络设置。 在此处输入图像描述默认情况下,DNS 服务器设置为自动 -> 将其更改为 fixed 8.8.8.8 然后您可以使用 10.0.75.2 而不是 localhost 访问您的容器。最后但同样重要的是,将此地址作为extra_host添加到您的 docker-compose 文件中并启动它。

version: '3'

volumes:
  etc:
      driver: local

services:
  nginx:
      container_name: nginx
      image: nginx:latest
      volumes:
        - ./etc:/etc/nginx
      ports:
        - 8088:80
      extra_hosts:
        - localhost:10.0.75.2
Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐