在 nginx 代理上定义多个 proxy_pass

在 nginx 代理上定义多个 proxy_pass

已经将 nginx-proxy 作为容器运行好几年了。非常顺利,没有问题。我为此使用了 Docker-compose.yml:

    services:
  nginx-proxy:
    image: jwilder/nginx-proxy:alpine
    container_name: nginx-proxy
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx-certs:/etc/nginx/certs:ro
      - ./nginx-vhost:/etc/nginx/vhost.d
      - ./nginx-html:/usr/share/nginx/html
      - ./uploadsize.conf:/etc/nginx/conf.d/uploadsize.conf
      - ./subdomain1.mytld.com.conf:/etc/nginx/conf.d/subdomain1.mytld.com.conf:ro

现在我想使用 proxy_pass 访问本地 IP 地址上的另一个服务。问题。它只在定义了其中一个子域时才起作用,而不能在同时激活两个子域时起作用。

工作(subdomain1.mytld.com.conf的内容):

server {
        listen 443 ssl default;
        ssl_certificate     certs/mytld.com.crt;
        ssl_certificate_key certs/mytld.com.key;
        server_name subdomain1.mytld.com;
        location / {
                proxy_pass https://10.8.0.4/;
        }
#        server_name subdomain2.mytld.com;
#        location / {
#                proxy_pass http://10.8.0.4:8096/;
#        }
}

不起作用:

server {
        listen 443 ssl default;
        ssl_certificate     certs/mytld.com.crt;
        ssl_certificate_key certs/mytld.com.key;
        server_name subdomain1.mytld.com;
        location / {
                proxy_pass https://10.8.0.4/;
        }
        server_name subdomain2.mytld.com;
        location / {
                proxy_pass http://10.8.0.4:8096/;
        }
}

一旦我激活其中两个 server_name,整个 nginx 代理就会停止工作。我错过了什么?感谢您的帮助

答案1

发现问题了。服务器标签只允许使用一次“默认”。因此,使用如下配置即可:

server {
        listen 443 ssl default;
        ssl_certificate     certs/mytld.com.crt;
        ssl_certificate_key certs/mytld.com.key;
        server_name subdomain1.mytld.com;
        location / {
                proxy_pass https://10.8.0.4/;
        }
}

server {
        listen 443 ssl;
        ssl_certificate     certs/mytld.com.crt;
        ssl_certificate_key certs/mytld.com.key;
        server_name subdomain2.mytld.com;
        location / {
                proxy_pass http://10.8.0.4:8096/;
        }
}

相关内容