Score:0

Define multiple proxy_pass on nginx proxy

bz flag

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

Lex Li avatar
vn flag
I believe the actual error message reported by nginx (`nginx -t`) is clear enough. Why didn't you include that in the question? Like you found out yourself, you simply cannot have duplicate `location /` in the same server block.
Score:0
bz flag

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/;
        }
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.