Docker nginx无法在默认网络

时间:2018-02-16 02:41:34

标签: docker nginx docker-compose

我遇到nginx反向代理作为docker容器的问题。我的问题是如何在默认的docker网络中正确代理传递nginx?

这是我的docker-compose.yml(为简洁省略了不必要的细节)

    version: '3'

    networks:
      nginx_default:
          external: true

    services:
      db:
        image: postgres:10.2
        ports:
          - "5432:5432"
        environment: ...

      postgrest:
        image: postgrest/postgrest
        ports:
          - "3000:3000"
        environment: ...

      nginx:
        restart: always
        image: nginx:latest
        ports:
          - "8080:80"
        volumes:
          - ./nginx/nginx.conf:/etc/nginx/sites-enabled/ruler
        command: [nginx-debug, '-g', 'daemon off;']

      webapp:
        build:
          context: "./"
          dockerfile: Dockerfile.dev
        volumes: ...
        ports:
          - "3001:3001"
        environment: ...

这是我的nginx.conf

    map $http_upgrade $connection_upgrade {
      default upgrade;
      ''      close;
    }

    server {
      listen 80;
      server_name _;

      gzip on;
      gzip_proxied any;
      gzip_types text/plain text/xml text/css application/x-javascript;
      gzip_vary on;
      gzip_disable "MSIE [1-6]\.(?!.*SV1)";

      access_log /var/log/nginx/access.log;
      error_log /var/log/nginx/error.log warn;

      location / {
        try_files $uri @node;
      }

      location /api/ {
        try_files $uri @postgrest;
      }

      location @node {
        proxy_pass http://webapp:3001;
        proxy_redirect off;
        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_cache_bypass $http_upgrade;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_cache_bypass $http_cache_control;
        add_header X-Proxy-Cache $upstream_cache_status;
      }

      location @postgrest {
        proxy_pass http://postgrest:3000;
        proxy_http_version 1.1;

        default_type application/json;

        proxy_set_header  Connection "";
        proxy_hide_header Content-Location;
        add_header Content-Location  /api/$upstream_http_content_location;
      }
    }

我的Dockerfile.dev

    FROM node:8.9

    WORKDIR /client

    CMD npm run dev -- -p 3001

当我做$ docker-compose up -d时,一切都开始没有错误。之后我可以成功地$ curl http://127.0.0.1:3001/(webapp)和$ curl http://127.0.0.1:3000(postgrest)。

但是当我尝试$ curl http://127.0.0.1:8080/时(nginx应该在这里处理代理)我得到默认的nginx欢迎页面。此外$ curl http://127.0.0.1:8080/api/未访问API:/

可能是什么原因?使用$ docker inspect我看到每个容器都在同一个默认网络中。

编辑:使用$ docker-compose logs似乎默认网络并未在所有O_o

中使用
    docker-compose logs
    WARNING: Some networks were defined but are not used by any service: nginx_default
    Attaching to ruler_webapp_1, ruler_nginx_1
    webapp_1  | 
    webapp_1  | > ruler@ dev /client
    webapp_1  | > next "-p" "3001"
    webapp_1  | 
    webapp_1  | > Using external babel configuration
    webapp_1  | > Location: "/client/.babelrc"
    webapp_1  |  DONE  Compiled successfully in 1741ms09:04:49
    webapp_1  | 

2 个答案:

答案 0 :(得分:0)

我的猜测是你将本地nginx配置文件映射到容器端的错误文件。 nginx图片的默认配置文件位于/etc/nginx/conf.d/default.conf,因此nginx容器的卷应为:

./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro

您可以执行以下命令检查配置文件是否正确使用:

docker-compose exec nginx nginx -T

附注:

  1. 永远不要使用latest标记,因为在某些时候您可能会面临兼容性问题。使用固定版本代码11.13等代替
  2. 您不需要在任何地方发布端口,例如。 3000:30003001:3001。这些端口可以通过容器内部访问

答案 1 :(得分:0)

您的配置是部分配置,而不是完整的nginx配置。因此,它需要进入容器内的conf.d而不是nginx.confsites-enabled。所以改变

    volumes:
      - ./nginx/nginx.conf:/etc/nginx/sites-enabled/ruler

    volumes:
      - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf

现在它应该开始工作了

相关问题