我在同一台服务器上托管多个php slim应用程序。它们位于路径apis/'tier'/'organization'/'appName'/'version'
,例如apis/FreeTierSmall/master/exampleApp/v1
。
我正在使用带有php-fpm的Nginx,我得到了一个非常奇怪的错误。我正在尝试将以apis/master/
开头的所有请求重定向到apis/FreeTierSmall/master
。我打开了Nginx rewrite_log,可以看到文件被正确重定向。如果我尝试apis/FreeTierSmall/master/example/v1
,我会得到正确的结果。但是,如果我尝试重定向到同一个php文件的apis/master/example/v1
,我会收到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;
}
}
}
答案 0 :(得分:0)
事实证明,问题是请求的URI不会随着重写而改变。 Slim提供404错误,因为路由不存在,并且路由不存在,因为URI从未通过重写而改变。因此,重写实际上并没有改变它刚刚用来决定要提供什么文件的请求参数。这对于大多数用例来说都没问题,但对于apis来说很糟糕...一个非常奇怪的错误调试。祝所有人在将来遇到这种情况好运。
解决方案:使用proxy_pass。
schema