T
T
Taras Labiak2018-04-24 18:13:04
Nginx
Taras Labiak, 2018-04-24 18:13:04

How to allow only certain URLs in nginx?

I need to restrict nginx to open only certain URLs with a regex, in JavaScript it looks like this

/^(\/[\w_-]*)+(.(php|js|css|jpg|png))?(\?([a-z][\w\[\]]*=[\w_-]*&?)*)?$/

Or pseudocode
^(/файлнейм)+(\.расширение)?(\?(ключ=значение&?)*)?$

nginx setup normal
server {
    listen 80;
    root /home/ubuntu/site;
    
    location ~ \.php$ {
        try_files $uri = 404;
        include fastcgi_params;
        #fastcgi_pass  unix:/var/run/php-fpm.sock;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }

    location / {
        try_files $uri /index.php?$query_string;
    }
}

For all other URLs - 404
If nginx did not find the file, then the URL processes index.php . It is desirable that files with the .php extension be also 404

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ainur Valiev, 2018-04-28
@kissarat

more or less like this

location / {
        deny all;                     # deny by default

        location ~* \.(php|js|css|jpg|png)$  {
            allow all;                
        }

    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question