Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have two back-ends with different data that I want to test my UI code against, so I'm trying to configure Nginx to allow me to switch between them based on URL.

So for example, the local URL https://ui.local.otherserver:80 would try my local files then proxy anything else to https://otherserver:80.

I'm trying to avoid having two server blocks as the config below is simplified and duplication is a pain.

The behaviour I'm seeing is that one of the back-ends works fine, and the other one gives me a 502 (Bad Gateway) error, but if I hard-code either server's name in the proxy_pass line it works as expected, i.e. successfully proxies to that specific server.

Any ideas what I'm doing wrong?

ssl             on;
ssl_certificate      /usr/local/etc/nginx/conf.d/cert.ui.crt;
ssl_certificate_key  /usr/local/etc/nginx/conf.d/cert.ui.key;

server {
    listen          443;
    listen          80;
    server_name     ~^ui\.local\.(?<backend>.+)$;
    expires         -1;
    access_log      /var/log/nginx/ui.access.log;
    error_log       /var/log/nginx/ui.error.log;

    root /Users/richard/Projects/ui/trunk/;

    location ~ ^(.*)$ {
        try_files $1 @platform;
    }
    location @platform {
        proxy_pass https://$backend:$port;
    }
}
share|improve this question
    
Anything in the nginx error log? –  mgorven Mar 4 '13 at 17:26
    
In fact, you will have much more pain with overcomplicated ineffective config that you are trying to accomplish, instead of two simple server blocks. A web-server configuration is not a programming language, and should not be. So, why bother about duplication, then? –  VBart Mar 4 '13 at 19:44
    
@mgorven yes! "no resolver defined to resolve..." this was a DNS issue, now fixed. Thanks for that. If you write up an answer I'll give you the credit –  Richard Marr Mar 5 '13 at 11:11
    
@VBart, as I mentioned, my real config is much larger than this and evolving, and I keep having to make every change twice. This has solved an actual real-world problem for me, so your argument about what a web server config should or shouldn't be seems a little academic. Happy to listen if you want to rephrase it in more pragmatic terms. –  Richard Marr Mar 5 '13 at 11:16

2 Answers 2

up vote 1 down vote accepted

502 is a Bad Gateway error, which means that nginx was unable to communicate with the backend. There should be a more detailed message in the nginx error log explaining what the communication error was. Have a look in /var/log/nginx/error.log (or similar) to find the actual cause.

share|improve this answer

You need to set explicit "resolver" directive in your server config or the location config for this to work. With use of variables in proxy_pass directive, NGINX needs to do a run time resolution. Here gethostbyname() cannot be used since its a blocking call. So NGINX expects an async resolver to be configured, otherwise it will give 502 bad gateway.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.