我是 nginx 及其重写命令的新手,我真的需要一些帮助。我整天都在尝试解决这个问题,但一无所获。
如果用户提供此 URL:
someurl.com/sub/1.0/healthcheck
我想重写它以指向此处的 Symfonys 项目文件:
/var/www/sub/1.0/web/app_dev.php
并且 URL 中的“healthcheck”适用于 Symfony。
但不行。这里有些不对劲。它似乎找到了 Symfony,但是 URL 有问题,因为它总是返回:
未找到路线
即使我从 url 中省略“healthcheck”,它仍然会返回相同的错误。(有一个以“/”作为路由的 index -action。)
这是当前的 Nginx 配置:
server {
server_name localhost;
root /var/www/sub/1.0/web;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location / {
root /var/www/html/;
index index.html;
}
location /sub/1.0/ {
index app_dev.php;
rewrite ^/sub/1.0/ /app_dev.php last;
}
location ~ (app|app_dev).php {
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
答案1
我搞清楚了这一点。问题在于 Symfony 采用了请求 uri,就 fastcgi 而言,它不受重写的影响。我添加了,fastcgi_param REQUEST_URI $uri?$args;
然后就好了!成功了!
这是固定配置(没有多余的行,例如根位置“/”):
server {
root /var/www/sub/1.0/web;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
# If user writes the app_xxx.php into the url, remove it:
rewrite ^/app_dev\.php/?(.*)$ /$1 permanent;
location /sub/1.0/ {
index app_dev.php;
rewrite ^/sub/1.0/(.*)$ /app_dev.php/$1 last;
return 403; # If the rewrite was not succesfull, return error.
}
location ~ (app|app_dev).php {
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param REQUEST_URI $uri?$args;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}