Not signed in (Sign In)
  1.  permalink
    Just a quick question on how nginx handles which virtual host site to display. Here is my nginx.conf file


    user www-data www-data;
    worker_processes 4;

    error_log /var/log/nginx/error.log;
    pid /var/run/nginx.pid;

    events {
    worker_connections 1024;
    }

    http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;

    sendfile on;
    tcp_nopush on;

    keepalive_timeout 5;
    tcp_nodelay off;

    gzip on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+r

    include /etc/nginx/sites-enabled/*;

    }


    in my sites-enabled directory I have 3 or 4 sites at any given time.

    My question is, when accessing my server via IP address instead of domain name, how does nginx choose which site to display?

    For instance, if I go to my server http://123.321.123.321 directly by IP address instead of via domain name like http://foobar.com, who does nginx choose which site to display? I can't seem to figure it out. Is it just whichever site gets loaded last from the sites-enabled directory? If that's the case, in what order does it process them because its not alphabetical
    • CommentAuthorgose
    • CommentTimeOct 15th 2008
     permalink
    One way to get around this is by creating a virtual host with the name of your IP address.
    ...
    server_name 123.321.123.321
    location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    }
    ...
    That way when someone hits your IP address directly, they won't get a virtual host that you don't want them to.
  2.  permalink
    Posted By: goseOne way to get around this is by creating a virtual host with the name of your IP address.
    ...
    server_name 123.321.123.321
    location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    }
    ...
    That way when someone hits your IP address directly, they won't get a virtual host that you don't want them to.


    thanks gose. I had already done that once I realize that nginx was picking an arbitrary vhost to display.

    I'm still curious as to how it picks which vhost to show though
    • CommentAuthorgose
    • CommentTimeOct 15th 2008
     permalink
    I can't find anything in their documentation other than them just recommending a catch-all in a virtual host environment:

    http://wiki.codemongers.com/NginxVirtualHostExample

    From my experience, nginx has passed direct IP access to the *first* virtual host listed. In Cent OS there is an /etc/nginx/conf.d/virtual.conf file where you specify your virtual hosts. Whatever one I specify first, always shows up when I access the site directly via the IP address.
  3.  permalink
    yeah, I assumed it was somthing like that.

    where I'm using

    include /etc/nginx/sites-enabled/*;

    I guess its just however the file system processes the include statement
    • CommentAuthorSchultz
    • CommentTimeOct 15th 2008
     permalink
    if you want a vhost to be the default you use the default keyword on the listen directive http://wiki.codemongers.com/NginxHttpCoreModule#listen
    so something like
    server {
    listen 80 default;
    }
    would make that server the default one to handle the request by IP:port address.

    And i think if no default specified it just passes it off to the first server block defined as it is scanning the config files. But not 100% sure on that.
    Thankful People: roshambo, ifthengoto, iChaitanya
  4.  permalink
    cool. thx
    • CommentAuthormgs
    • CommentTimeNov 25th 2008
     permalink
    cpj, this is just part of http, the way vhosts are able to work is that the server is told the exact domain specified by the browser. www.cpjolicoeur.com (for example :)..) points to your ip which is cool but what really matters to the nginx server is the data it gets from the http transmission telling the WEBSERVER what vhost to look (url entered).

    It's confusing at first but it's actually very straight forward. Just think of it as two seperate processes. 1 being DNS and 1 being the HTTP transmission. Nginx knows and cares nothing about DNS. That's why you can enable sites for fictitious domains without any ramification, until the server gets an http request from that url, it does nothing.

    anyone may feel free to correct me, that's off the top of my head. i just figured i'd post this because it seems like something people find particularly vexing and it's really very simple just easy to convolute. :)