nginx - NGINX 重写规则和 try_files 不起作用,为什么?
问题描述
我想要404路由,这里请求site.com/get/123,会被重写为site.com/get/index.php?get=123。当我有 Apache 时,我使用了以下规则:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Za-z]*)$ index.php?code=$1 [L,QSA]
我现在有 php-fpm + nginx。我尝试了很多选择。我的这个域的配置文件:
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 webmaster@site.com";
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 webmaster@site.com";
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;
}
}
我有404 Not Found nginx/1.18.0
通过尝试日志消息
return 200 "test";
发现这段 404 错误期间正在执行的代码。这些是最后几行
error_page 404 = @routing;
location @routing {
....
}
我尝试将 .... 替换为(用于测试)
try_files $uri index.php;
或者
rewrite ^$ index.php redirect;
但没有发生任何变化。当然,我重新启动了 NGINX 和服务器
我不断尝试,几乎成功了。
rewrite ^(.*)$ /get/?code=$request_filename break;
但是我不知道要插入什么而不是 $ request_filename,这样就只有正在发出请求的不存在的文件。
解决方案
推荐阅读
- r - 如何使用实验的重复进行绘图并执行标准。图中重复的偏差?
- excel - 如何将多个搜索添加到单个代码
- flutter - InheritedWidget 在小部件树 Flutter 中将数据设为 null
- php - Mysql:按日期交集选择,而不是之间
- c# - 任意大小的 C# ValueTuple
- javascript - 如何防止通过 JavaScript 访问我的网站
- php - 为什么我没有从数据库中获取 $user_name?
- javascript - RxJS:将对象键映射到可观察对象
- swift - 使用故事板中定义的自定义类
- c# - 在 ASP.NET Core 中使用 except 方法时显示不正确的答案