我需要在同一主机上但不同的子目录(或位置 block )上安装多个 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才能理解),所以我分享这个。
这个配置对于目录 {subdir} 中的 sf2 来说似乎没问题(并且禁止对 {subdir}/web/* 之外的其他文件进行 Web 访问)。
它适用于我的 php-fpm(套接字)。
当然,将“{subdir}”替换为您的/path/from/docroot/to/symfony_root/
可以通过将“dev”添加到“{subdir}”来选择开发环境(因为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;
}
关于symfony - 如何在 nginx 的子目录中安装 symfony2 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12266297/