G
G
Gelloiss2021-06-08 01:57:52
Nginx
Gelloiss, 2021-06-08 01:57:52

How to write nginx php routing instead of 404 error?

It is necessary to write a routing so that when there was a request to site/dir/123, in the absence of file 123, the request was redirected to site/dir/index.php?code=123 On Apache, this was implemented like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Za-z]*)$ index.php?code=$1 [L,QSA]


Now the server on PHP-FPM + NGINX has found a config that belongs to the domain for which routing needs to be done. Tried to use several different constructs, like

if (!-e $request_filename){
  rewrite ^/(.*) /index.php?code=$query_string;
}


or

error_page 404 = @routing;
location @routing {
  rewrite ^/(.+)$ /index.php?_route_=$1 last;
}


But nothing happened. As a result, the config file now looks like this

server {
    server_name site.com www.site.com;
    charset UTF-8;
    index index.php index.html;
    disable_symlinks if_not_owner from=$root_path;
    include /etc/nginx/vhosts-includes/*.conf;
    include /etc/nginx/vhosts-resources/site.com/*.conf;
    access_log /var/www/httpd-logs/site.com.access.log;
    error_log /var/www/httpd-logs/site.com.error.log notice;
    ssi on;
    set $root_path /var/www/www-root/data/www/bot;
    root $root_path;
    location / {
        if (!-e $request_filename){
                        rewrite ^/(.*) /index.php?code=$query_string;
                }

        location ~ [^/]\.ph(p\d*|tml)$ {
            try_files /does_not_exists @php;
        }
    }
    listen 1.1.1.1:80;
    location @php {
        fastcgi_index index.php;
        fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f [email protected]";
        fastcgi_pass unix:/var/www/php-fpm/www-root.sock;
        fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
        try_files $uri =404;
        include fastcgi_params;
    }
    error_page 404 = @routing;
        location @routing {
                rewrite ^/(.+)$ /index.php?_route_=$1 last;
        }

}
server {
    server_name site.com www.site.com;
    ssl_certificate "/var/www/httpd-cert/www-root/site.com_le1.crtca";
    ssl_certificate_key "/var/www/httpd-cert/www-root/site.com_le1.key";
    ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    add_header Strict-Transport-Security "max-age=31536000;";
    ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
    charset UTF-8;
    index index.php index.html;
    disable_symlinks if_not_owner from=$root_path;
    include /etc/nginx/vhosts-includes/*.conf;
    include /etc/nginx/vhosts-resources/site.com/*.conf;
    access_log /var/www/httpd-logs/site.com.access.log;
    error_log /var/www/httpd-logs/site.com.error.log notice;
    ssi on;
    set $root_path /var/www/www-root/data/www/bot;
    root $root_path;

    location / {
                if (!-e $request_filename) {
                        rewrite ^/(.*) /index.php?$query_string;
                }
        }


    location / {
        location ~ [^/]\.ph(p\d*|tml)$ {
            try_files /does_not_exists @php;
        }
    }
    listen 1.1.1.1:443 ssl;
    location @php {
        fastcgi_index index.php;
        fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f [email protected]";
        fastcgi_pass unix:/var/www/php-fpm/www-root.sock;
        fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
        try_files $uri =404;
        include fastcgi_params;
    }
    
    error_page 404 = @routing;
    location @routing {
        rewrite ^/(.+)$ /index.php?_route_=$1 last; 
    }
}


as a result, 404 Not Found nginx/1.18.0

is still written in index.php itself
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
var_dump($_GET);


By typing logs

return 200 "test";

I found a part of the code that is just being executed during this 404 error. These are the last lines

error_page 404 = @routing;
  location @routing {
  ....
}


But so I don't write to the place...., the page still returns 404 and no index.php file is run

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2021-06-08
@dodo512

location / {
    if (!-e $request_filename){
                    rewrite ^/(.*) /index.php?code=$query_string;
            }

    location ~ [^/]\.ph(p\d*|tml)$ {
        try_files /does_not_exists @php;
    }
}

If there is a request to the /dir/ folder, then this fragment needs to be changed to this:
location / {

}

location ~ ^/dir/([0-9A-Za-z]*)$ {
    try_files $uri /dir/index.php?code=$1&$args;
}

And also change to location @php {location \.php$ {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question