一次NGINX rewrite重定向403错误排除

516 阅读1分钟

一次NGINX rewrite重定向403错误排除 以下2个网址,第一个(没有.php字样)的正常,第二个会显示403错误,但返回内容 api.cn/type/item_g…? api.cn/type/api_ca…

与第一个一致,由于部分客户端会根据HTTP状态判断网络请求是否成功,于是必须修复。 经过排查,凡是URL中出现.php即会报错(?符号后不影响) 尝试删除nginx/conf/fcgi.conf中的403输出,但又会输出404 最后检查到问题出在try_files语句上,可能是它将type/api_call.php传给了/index.php?type/api_call.php 导致fcgi.conf解析PHP脚本路径时判断错误 于是在location / 上面添加一个REWRETE语句解决 rewrite ^/(.*)api_call.php/index.php? /index.php?1 last;

相关配置文件 nginx/conf/vhost/api.conf server { listen 80; root /www/web/api/public_html; server_name api.cn; index index.html index.php index.htm; error_page 400 /errpage/400.html; error_page 403 /errpage/403.html; error_page 404 /errpage/404.html; error_page 503 /errpage/503.html;

#最后添加的代码解决403问题 #rewrite ^/(.*)api_call.php/index.php? /index.php?1 last;

location / { try_files uriuri uri/ /?args; } location ~ \.php(.*) { fastcgi_pass unix:/tmp/php-70-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME DOCUMENTROOTDOCUMENT_ROOTfastcgi_script_name; fastcgi_param PATH_INFO $2; include fcgi.conf; } location ~ /.ht { deny all; } }

nginx/conf/fcgi.conf

if (request_filename ~* (.*)\.php) { set php_url 1; } if (!-e php_url.php) { return 403; }