【发布时间】:2015-02-22 14:23:10
【问题描述】:
我看到很多人在配置一个 nginx 服务器以拥有多个 symfony2 应用程序时遇到问题。然而,没有人想要和我一样的东西,也有同样的问题。 我想要做的是在同一个域上有多个应用程序。一个主应用程序将直接响应域,而其他应用程序将位于别名子目录中。 使用架构:
http://mydomain/ -> main app
http://mydomain/subdir1 -> another app
http://mydomain/subdir2 -> yet another 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;
}
}
}
感谢您的帮助!
2014 年 12 月 26 日编辑: 对于那些不完全了解我想要什么的人:我想在同一个域名上托管多个 symfony2 应用程序,而无需子域。没有子域,我必须使用子目录。在此之前我尝试过 nginx,我使用的是 Apache2,使用 Alias 很容易做到这一点。
我进行了更多搜索,发现“alias”和“try_files”不是好朋友(请参阅此错误报告:http://trac.nginx.org/nginx/ticket/97)。所以我激活了调试模式并做了很多测试。
现在我几乎做到了。主要应用程序不再拦截子目录,其他应用程序回答。
但是那些其他应用程序的回答是 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 发送正确路径的另一个技巧。
再次感谢您的帮助!
【问题讨论】:
标签: symfony nginx configuration url-rewriting url-routing