xyious
xyious

Reputation: 1073

Nginx reverse proxy instead of directory listing

I'm trying to have nginx serve static content while reverse proxying everything else to a rails server.

All that works, except for the home page. If I go to example.com I get a 403 error and the error log shows

2019/06/14 04:32:59 [error] 9746#9746: *1 directory index of "/var/www/html/" is forbidden

I want the request to be sent to the rails server as example.com/ instead of trying (and failing) to get a directory listing. The rails server should display a homepage instead. (side note: if I turn autoindex on I will get a directory listing)

Configuration is here:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        server_name example.com;
        index index.html;
        location / {
            autoindex off;
            root /var/www/html;
            try_files $uri $uri/ @rails;
            expires max;
            access_log off;
        }
        location @rails {
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:3000;
        }

}

Upvotes: 1

Views: 418

Answers (2)

Richard Smith
Richard Smith

Reputation: 49802

If you want to disable the index behaviour for all URIs, drop the $uri/ term from the try_files statement. For example:

location / {
    try_files $uri @rails;
    ...
}

See this document for details.


Alternatively, add a new location block to specifically handle the URI /, for example:

location = / { 
    try_files nonexistant @rails; 
}

See this document for details.

Upvotes: 1

Danila Vershinin
Danila Vershinin

Reputation: 9895

A fix for the homepage would be to add an exact location for the homepage, like so:

    location = / {
        try_files @rails =404;
    }

Upvotes: 1

Related Questions