简体   繁体   English

子域名,Nginx-proxy和Docker-compose

[英]Subdomains, Nginx-proxy and Docker-compose

I'm looking for a way to configure Nginx to access hosted services through a subdomain of my server. 我正在寻找一种方法来配置Nginx通过我的服务器的子域访问托管服务。 Those services and Nginx are instantiated with Docker-compose. 这些服务和Nginx都是用Docker-compose实例化的。

In short, when typing jenkins.192.168.1.2 , I should access to Jenkins hosted on 192.168.1.2 redirected with Nginx proxy. 简而言之,在输入jenkins.192.168.1.2 ,我应该访问使用Nginx代理重定向的192.168.1.2托管的Jenkins。

A quick look of what I currently have. 快速浏览一下我目前拥有的东西。 It doesn't work without a top domain name, so it works fine on play-with-docker.com , but not locally with for example 192.168.1.2 . 没有顶级域名它不起作用,因此它在play-with-docker.com上工作正常,但在本地不适用,例如192.168.1.2

server {
    server_name jenkins.REVERSE_PROXY_DOMAIN_NAME;
        location / {
            proxy_pass http://jenkins:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Host $host:$server_port;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
}

To have a look of what I want: https://github.com/Ivaprag/devtools-compose 看看我想要的东西: https//github.com/Ivaprag/devtools-compose

My overall goal is to access remote docker containers without modifying clients' DNS service. 我的总体目标是访问远程docker容器而不修改客户端的DNS服务。

Unfortunately nginx doesn't support sub-domains on IP addresses like that. 不幸的是,nginx不支持IP地址上的子域。

You would either have to modify the clients hosts file (which you said you didn't want to do)... 您可能要修改客户端主机文件(您说您不想这样做)...


Or you can just set your nginx to redirect like so: 或者您可以将nginx设置为重定向,如下所示:

location /jenkins {
    proxy_pass http://jenkins:8080;
    ...
}

location /other-container {
    proxy_pass http://other-container:8080;
}

which would allow you to access jenkins at 192.168.1.2/jenkins 这将允许您访问192.168.1.2/jenkins jenkins


Or you can try and serve your different containers through different ports. 或者您可以尝试通过不同的端口为不同的容器提供服务。 Eg: 例如:

server {
    listen 8081;
    location / {
        proxy_pass http://jenkins:8080;
        ...
    }
}

server {
    listen 8082;
    location / {
        proxy_pass http://other-container:8080;
        ...
    }
}

And then access jenkins from 192.168.1.2:8081/ 然后从192.168.1.2:8081/访问jenkins

If you are already using docker-compose I recommend using the jwilder nginx-proxy container. 如果您已经在使用docker-compose,我建议使用jwilder nginx-proxy容器。

https://github.com/jwilder/nginx-proxy https://github.com/jwilder/nginx-proxy

This allows you to add unlimited number of web service containers to the backend of the defined nginx proxy, for example: 这允许您向定义的nginx代理的后端添加无限数量的Web服务容器,例如:

nginx-proxy:
  image: jwilder/nginx-proxy
  ports:
    - "80:80"
    - "443:443"
  volumes:
    - "/etc/nginx/vhost.d"
    - "/usr/share/nginx/html"
    - "/var/run/docker.sock:/tmp/docker.sock:ro"
    - "nginx_certs:/etc/nginx/certs:rw"
nginx:
  build:
   context: ./docker/nginx/
   dockerfile: Dockerfile
  volumes_from:
     - data
  environment:
     VIRTUAL_HOST: www.host1.com
nginx_2:
  build:
   context: ./docker/nginx_2/
   dockerfile: Dockerfile
  volumes_from:
     - data
  environment:
     VIRTUAL_HOST: www.host2.com
apache_1:
  build:
   context: ./docker/apache_1/
   dockerfile: Dockerfile
  volumes_from:
     - data
  environment:
     VIRTUAL_HOST: www.host3.com

The nginx-proxy mount the host docker sock file in order to get information about the other containers running, if any of them have the env variable VIRTUAL_HOST set then it will add it to its configuration. nginx-proxy安装主机docker sock文件以获取有关正在运行的其他容器的信息,如果它们中的任何一个设置了env变量VIRTUAL_HOST,那么它会将其添加到其配置中。

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

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