Answer the question
In order to leave comments, you need to log in
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]
if (!-e $request_filename){
rewrite ^/(.*) /index.php?code=$query_string;
}
error_page 404 = @routing;
location @routing {
rewrite ^/(.+)$ /index.php?_route_=$1 last;
}
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;
}
}
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
var_dump($_GET);
return 200 "test";
error_page 404 = @routing;
location @routing {
....
}
Answer the question
In order to leave comments, you need to log in
location / {
if (!-e $request_filename){
rewrite ^/(.*) /index.php?code=$query_string;
}
location ~ [^/]\.ph(p\d*|tml)$ {
try_files /does_not_exists @php;
}
}
location / {
}
location ~ ^/dir/([0-9A-Za-z]*)$ {
try_files $uri /dir/index.php?code=$1&$args;
}
location @php {
location \.php$ {
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question