502通过Jwilder的Nginx代理访问虚拟主机时出现错误网关

时间:2016-04-06 08:28:24

标签: nginx docker docker-compose

我在同一台主机上运行了Nginx-proxy(Jwilder,默认配置)和Gitlab-Instance。 git.myhost.com指向主机IP。 当使用下面的docker-compose.yml启动Gitlab时,我会在访问http://git.myhost.com时获得502 Bad Gateway

nginx-proxy容器中生成的/etc/nginx/conf.d/default.conf看起来也很好:

upstream git.myhost.com {
                # 2ab9168d-c69e-4725-8c20-31a194ad8d07
                server 172.17.0.13 vhost;
}
server {
        server_name git.myhost.com;
        listen 80 ;
        access_log /var/log/nginx/access.log vhost;
        location / {
                proxy_pass http://git.myhost.com;
        }
}

我做错了什么?

这是Gitlab的docker-compose.yml

gitlab-server:
  hostname: git.myhost.com
  expose:
  - "8100"
  ports:
  - 8101:22/tcp
#  - 8100:8100/tcp
  labels:
    io.rancher.sidekicks: gitlab-data
  environment:
    GITLAB_OMNIBUS_CONFIG: |
      external_url 'http://git.myhost.com'
      gitlab_rails['gitlab_shell_ssh_port'] = 8101
    VIRTUAL_HOST: git.myhost.com
    VIRTUAL_PORT: 8100
  image: gitlab/gitlab-ce:latest
  volumes_from:
  - gitlab-data

gitlab-data:
  labels:
    io.rancher.container.start_once: 'true'
  entrypoint:
  - /bin/true
  hostname: gitdata
  image: gitlab/gitlab-ce:latest
  volumes:
  - /etc/gitlab:/etc/gitlab
  - /var/log/gitlab:/var/log/gitlab
  - /var/opt/gitlab:/var/opt/gitlab

2 个答案:

答案 0 :(得分:1)

你应该改变nginx如下:

upstream gitlab {
                # 2ab9168d-c69e-4725-8c20-31a194ad8d07
                server 172.17.0.13:8100;
}
server {
        server_name git.myhost.com;
        listen 80 ;
        access_log /var/log/nginx/access.log vhost;
        location / {
                proxy_pass http://gitlab;
        }
}

172.17.0.13是gitlab docker container的ip地址。

答案 1 :(得分:0)

可能导致此问题的常见问题之一是在nginx-proxy网络外部运行容器,而nginx-proxy最终无法访问该容器。

要在nginx-proxy所在的同一网络中运行它,首先,您需要找出nginx-proxy在哪个网络中运行。只需运行以下命令即可找出答案:

docker network ls -f driver=bridge --format "{{.ID}}" | xargs docker network inspect | grep Name

它应该给你这样的东西:

    "Name": "bridge",
            "Name": "influxdb",
            "Name": "mariadb",
    "Name": "http_proxy",
            "Name": "web-app",
            "Name": "proxy",
            "Name": "grafana",

现在,我可以看到名称为proxy的容器在名为http_proxy的网络下。 我要做的就是在网络中运行新容器,方法是添加标志--network http_proxy(用您的网络名称替换http_proxy

docker run -d --name test --network http_proxy -h test.local -e VIRTUAL_HOST=test.local -v "$PWD":/usr/share/nginx/html nginx

希望这会有所帮助。 祝你好运!

相关问题