51

this is my first web app deployment and am running into all sorts of issues.

I am currently going for a nginx + gunicorn implementation for the Django app, but mostly this question relates to nginx configurations. For some context - nginx would receive connections and proxy to the gunicorn local server.

in the nginx configurations, where it says server_name do I have to provide one? I don't plan to use domain names of any kind, just through my network's external ip (it is static) and the port number to listen to.

My desire is that when I access something like http://xxx.xxx.xxx.xxx:9050 I would be able to get the site.

The following is the sample code that I will base the configurations on for reference.

   server {
        listen   80;
        server_name WHAT TO PUT HERE?;

    root /path/to/test/hello;

    location /media/ {
        # if asset versioning is used
        if ($query_string) {
            expires max;
        }
    }
    location /admin/media/ {
        # this changes depending on your python version
        root /path/to/test/lib/python2.6/site-packages/django/contrib;
    }
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    }
        # what to serve if upstream is not available or crashes
        error_page 500 502 503 504 /media/50x.html;
     }
bash-
  • 747
  • 1
  • 6
  • 10

3 Answers3

44

server_name defaults to an empty string, which is fine; you can exclude it completely.

Another common approach for the "I don't want to put a name on this" need is to use server_name _;

Your http://xxx.xxx.xxx.xxx:9050 URL won't work with this config, though; you're only listening on port 80. You'd need to add a listen 9050; as well.

Shane Madden
  • 112,982
  • 12
  • 174
  • 248
9

server_name _; is not a wildcard see here:

http://blog.gahooa.com/2013/08/21/nginx-how-to-specify-a-default-server

just specify the default_server directive for ip-only access (see http://nginx.org/en/docs/http/request_processing.html)

server {
    listen 1.2.3.4:80 default_server;
    ... 
    }
Joe
  • 197
  • 1
  • 4
  • 2
    It's discouraged to simply link to the useful piece of information and not include it in your answer - in this case that the `server_name _;` is a non-match. – BE77Y Feb 20 '15 at 09:01
  • 1
    "is not a wildcard" == "is a non-match". I'm sure they'll get it. – Joe Feb 21 '15 at 01:06
  • 2
    The two aren't equal; non-match is what is served when nothing else matches, whilst a wildcard matches everything. The point here however is that the useful information should be in the answer, not linked on some other site (however well presented on that site), as detailed [here](http://serverfault.com/help/how-to-answer) – BE77Y Feb 21 '15 at 08:50
  • And the point is that this is not a 'good answer' but it's an answer. Sorry no time. If you would like to edit/rewrite it, I give you full permission to. Otherwise, they will know exactly what to do, and it is helpful. Feel free to rewrite it, otherwise the answer stands. – Joe Feb 21 '15 at 19:32
  • An extra line? I hardly think a single line is what turns this from a 'bad' answer to a good one. It would never appease all the 'answer Nazis' out there that have ruined SE/Wiki. And age notwithstanding, this question is as relevant today as it was the day it was asked. – Joe Feb 22 '15 at 00:43
  • Link does not finish loading. – Majid Fouladpour Jun 07 '16 at 05:59
  • link works. But you don't need it, just specify default_server – Joe Jun 21 '16 at 15:47
4

If you want your app to respond on port 9050 without specific hostname then you can just skip server_name, it's not required since Nginx first resolves listen entry and then server_name if present:

server {
   listen 9050;

   .....
}

More details here: Nginx server_name and how it works

gansbrest
  • 785
  • 1
  • 8
  • 17