Ashish Sahu
Ashish Sahu

Reputation: 1558

Nginx Denied All files/nested files and forward all request to index.php

The goal is to block every file/nested file as well as root files and forward all traffic to index.php except those files reside inside the static directory or nested static directory.

Here is config so far it's redirecting all traffic but issue with blocking others.

listen               80 default_server;

root                 "/Applications/MAMP/htdocs/test";

access_log           /Applications/MAMP/logs/nginx_access.log;
error_log            /Applications/MAMP/logs/nginx_error.log;

# block hidden files
location ~ /\. {
    deny all;
}

# forward all traffic to index.php      
location / {
     index index.php;
     try_files $uri /index.php$is_args$args;
}


# block everthing else       
# location ~* ^\/.+$ { block all; }

# only allow files inside static directory (even from nested static)        
location ~* ^.*\/static\/.*$ {
    allow all;
}

Upvotes: 1

Views: 489

Answers (1)

Ivan Shatsky
Ivan Shatsky

Reputation: 15577

Try this one:

location / {
    rewrite . /index.php last;
}

location ~ /\. {
    deny all;
}

location ~* /static/ {
    try_files $uri /index.php$is_args$args;
}

Upvotes: 1

Related Questions