0

This is a part of my /etc/nginx/nginx.conf file. (I've modified it here slightly as to not disclose the actual IP address or DNS entry, but you get the idea.)

server {
    listen 80;
    server_name ec2-52-1-2-3.compute-1.amazonaws.com;
    location / {
        proxy_pass http://thumbor;
    }
}
server {
    listen 80;
    server_name 52.1.2.3;
    location / {
        proxy_pass http://thumbor;
    }
}

I've also got an upstream section for thumbor, based on this guide. And this works; I can access it via http://ec2-52-1-2-3.compute-1.amazonaws.com/... or via http://52.1.2.3/...

However I feel that it's a little bit fragile the way it's set up right now. What if the IP address changes? Or what if the DNS entry changes? I would prefer if it would simply respond to all requests on port 80, regardless of the hostname.

So naturally I tried doing this instead:

server {
    listen 80;
    location / {
        proxy_pass http://thumbor;
    }
}

As you can see all I did there was pare it down to a single server section instead of two, and I removed the server_name variable from that section. But rather than responding to all requests like I hoped, it no longer responded to any. It would 404 on everything.

How can I set up a generic server listener like I'm describing here?

2

No need for regular expressions. You can do it by appending default_server to your listen directive, like so: listen 80 default_server;. This points to the configuration which is to be applied if a request is directed at server IP but no hostname is matching.

  • I left this question open because I was hoping someone would come along just like this and show me a better way! Thank you. However... I think I'm still missing something. I removed the server_name variable altogether and added default_server to the listen just as you instructed. But then when I tried running sudo service nginx restart it said [ fail ]. What did I do wrong? – soapergem Apr 15 '15 at 16:53
  • Leave the server_name directive in place - it's required and doesn't interfere with default_server. You can test nginx configuration with nginx -t before attempting a restart. The output may be helpful in debugging. – Erathiel Apr 15 '15 at 17:02
1

Wait... I figured it out, after realizing that you can use regular expressions. I ended up doing this as my single server section:

server {
    listen 80;
    server_name ~^.+$;
    location / {
        proxy_pass http://thumbor;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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