I have the following two server directives setup by including conf.d/*.conf. When I navigate to the server via the public, external IP address it seems to match the server for 127.0.0.1 as an entry is added to the localhost.access_log file. I expected this to only happen when accessing via localhost. From an external IP I expected to match the .host.tld server_name directive or the default_server parameter.

Do I need to ensure an order to how the conf files are loaded? Why would the listen on 127.0.0.1 be matched in this way? Is it the second location directive that is empty?

server {
    listen 127.0.0.1;
    server_name localhost;

    access_log /var/log/nginx/localhost.access_log main;
    error_log /var/log/nginx/localhost.error_log info;

    root /var/www/localhost/htdocs;
}

server {
    listen 80 default_server;
    server_name .host.tld;

    access_log /var/log/nginx/host.access_log main;
    error_log /var/log/nginx/host.error_log info;

    root /var/www/host/htdocs;

    location = / {
            index index.php index.html;
    }

    location / {
    }
}
up vote 1 down vote accepted

Changing both listen directives to 80 and adding the 127.0.0.1 name to the server_name of the localhost segment fixed it:

server {
    listen 80;
    server_name localhost;

    access_log /var/log/nginx/localhost.access_log main;
    error_log /var/log/nginx/localhost.error_log info;

    root /var/www/localhost/htdocs;
}

server {
    listen 80 default_server;
    server_name .host.tld;

    access_log /var/log/nginx/host.access_log main;
    error_log /var/log/nginx/host.error_log info;

    root /var/www/host/htdocs;

    location = / {
            index index.php index.html;
    }

    ...
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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