1

I have the following structure

/var/www/lsl/generalmedia
    subcontent1/
         picture.jpg
    subcontent2/
         subsub1/
             picture.jpg
         subsub2/
    index.php

Accessible via website.com/lsl/generalmedia

I would like to make the subdirectories use the same index.php file, but serve files normally. Examples:

website.com/lsl/generalmedia/subcontent1 needs to call lsl/generalmedia/index.php

website.com/lsl/generalmedia/subcontent1/picture.jpg must open the picture in your browser.

I have tried the following, but I'm getting 403 forbidden whenever I go to a subdirectory:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/;
    index index.php;
    server_name _;

    location /lsl/generalmedia/ {
            try_files $uri $uri/ @nested;
            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    fastcgi_param SCRIPT_FILENAME /var/www/lsl/generalmedia/index.php;
                    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            }
    }

    location @nested{
            rewrite /lsl/generalmedia/(.*)$ /lsl/generalmedia/index.php?/$1 last;
    }


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

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    }

}
5
  • Why would you want that?
    – Tommiie
    Oct 18, 2018 at 14:27
  • What's in the error log? Oct 18, 2018 at 14:34
  • @Tom I'm updating an old asset browser which needs to be able to explore folders under lsl/generalmedia in a slightly better looking way than standard old indexes Oct 18, 2018 at 14:43
  • Try: try_files $uri @nested; - and you can remove the nested location ~ \.php$ block as it's never invoked. Oct 18, 2018 at 14:43
  • @RichardSmith yep that's what was tripping it. I rewrote it to just try_files $uri /lsl/generalmedia/index.php?$args; so I wouldn't need the @nested location. I'm assuming it's because $uri/ tries to fetch the location, and since it doesn't have an index file it throws a 403 error? Feel free to post it as an answer, because it did what I was after. Oct 18, 2018 at 14:51

1 Answer 1

2

The $uri/ term in the try_files statement causes Nginx to test for the existence of a directory. See this document for details.

Because subcontent1 is a directory, Nginx processes it according to its index directive. The 403 response is because there is no index.php file within that subdirectory.

Remove that term, for example:

location /lsl/generalmedia/ {
    try_files $uri @nested;
}

Also, the nested location block is never visited as any URI ending with .php is currently processed by the location ~ \.php$ block at the end of your configuration.


If the specific rewrite in the @nested block is not required, the default PHP file can be specified in the same statement. For example:

location /lsl/generalmedia/ {
    try_files $uri /lsl/generalmedia/index.php?$args;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .