0

I have been trying to setup nginx as a proxy wherein request is routed to upstream based on subdomain. I hope example makes it more clear

upstream ubuntu {
  server  www.ubuntu.com:443;
}
upstream google {
  server  www.google.com:443;
}

server {
        listen 443 ssl ;

        ssl_certificate /etc/ssl/certs/client-certs/server.crt;
        ssl_certificate_key /etc/ssl/certs/client-certs/server.key;
        ssl_session_timeout 5m;
        ssl_protocols  TLSv1.2;
        resolver 127.0.0.1 ipv6=off;

    # Make site accessible from http://localhost/
    server_name ~^(.*)\.test\.com$;

    location / {
            proxy_pass https://$1;
            proxy_set_header X-Forwarded-Proto $scheme;
    }
}

so if i request https://google.test.com it should go to https://www.google.com

With this current setup nginx returns me 404 for any of the site that i query. Though i can see that request goes to my queried upstream site(in this case google.com).

Not getting if am missing anything.

avaj
  • 299
  • 1
  • 5
  • 11
  • You probably need to set the Host header. – Richard Smith Jul 27 '19 at 16:03
  • The upstream directive is for routing to a group of servers, not a single server. You are specifying an upstream server using the domain name, yet you have no resolver directive... – miknik Jul 27 '19 at 23:33
  • @RichardSmith I tried adding setting host header but i am still not able to make it work. proxy_set_header Host $1; – avaj Jul 28 '19 at 17:48
  • @miknik couldn't quite understand on _yet you have no resolver directive_ I have a local resolver running which should take care of it i suppose. Can you please explain it. – avaj Jul 28 '19 at 18:02
  • The upstream server probably needs the Host header to be set to something sensible like `www.example.com`, so with your existing scheme, that would be `proxy_set_header Host www.$1.com;` – Richard Smith Jul 28 '19 at 18:08
  • @avaj from the documentation, the resolver directive Configures name servers used to resolve names of upstream servers into addresses. – miknik Jul 30 '19 at 21:02
  • @RichardSmith I tried playing around with setting up host headers but result is still the same. – avaj Aug 05 '19 at 07:43
  • @miknik upstreams are getting resolved. – avaj Aug 05 '19 at 07:43

0 Answers0