我看到很多人在将一台 nginx 服务器配置为具有多个 symfony2 应用程序时遇到问题。然而,没有人想要同样的东西,也没有和我一样的问题。
我想要做的是在同一个域上有多个应用程序。一个主要应用程序将直接响应域,而其他应用程序将位于别名子目录中。
使用架构:
http://mydomain/ -> main app
http://mydomain/subdir1 -> another app
http://mydomain/subdir2 -> yet another app
我自己尝试过这样做,并且主应用程序运行良好。但是子目录大部分时间都被主app拦截了,抛出404。当我尝试在子目录的URL中添加app.php时(如
http://mydomain/subdir1/app.php/my/route
),服务器返回404。这就是我到目前为止所做的:
server {
listen 80;
server_name mydomain;
root /server/www/main-app/web;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
location /subdir1/ {
alias /server/www/other-app1/web;
# try to serve file directly, fallback to app.php
try_files $uri /server/www/other-app1/web/app.php$is_args$args;
# PROD
location ~ ^/other-app1/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
}
谢谢你的帮助!
编辑 26/12/2014:
对于那些不完全理解我想要什么的人:我想在没有子域的同一个域名上托管多个 symfony2 应用程序。没有子域,我必须使用子目录。在此之前,我尝试了 nginx,我使用了 Apache2,使用 Alias 很容易做到这一点。
我做了更多的搜索,发现“别名”和“try_files”不是好 friend (请参阅此错误报告:http://trac.nginx.org/nginx/ticket/97)。所以我激活了 Debug模式并做了很多测试。
现在我几乎做到了。主应用程序不再拦截子目录,其他应用程序回答。
但是那些其他应用程序的回答是 404,所以我查看了他们的日志。我发现他们在寻找包含子目录的 URL 模式。例如,他们搜索了
/subdir1/login
而不是 /login
.所以这是我的新配置:
server {
listen 80;
server_name mydomain;
root /server/www/main-app/web;
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location /subdir1/ {
set $root "/server/www/other-app1/web";
# try to serve file directly, fallback to app.php
try_files $uri @rewriteapp;
}
location / {
index app.php;
set $root "/server/www/main-app/web";
# try to serve file directly, fallback to app.php
try_files $uri @rewriteapp;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
如您所见,诀窍是不要将 $document_root 用于 SCRIPT_FILENAME,而是我创建了自己的。我不知道 symfony2 路由器如何搜索 URL 中的模式,但是使用我以前的配置(Apache2)我从来没有遇到过这个问题。所以也许他们是另一个将正确路径发送到脚本 app.php 的技巧。
再次感谢您的帮助!
最佳答案
经过几个小时的调试,我终于解决了这个问题。这是我的最终配置:
server {
listen 80;
server_name mydomain;
root /server/www;
location @rewriteMainApp {
rewrite ^(.*)$ /app.php/$1 last;
}
location @rewriteOtherApp1 {
rewrite ^(.*)$ /subdir1/app.php/$1 last;
}
location /subdir1 {
alias /server/www/other-app1/web;
index app.php;
set $subfolder "other-app1/web";
try_files $uri @rewriteOtherApp1;
}
location / {
root /server/www/main-app/web;
index app.php;
set $subfolder "main-app/web";
try_files $uri @rewriteMainApp;
}
# PROD
location ~ /app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$subfolder/app.php;
}
}
谢谢大家的帮助!
关于symfony - 具有多个 symfony2 应用程序的 nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27637369/