40

I'm trying to set up a simple virtual host, serving only static files. Trouble is, directing the browser to (in this case) jorum.dev displays the default nginx welcome page, as opposed to jorum.dev/index.html.

Nginx was installed using Homebrew on Mac OS X Mountain Lion.

hosts

127.0.0.1       jorum.dev

jorum.dev

server {
    listen          80;
    server_name     jorum.dev;

    location / {
        root        ~/Sites/jorum;
        index       index.html index.htm;
    }
}

nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile        on;

    gzip            on;
    gzip_disable    "msie6";
    gzip_min_length 1100;
    gzip_vary       on;
    gzip_proxied    any;
    gzip_buffers    16 8k;
    gzip_types      text/plain text/css application/json application/x-javascript text/xml application/xml application/rss+xml text/javascript image/svg+xml application/x-font-ttf font/opentype application/vnd.ms-fontobject;

    server_tokens off;

    client_max_body_size    4096k;
    client_header_timeout   10;
    client_body_timeout     10;
    keepalive_timeout       10 10;
    send_timeout            10;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
2
  • 1
    where do you put 'jorum.dev' block? in nginx.conf?
    – freestyler
    Jan 22, 2013 at 14:37
  • 1
    No, it's in /usr/local/etc/nginx/sites-available/jorum.dev, symlinked to /usr/local/etc/nginx/sites-enabled/jorum.dev.
    – jdlm
    Jan 22, 2013 at 17:48

8 Answers 8

31

the debian/ubuntu nginx package comes with a default sites-available that takes over the default host dispatch. Simply remove the default link and it should load your site instead.


another gotcha, the debian/ubuntu nginx package also comes with a default index value for the default server in the default site that reads:

index index.html index.htm index.nginx-debian.html;

I've had the file index.nginx-debian.html show up in my www root directory, which took precedence over the cgi locations.


nginx has a verbose test mode that can be used to dump the entire configuration as parsed by nginx, which you can sanity check against your expectations.

nginx -T
7
  • 1
    for those not so familiar unix, is it ok to do this? $ mv site-enabled/ site-enabled_backup/
    – greenhouse
    Apr 13, 2016 at 7:52
  • @greenhouse IIRC the sites-* locations are declared in /etc/nginx/nginx.conf, I think it would be safe to move the location like you do so long as where nginx looks for configs still exists (if it even cares). you can always run sudo nginx -t to check if your changes are valid Apr 13, 2016 at 16:47
  • 9
    what I did was simply unlink sites-enabled/default.
    – mauris
    Jul 16, 2016 at 5:59
  • 1
    Thanks - this saved me from a few hours of research. The default being sym-linked from sites-available to sites-enabled was capturing my reverse proxy setup. Removing the sym link worked.
    – Edward
    Aug 16, 2016 at 17:56
  • I have spent 3 days to solve this, your solution saved me weeks of googling. Thanks so much
    – Wardruna
    Sep 5, 2019 at 9:42
24

Missing includes in nginx.conf

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

http://wiki.nginx.org/CoreModule#include

3
  • Thank you! After adding the include I got a regular 404 error, which in turn got me to trying out writing out the full path to the root, rather than using ~ for the home folder. Works now.
    – jdlm
    Jan 23, 2013 at 6:56
  • 6
    where to put include? Mar 27, 2019 at 19:56
  • @HerilMuratovic in /etc/nginx/nginx.conf
    – saytricky
    Sep 2, 2021 at 20:16
14

remove default.config from /etc/nginx/sites-available/ and /etc/nginx/sites-enabled/ and restart nginx

rm /etc/nginx/sites-available/default
rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx
1
  • This is perfect, I was trying to do this from within a docker container and this is exactly what it needed. However, from within the docker container, can't use systemctl, so nginx -s reload worked.
    – Biaspoint
    Nov 7, 2022 at 21:02
9

For other people coming to this thread and already tried @freestyler's answer, but still getting the default nginx screen, try to rename the default file located in /etc/nginx/sites-available/ to something like default.txt. I recommend not deleting the file, since it helps you explain what to do in your next .conf file.

1
  • 1
    If I do this, nginx does not start any more due to an error. But this answer lead me to the solution for me: I commented out the line include /etc/nginx/sites-enabled/* in the default config and it worked.
    – esel
    Mar 31, 2022 at 10:22
2

For me it was that I had forgotten to bind the site to IPv6 and my PC was on an IPv6 connection.

I simply added listen [::]:80; to the server object, before I only had listen 80;.

That made the Nginx show the site also if connections were made using IPv6.

1

Maybe unrelated, but hopefully will help people dropping on to this question...

Sometimes people forget to add the symlink in the /etc/nginx/sites-enabled pointing to the site config in the /etc/nginx/sites-available.

something like:

ln -s /etc/nginx/sites-availabe/mysite.com /etc/nginx/sites-enabled/mysite.com

0

Add the following to the location block:

index index.html index.htm

For example:

location / {
    …
    index  index.html index.htm;
 }
0

For me the issue was that I'd initially had the website at a subdomain, and when regenerating the certbot certificate to get it to also work at a new domain, I'd not added the new domain name redirects in server section, adding as follows fixed for me:

server {
    if ($host = domain_1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = domain_2.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    server_name domain_1.com domain_2.com;
    listen 80;
    return 404; # managed by Certbot
}

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.