Nginx proxy pass chaining issue
My setup is as follows: I have server 1 which is open to public and server 2 which is open only to server 1.
Request should come to the server 1 and then get proxied to server 2 which proxies it further to a backend app running locally. What actually happens is that request goes to the other/default server block instead to the subdomain1.domain.com.
When I curl the subdomain1.domain.com from the Server 1 the request is routed correctly. What am I missing?
I've also tried adding proxy_set_header Host $host;
and proxy_redirect off;
but none worked.
Server 1 config:
upstream my-api {
server subdomain1.domain.com:80;
}
server {
listen 443 ssl http2;
server_name subdomain2.subdomain1.domain.com;
ssl_certificate /path/file.cert;
ssl_certificate_key /path/file.key;
access_log /var/log/nginx/my-api.access.log;
error_log /var/log/nginx/my-api.error.log;
location / {
proxy_pass http://my-api;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
listen 80;
server_name subdomain2.subdomain1.domain.com;
return 301 https://$host$request_uri;
}
Server 2 config:
server {
listen 80;
server_name subdomain1.domain.com;
access_log /var/log/nginx/api.en.access.log;
error_log /var/log/nginx/api.en.error.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:8001;
}
}
Figured it out finally.
I have added this line to the Server 1 config: proxy_set_header Host subdomain1.domain.com;
Only then it ends up in the correct server block. I hope it also helps someone else.