使用docker-compose时如何设置Nginx

时间:2019-04-07 18:02:41

标签: docker nginx docker-compose nginx-reverse-proxy

下面是我的ngionx.conf

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;

    keepalive_timeout  65;


    server {
        listen 80;
        access_log /var/log/nginx/access.log main;

        location /beta/ {
            proxy_pass         http://localhost:9001;
        }

        location /qa/ {
            proxy_pass         http://localhost:9002;
        }

        location /alpha/ {
            proxy_pass         http://localhost:9003;
        }

        location / {
            proxy_pass         http://www.google.com;
        }
    }
}

和下面是我的docker-compose.yml

version: '3'
services:
  Reverse-proxy:
    image: nginx
    ports:
      - 80:80
    volumes:
      - /nginx.conf:/etc/nginx/nginx.conf
    restart: always
  GQLbeta:
    image: gql-beta
    ports:
      - 9001:80
    restart: always
  GQLqa:
    image: gql-qa
    ports:
      - 9002:80
    restart: always
  GQLalpha:
    image: gql-alpha
    ports:
      - 9003:80
    restart: always

当我运行docker-compose up -d时,所有容器都运行良好。

然后我在浏览器上进入了localhost:80,它显示 pic

我希望看到Google页面。

当我进入localhost / beta时,它将显示

502 Bad Gateway

我希望它将转到本地主机:9001

为什么会这样?我想念一些设置吗?

1 个答案:

答案 0 :(得分:0)

docker容器中的

localhost是容器本身,因此您应为应用容器命名,并将其描述为上游-它将修复您的502。使用默认位置,请尝试以下操作:

location / {
    return 301 http://google.com;
}