Nginx wildcard proxy, pass subdomain to the server (upstream proxy)
Asked Answered
M

2

18

I would like to be able to pass subdomain.domain.com to .domain.com apache server, with subdomain info too.

I would like to make a nginx cache for domain, acting like wildcard, but passing subdomain to the destination (there is apache witch wildcard too). Up to now, I pass the info via proxy_set_header Host $host; but I would like to have request with subdomain at the apache server.

  upstream domain.com {
    server 172.1.1.1:80 weight=50 fail_timeout=30s;
  }

  server {
    server_name *.domain.com;

    location / {
      proxy_pass http://domain.com;
      #proxy_pass $request;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
     }

    location ~* ^.+.    (jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf)$ {
     proxy_pass http://topmanagergame.com;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_cache my-cache;
     proxy_cache_valid  200 302  30m;
     proxy_cache_valid  404      1m;
    }

    access_log /var/log/nginx/domain.com.log main;
    error_log off;
 }

Do you think I can use proxy_pass with upstream ?

Nginx (*wildcard_domain.com) --(cache)--> Apache (*wildcard_domain.com)
Nginx (anything.domain.com) --(cache)--> Apache (anything.domain.com)
Madder answered 18/10, 2012 at 8:45 Comment(0)
W
18
upstream somestring {
    server domain2.com:80 weight=50 fail_timeout=30s;
}

server {
    listen  80;
    server_name *.domain.com;

    server_name ~^(?<subdomain>.+)\.domain\.com$;

    location / {
        proxy_pass http://somestring;
        proxy_set_header   Host             $subdomain.domain2.com;
    }
}
Westhead answered 25/12, 2014 at 1:44 Comment(1)
Would be nice if there was some explanation around this answer. Having just code as an answer, while might be accurate, doesn't give any information about WHY it works. Knowing why something works is far more valuable than knowing what works.Paramedic
B
11

So I was trying to find the answer to this problem and kept finding this post. But I think dmytrivv answer is out of date. In our scenario, we have both wildcard domains (e.g. *.mydomain.com) and custom domains (e.g. fullycustomdomain.com). But you can solve both by using proxy_set_header Host $host; and having default at the end of your listen.

upstream qaweb {
    # Servers in the web farm
    server ip-notreal-name.ec2.internal:80;
}


server {
        listen 443 ssl default;

        ssl_certificate     certs/mydomain.com.crt;
        ssl_certificate_key certs/mydomain.com.key;

        # Support for wildcard domains
        server_name admin.mydomain.com *.mydomain.com "";

        location / {
                # Turn off access logging so we don't fill the hardrive
                access_log      off;
                proxy_pass http://qaweb;
                proxy_set_header Host $host;
                # So that the correct IP shows up in the log once libapache2-mod-rpaf is installed
                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

Note, we are also using it as a TLS termination proxy.

You can also find more examples on how to use proxy_pass here https://www.liaohuqiu.net/posts/nginx-proxy-pass/

Barye answered 1/3, 2017 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.