symfony - 具有多个 symfony2 应用程序的 nginx

标签 symfony nginx configuration url-rewriting url-routing

我看到很多人在将一台 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/

相关文章:

php - 为什么总是调用 symfony2 安全选民?

symfony - 皮维克 : There are no commands defined in the “generate” namespace?

php - Symfony 2 配置页面没有样式

tomcat - Tomcat 类加载器的顺序 : common, 共享,和服务器

dependency-injection - 如何在 symfony3 中注入(inject)接口(interface)(而不是类)?

docker - nginx 反向代理背后的 Nexus docker 存储库

php - Symfony - Guzzle 请求 : cURL error 6: Could not resolve host

nginx - 在 Nginx 上通过 https 配置 tusd 时出现问题

amazon-web-services - 将弹性IP分配给自动伸缩组使用cloudformation脚本创建的实例

ruby-on-rails - 如何从 Rspec 输出中禁用 http 日志?