我需要在同一主机上但在不同的子目录(或位置块)上安装多个symfony2应用程序.
使用此配置,Nginx在尝试访问任何URL时抛出“找不到文件”或重定向循环消息.
例:
/login -> /base/login
/app1 -> /base/app1
/app2 -> /base/app2
当前配置:
root /base/default; #Points to an empty directory
# Login Application
location ^~ /login {
alias /base/login/web;
try_files $uri app_dev.PHP;
}
# Anything else
location ~ ^/([\w\-]+) {
alias /base/$1/web;
try_files $uri app_dev.PHP;
}
location / {
# Redirect to the login
rewrite ^ /login redirect;
}
# Handle PHP
location ~ \.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 HTTPS off;
fastcgi_pass unix:/var/run/PHP5-fpm.sock;
}
最佳答案
花了几个小时才发现这个(sf2 doc没有解释如何需要和解释cgi参数,你需要通过Request.PHP来理解),所以我分享了这个.
原文链接:https://www.f2er.com/nginx/434395.html这是一个配置,对于目录{subdir}中的sf2似乎没问题(并且禁止访问除{subdir} / web / *之外的其他文件).
它适用于PHP-fpm(socket).
当然,将/ {subdir}替换为/ path / from / docroot / to / symfony_root /
可以通过将“dev”添加到“{subdir}”来选择dev environnement(因为url中的app_dev.PHP不再适用于此conf)
server {
# general directives
location ~ ^/{subdir}(/.*)${
try_files /{subdir}/web$1 @sf2;
}
location ~ ^/{subdir}dev(/.*)${
expires off;
try_files /{subdir}/web$1 @sf2dev;
}
location @sf2 {
expires off;
fastcgi_pass {your backend};
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/{subdir}/web/app.PHP;
fastcgi_param SCRIPT_NAME /{subdir}/app.PHP;
fastcgi_param REQUEST_URI /{subdir}$1;
}
location @sf2dev {
expires off;
fastcgi_pass {your backend};
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/{subdir}/web/app_dev.PHP;
fastcgi_param SCRIPT_NAME /{subdir}dev/app_dev.PHP;
fastcgi_param REQUEST_URI /{subdir}dev$1;
}
# other location directives
# if some others apps needs PHP,put your usual location to cactch PHP here
}
我希望它有帮助(并且没有任何错误配置),没有任何保证……
当然,如果你不需要,你可以选择prod / dev conf.你可以使用var而只使用一个@ sf2位置:
set $sf2_root /{subdir};
location ~ ^/{subdir}(/.*)${
set $sf2_prefix /{subdir};
set $sf2_ctrl app.PHP;
try_files $sf2_root/web$1 @sf2;
}
location ~ ^/{subdir}dev(/.*)${
set $sf2_prefix /{subdir}dev;
set $sf2_ctrl app_dev.PHP;
expires off;
try_files $sf2_root/web$1 @sf2;
}
location @sf2 {
expires off;
fastcgi_pass {your backend};
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$sf2_root/web/$sf2_ctrl;
fastcgi_param SCRIPT_NAME $sf2_prefix/$sf2_ctrl;
fastcgi_param REQUEST_URI $sf2_prefix$1;
}