I have two configs, one for each website. They work fine when used separately. When both are enabled then going to domainB serves content from domainA (it doesn’t redirect to it though).
DomainA:
server {
server_name domainA.com www.domainA.com;
listen 443 ssl; # managed by Certbot
listen [::]:443 ssl ipv6only=on; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domainA.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domainA.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
proxy_pass http://localhost:9100;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}
}
server {
server_name domainA.com www.domainA.com;
if ($host = www.domainA.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = domainA.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
return 404; # managed by Certbot
}
DomainB:
server {
server_name domainB.com www.domainB.com;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domainB.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domainB.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}
location /api/ws {
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
}
server {
server_name domainB.com www.domainB.com;
if ($host = www.domainB.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = domainB.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
return 404; # managed by Certbot
}
I tried adding adding IP address to listen directives, like so: 192.168.1.1:443
but this fails with: [emerg] bind() to 192.168.1.2:443 failed (99: Cannot assign requested address)
. I tried it because of this page: https://nginx.org/en/docs/http/configuring_https_servers.html, section “Name-based HTTPS servers”. I’m not sure if the problem is because of SSL.
It seems to me that the problem is that server_name
is somehow not respected for the servers that listen on 443 and by default domainA gets served.