A
A
akdes2020-06-15 16:07:05
Nginx
akdes, 2020-06-15 16:07:05

Nginx configuration - all requests only to index.php, how?

Hi all!

There is a service on php only backend. Work happens only through index.php.

Here is the config:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www;
    index.php;

    server_name _;

  

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

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


Works great, but because the request is necessary only to index.php, I want to secure everything else located in /var/www, for example change.log should not be available.

I tried to leave only one of the locations, so that all requests go to php-fpm at once, without loading static files, etc.
location / {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

but then requests like site.com/api/1/2/3 are lost - 404. And I need them to also go to index.php.

I tried to remove try_files, but something went wrong here too ... I didn’t understand until the end ..

Thank you

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lynn "Coffee Man", 2020-06-15
@akdes

For example like this:

location / {
    rewrite ^ /index.php break;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}

E
Eugene, 2020-06-15
@Nc_Soft

location / {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME /var/www/index.php;
        include fastcgi_params;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question