[英]Nginx send all requests from subdirectory to php-fpm?
我如何将所有转到/api/v1/*ANYTHING*
请求重定向到/api/v1/router.php
? 我当前的nginx配置是:
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /srv/http/default/www;
autoindex on;
# Add index.php to the list if you are using PHP
index index.html index.htm index.php;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
和我尝试过的事情:
location /api/v1 {
try_files FIXME_DO_NOT_MAKE_FILE_WITH_THIS_NAME /api/v1/router.php$is_args$args;
}
/api/v1/get/123
可以工作..但是,如果网址以.php结尾,则无法正常工作,例如/api/v1/get/123.php
不会被重定向,从而导致如果get / 123.php不存在,则找不到404。 (是的,假人显然是强制性的,有1个参数我得到一个错误,即try_files至少需要2个参数,而try_files /api/v1/router.php =500;
它只是打印php源代码而不是发送到php-fpm) 也尝试过:
location /api/v1 {
rewrite ^/api/v1/(.*)$ /api/v1/router.php last;
}
..这似乎遭受与上述完全相同的问题,只要url不以.php
就可以了,否则我得到404的...我想这与location ~ \\.php$
块优先,但是我将重写规则放在.php $块之上还是之下都没有关系...所以现在我有点困惑
(也不确定这个问题是否属于SO或serverfault或超级用户,但是鉴于类似的问题,例如Nginx将所有请求从子目录重定向到 SO上存在的另一个子目录根 ,我决定尝试在这里进行)
最直接的解决方案是定义一个“ regex”位置,并直接指定将用于处理所有请求的脚本名称,如下所示:
location ~ ^/api/v1/ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/api/v1/router.php;
}
确保将其放在location ~ \\.php$ {
上方 ,因为NGINX会根据配置文件中出现的顺序检查正则表达式的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.