简体   繁体   English

如何在 Docker for Windows 上使用 nginx 作为本地主机的反向代理?

[英]How to use nginx on Docker for Windows as reverse proxy for localhost?

I am using Docker for Windows and want to set up nginx as reverse proxy.我在 Windows 上使用 Docker,并希望将 nginx 设置为反向代理。 All is working fine but if I want to define a proxy to my localhost I always get a 502 or 504 error.一切正常,但如果我想为本地主机定义代理,我总是会收到 502 或 504 错误。 I thought setting an extra_host would solve my problem but didn't.我认为设置一个extra_host会解决我的问题,但没有。 Is there any other IP that I can try to set as host or is something else wrong?我可以尝试将任何其他 IP 设置为主机还是其他错误?
docker-compose.yml: docker-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: 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: all referred paths are docker-containers eg /auth is a keycloak authentification server PS:所有引用的路径都是 docker-containers,例如/auth是一个 keycloak 身份验证服务器

I solved this problem myself.我自己解决了这个问题。 If you open the docker settings (right-click on docker icon) then you have the following network settings.如果您打开 docker 设置(右键单击 docker 图标),那么您将获得以下网络设置。 在此处输入图片说明 Per default the DNS server is set to automatic -> change this to fixed 8.8.8.8 Then you can access your containers with 10.0.75.2 instead localhost.默认情况下,DNS 服务器设置为自动 -> 将其更改为固定 8.8.8.8然后您可以使用10.0.75.2而不是 localhost 访问您的容器。 Last but not least add this address as extra_host to your docker-compose file and fire it up.最后但并非最不重要的是,将此地址作为extra_host添加到您的extra_host -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

Looking at the documentation, with docker for mac we can use host.docker.internal to resolve the internal IP used by the host查看文档,使用host.docker.internal for mac我们可以使用host.docker.internal来解析主机使用的内部IP

location /api {
    proxy_pass   http://host.docker.internal:8080;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM