-3

I am brand new to nginx and have it running on a VM (mynginx.example.com) and successfully serving back some static content. So far so good.

However, it appears to be configured so that you need to specify both the port and the file you wish toview. This is mainly a problem on the homepage (index.html) because I'd like users to be able to just go to http://mynginx.example.com and see my homepage.

So if I open a browser and go to http://mynginx.example.com:8080/index.html, my homepage (and all other static content) loads up just fine. But, when accessing the homepage, if I omit either the port or index.html, the browser chokes and nginx seems to refuse to serve back anything. Meaning, the following two HTTP requests do not work:

  • http://mynginx.example.com:8080 <-- nope
  • http://mynginx.example.com/index.html <-- nope

Below is the nginx.conf that I'm using:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

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

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

    gzip on;
    gzip_disable "msie6";

    server {
        listen       80;
        server_name   mynginx.example.com;

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

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

Can anybody figure out what I need to do in order to make http://mynginx.example.com the same as http://mynginx.example.com:8080/index.html (from nginx's perspective)? Meaning, I'd like the users to be able to just use the main domain name and have nginx serve back index.html (which is located at /opt/mysite/index.html).


Update

I changed 8080 to 80 and that fixed the port issue. However, now (with port 80 specified), going to both http://mynginx.example.com and http://mynginx.example.com/index.html have the exact same effect: they take you to the default nginx page (Welcome to nginx!)...

So by changing the port from 8080 -> 80 now some nginx default is kicking in and is preventing me from getting to my index.html page.

I did have HTTP GET errors in the error log but I fixed them and they were separate from the behavior I'm seeing here. The logs (access and error) are now empty.

8
  • 1
    The default port for HTTP is 80 so first change the listen directive.
    – HBruijn
    Mar 21, 2016 at 15:15
  • 1
    What is the exact error when accessing http://mynginx.example.com/index.html? What is the purpose of root html; in the location = /50x.html block? In general root directive should be in the server level, not inside a location. Mar 21, 2016 at 15:56
  • 1
    If you can post your access.log and error.log that would help. @FlorinAsăvoaie given the first person to comment on is a community moderator and didn't think it should be closed, and there are some +8000 questions on Nginx here and it doesn't appear to be a "home user" question SF would the more suitable place
    – Drifter104
    Mar 21, 2016 at 16:17
  • 1
    Nginx default_server configuration will handle requests if the request doesn't match a server_name If this is your only site try commenting out your 3 include lines as this will remove any other configurations from the question
    – Drifter104
    Mar 21, 2016 at 17:57
  • 1
    Try a different approach, let's side step index for the moment. Create another file in the same place as your index file called hello.html, stick a bit of html in there and see if you can view it using mynginx.example.com/hello.html
    – Drifter104
    Mar 21, 2016 at 23:49

1 Answer 1

3

All of your questions can be answered with an Nginx beginners tutorial and a tiny bit of thought. You need to read up on the basics of web servers and Nginx.

The reason it's displaying index.html when you request the root of the domain is because you told it to.

location / {
    root   /opt/mysite;
    index  index.html index.htm; # This sets the index
}
2
  • Thanks @Tim (+1) - However it sounds like you are misunderstanding the situation. If I go to http://mynginx.example.com, it keeps me there (i.e. does not display my index.html page). It just displays the default nginx Welcome! page. Also, that tutorial doesn't mention an index property anywhere. It sounds like you are saying that I want to change my index property values from index.html index.htm to something different. Any idea what that should be?
    – smeeb
    Mar 21, 2016 at 19:28
  • I may have misunderstood. I notice you defined your server (aka website) within the main nginx config file, but you still include the sites-enabled and conf.d directories, which may be the issue. You'll also need to check logs to see what's going on, and of course restart nginx after any configuration change is made. Have another look and report back what you find, and preferably post all configuration files.
    – Tim
    Mar 21, 2016 at 20:12

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