Define multiple proxy_pass on nginx proxy

Define multiple proxy_pass on nginx proxy

running nginx-proxy as a container for several years now. Perfect smooth, no problems. Docker-compose.yml I am using for that:

    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

Now I want to use proxy_pass to another service on a local ip address. Problem. Its working only with one of these subdomains defined, not with both activated.

Working (content of 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/;
#        }
}

NOT working:

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/;
        }
}

As soon as I activate two of these server_names the whole nginx proxy stops working. What am I missing? Thanks for your help

답변1

Found the hickup. The server tag only allows the use of "default" one time. So, using the config like that works:

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/;
        }
}

관련 정보