MysteryDev
MysteryDev

Reputation: 638

nginx rewrite with try_files 403

I have a webserver with /usercp/ and usercp.php. I'm using tryfiles and re-write to see if file.php exists do file, otherwise goto /file/ (in my case file = usercp)

Here is my nginx conf.

location / {
    try_files $uri $uri/ @extension-php;
}

location @extension-php {
    rewrite ^(.*)$ $1.php last;
}

This also makes site.com/usercp/ give a 403 error. Any ideas?

Upvotes: 2

Views: 5029

Answers (2)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42799

The problem is that you are prioritizing the folder indexing over the php file if you want the opposite I recommend not to use the autoindex on because it exposes the contents of your folder and swap the last 2 items in the try_files, try this

location / {
    try_files $uri $uri.php $uri/;
}

PS: $uri/ will always return 403 if it doesn't contain the index file specified in index because by default it forbids folder listing, you should either put the index file if that's what you intend to do, or just remove the whole $uri/ from the try_files so that it would return 404 instead of 403

Upvotes: 3

cnst
cnst

Reputation: 27218

http://nginx.org/r/try_files

What it does is simply checks the existence of files, and then serves the file that exists.

You claim /usercp/ exists. As such, that's what it'll try to serve. But you probably don't have autoindex on, hence, directory listing is disallowed — 403 Forbidden.

Upvotes: 1

Related Questions