I see there are multiple existing question but none seem to work.
Nginx reverse proxy for multiple domains and subdomains
$server_name not matching domain when multiple subdomains listed in nginx
I have the following functional nginx.conf...
server {
listen 80;
listen 443 ssl;
ssl_certificate /usr/src/b.pem;
ssl_certificate_key /usr/src/b.key;
server_name b.net www.b.net;
if ($host = www.b.net) {
rewrite ^/(.*)$ https://b.net/$1 permanent;
}
# include /usr/src/allow-cloudflare-only.conf;
location / {
proxy_pass http://${HOST_IP}:8123/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /usr/src/site.crt;
ssl_certificate_key /usr/src/site.key;
server_name a.com www.a.com;
if ($host = www.a.com) {
rewrite ^/(.*)$ https://a.com/$1 permanent;
}
location /services/ {
proxy_pass http://be:8080/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
proxy_pass http://ui:3000/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
This works fine and I can access both websites (a.com and b.net) using their respective domains. Now I want to access http://4.4.4.3/admin using pihole.b.net so I add the following...
server {
listen 80;
server_name pihole.b.net;
listen 443 ssl;
ssl_certificate /usr/src/b.pem;
ssl_certificate_key /usr/src/b.key;
location / {
proxy_pass http://4.4.4.3/admin;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I have an A DNS record that points to the IP address of NGINX
but when I access https://pihole.b.net it doesn't respond. How do I use NGINX to forward a subdomain with other forwarded domains?
Questions
- Why are you defining ssl certificates for HTTP sites?
My understanding is I can configure both with the same server. Of course the http does not require the cert only the https. I have seen this approach on multiple examples and I'll admit it is copy pasta but it also seems to work so I am not sure of the issue there.
- Why are you using the unprefixed hostname as the canonical name for the site?
Not sure about this one the site should be unprefixed except the last one (pihole) in other words after there should be 3 url going to 3 different proxies (a.com, b.net, pihole.b.net)
- Why are you using that 'if' to redirect?
this should be redirecting www.p.net to p.net
- Why don't you start investigating this?
I did as I tried to mention but probably didn't make clear is that the URL pihole.b.net works when I do not have the 3rd server in the conf. But once I add it to proxy to the other location it stops working. This is why I am pretty confident it isnt the DNS record. Also to be clear if I access the ip it is supposed to be proxying to directly everything works as expected.
I am rather new to NGINX so if there is a more effective way to structure the config I am all for it.