0

So I'm using Nginx as a reverse proxy, and my current config has gotten very long, since I have about 12 sub-domain's, and that number will continue to grow a little more. I'm looking for a way to clean up my config, and I think I found a way but I can't get it to work. Here is what I'm trying:

server {
  server_name  ~^(.+)\.domain\.com$;
  ssl  on;

  location ~ domain1 {
    proxy_pass  http://192.168.1.50:4040/;
  }

  location ~ domain2 {
    proxy_pass  http://192.168.1.60:4040/;
  }
}

But it's giving me an error that says

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except"

And I understand what it's saying, but unsure how to resolve it. If I remove the trailing slash from the proxy_pass directives, Nginx will start without error but neither of the subdomains work.

Am I going about this the right way? Is there a better way to do it?

Thanks in advance!

3
  • Why not use separate server blocks for each server_name?
    – womble
    Nov 4, 2015 at 4:34
  • Why not use separate conf file for each subdomain? That's how I have it, works perfectly. I call mine sub_domain_tld.conf and when I have a new one, I copy a previous one, amend it slightly, presto.
    – JayMcTee
    Nov 4, 2015 at 8:03
  • I am currently using separate server blocks, but as I said, my config is growing quite large. I have to scroll to to see all the config to verify everything is there, and it's annoying to me. I could do separate conf files, but again, I'd have to open each of them to verify configs (some have auth_basic, others have different proxy_pass params, etc). I'm literally hoping I can shrink my config to where I can see it all easier.
    – tycoonbob
    Nov 4, 2015 at 16:08

1 Answer 1

0

As mentioned in the comments, the best way (most readable and fastest) is to manually configure each domain. This is because NGINX will create static entities in memory to avoid the runtime processing of each request.

However, if you really want to do this (proxy_pass to different places for different subdomains) then you can do this:

E.g.

map $http_host $my_upstream {
    default http://192.168.1.1:8080;
    domain1.* http://172.0.0.1:8081;
    domain2.* http://10.0.0.1:80802;
}

server {
    server_name ~(.+)\.domain\.com;

    location / {
        proxy_pass $my_upstream;
    }
}

Note that the map needs to be outside the server block

2
  • While this would work, it would require that all my locations have the exact same params, but that's not true unfortunately. Some have auth_basic, one have client_max_body_size, one has proxy_buffering off, etc. Right now it works with a bunch of separate server blocks, but it's getting difficult to scroll through and very everything was there. If I could do something like what I posted above, it would shrink my config and make it easier to see everything without scrolling or opening multiple configs. Sounds lazy, I know.
    – tycoonbob
    Nov 4, 2015 at 16:10
  • And thanks for the reminder that having static config would be loaded in memory, and avoiding runtime processing.
    – tycoonbob
    Nov 4, 2015 at 16:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .