user7353167
user7353167

Reputation:

Nginx setup with multiple html files

I have got a website with multiple html files which I want to serve with Nginx.

server {
        listen 80;
        root /var/www;
        location / {
                index index.html;
        }

        location /projects/ {
                index projects.html;
        }

        server_name mylady17.de;


        location /shiny/ {
                proxy_pass http://104.248.41.231:3838/;
        }
}

This is the way it is set up. The index.html works perfectly fine, but however "http://mylady17.de/projects" gives me an error (404, not found). The projects.html file is stored in var/www/ and should work. What am I doing wrong? Why can´t I access the file?

Upvotes: 3

Views: 5608

Answers (1)

Richard Smith
Richard Smith

Reputation: 49702

The index directive operates on URIs which end with a / and attempt to locate files by appending the value of the directive to the URI. See this document for details.

So your URI /projects will not invoke the index module. Even if you did use /projects/ instead, the index module would attempt to locate the file at /var/www/projects/projects.html.


To point a single URI to a given file, you can use an exact match location. See this document for details.

For example:

location = /projects {
    rewrite ^ /projects.html last;
}

If you did decide to expand this in the future, requiring nginx to search for files by appending .html to the end of the URI, you could use a try_files directive instead. See this document for details.

For example:

location / {
    try_files $uri $uri/ $uri.html =404;
}

Upvotes: 3

Related Questions