nginx proxy_pass 与 docker 1.12 和 swarm

nginx proxy_pass 与 docker 1.12 和 swarm

我正在尝试设置一个具有多个网络的 docker swarm,这样我就可以在 swarm 上的不同网络上运行一组服务(不同版本)。我还想在 swarm 之外访问这些服务,但不想每次启动新服务时都争抢端口

为了解决这个问题,我尝试使用 nginx 设置代理,这样我就可以指向http://service-v1.swarm-master/config/到我的 etcd-viewer 运行http://etcd-viewer.rest-test:8080休息测试覆盖网络;全部在 Swarm 上。这现在让我很头疼!

当访问 http:// service-v1.swarm-master/config/ 时,我看到了 404 页面:

HTTP ERROR 404
Problem accessing /config/. Reason:
Not Found
Powered by Jetty://

在我的 nginx 容器内部,我已确保可以通过执行ping -c1 etcd-viewer和来访问我尝试访问的服务ping -c1 etcd-viewer.rest-test。在正在运行的 nginx 容器内部可以访问这两项服务。

那么为什么我的 nginx.conf 似乎不起作用?它安装在/etc/nginx/conf.d/

resolver 127.0.0.11;

upstream config {
  server etcd-viewer.rest-test:8080;
}

log_format compression '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $request_body ($body_bytes_sent) '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';

server {
  listen 80;
  #server_name docker.cdrator.com;

  access_log /var/log/nginx/access.log compression;

  # disable any limits to avoid HTTP 413 for large image uploads
  client_max_body_size 0;

  # required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
  chunked_transfer_encoding on;

  location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

  location /config/ {
    proxy_pass                          http://config;
    proxy_set_header  Host              $http_host;   # required for docker client's sake
    proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
    proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto $scheme;
    proxy_read_timeout                  900;
  }

  location /basic_status {
      stub_status;
  }
}

答案1

对配置做了一些小改动,并决定覆盖主 nginx.conf。这不是很干净,但目前可以工作。

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;


    upstream config {
      server etcd-viewer.rest-test:8080;
    }

    server {
      listen 80;

      location /etcd {
        proxy_set_header  Host              $http_host;   # required for docker client's sake
        proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
        proxy_read_timeout                  900;

        proxy_pass                          http://config;
      }

      location /basic_status {
          stub_status;
      }
    }

    include /etc/nginx/conf.d/*.conf;


}

相关内容