[英]Nginx php-fpm 404 error for one request and not another
我在同一台服务器上托管多个php slim应用程序。 它们位于路径apis/'tier'/'organization'/'appName'/'version'
,例如apis/FreeTierSmall/master/exampleApp/v1
。
我将Nginx与php-fpm结合使用,并且遇到了一个非常奇怪的错误。 我正在尝试将以apis/master/
开头的所有请求重定向到apis/FreeTierSmall/master
。 我打开了Nginx rewrite_log,可以看到文件被正确重定向了。 如果尝试apis/FreeTierSmall/master/example/v1
我会得到正确的结果。 但是,如果我尝试将apis/master/example/v1
重定向到相同的php文件,则会收到404错误。 我知道重定向有效,因为我可以在日志中看到它。 似乎php-fpm有问题。 我在php-fpm执行中添加了标头,所以我知道它正在调用正确的脚本。 出于某种原因,尽管在一种情况下,对同一文件的请求会产生404错误,而在另一种情况下却不会。
是否有一些参数可能导致传递给fpm的同一文件在一个实例中起作用而在另一个实例中不起作用?
这是我的nginx配置:
worker_processes 1;
pid /run/nginx.pid;
user nginx www-data;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main_timed '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $upstream_response_time $pipe $upstream_cache_status'
'FPM - $document_root - $fastcgi_script_name > $request';
access_log /var/log/nginx/access.log main_timed;
# error_log /dev/stderr notice;
error_log /var/log/nginx/error.log debug;
# error_log above can be debug
rewrite_log on;
keepalive_timeout 65;
server {
listen [::]:80 default_server;
listen 80 default_server;
server_name _;
sendfile off;
root /var/www/html;
index index.php index.html;
error_page 404 /404.html;
# NOTE: Once you use last, that is the last redirect you can do. You must find the file after that.
# HEALTH CHECK
location /apis/FreeTierSmall/elb-status {
access_log off;
return 200 'A-OK!';
# because default content-type is application/octet-stream,
# browser will offer to "save the file"...
# the next line allows you to see it in the browser so you can test
add_header Content-Type text/plain;
}
# NORMAL API PATHS
location /apis/ {
#rewrite the old apis
rewrite ^/apis/master/([\w-]+)/([\w-]+)(.*)$ /apis/FreeTierSmall/master/$1/$2/api.php$3 last;
rewrite ^/apis/interfaceop/([\w-]+)/([\w-]+)(.*)$ /apis/FreeTierSmall/interfaceop/$1/$2/api.php$3 last;
# add api.php to the path of the file
rewrite ^/apis/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)(.*)$ /apis/$1/$2/$3/$4/api.php$5 last;
}
# ANY OTHER FILES
location / {
# try to serve the file, the directory, or a 404 error
add_header X-debug-message-2 "A static file was served or 404 error $uri" always;
try_files $uri $uri/ /robots.txt; # Need to change back to =404
}
# ERRORS
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/lib/nginx/html;
}
# PHP FILES
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php {
add_header X-debug-message-5 "fastCGI -> .php $document_root$fastcgi_script_name" always;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
# SECURITY CONCERNS
# deny access to . files, for security
location ~ /\. {
log_not_found off;
deny all;
}
}
}
事实证明,问题在于请求的URI不会随重写而更改。 Slim提供了404错误,因为该路由不存在,并且该路由不存在,因为URI从未通过重写而更改。 因此,重写实际上并不会改变请求参数,它只是用来决定要提供哪个文件的。 对于大多数用例来说,这是可以的,但对于api来说却是可怕的。 以后遇到这个的人,祝你好运。
解决方案:使用proxy_pass。
location /apis/master/ {
# Reroutes /apis/master/* to /apis/FreeTierSmall/master/* correctly!
proxy_pass http://localhost:80/apis/FreeTierSmall/master/;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.