Having multiple Symfony2 apps on same domain

Claros nginx-forum at nginx.us
Sat Dec 27 10:52:32 UTC 2014


Hello everybody !

I just switched from Apache2 to Nginx and I met some issues having the same
configuration. What I want to do is having multiple Symfony2 apps on the
same domain name. Each app will have a subdirectory and a main app will be
on the domain name itself. For instance :
http://mydomain/               -> main app
http://mydomain/subdir1   -> another app
http://mydomain/subdir2   -> yet another app
One of Symfony2 feature is to have only three php files to be executed, and
all the URL are rewritten to those files. You can found basic configuration
for Symfony2 at this address if you need more information :
http://wiki.nginx.org/Symfony
Now after many hours of configuration, with the help of debug logs, I almost
did it. This is my current configuration :

server {
    listen   80;
    server_name mydomain;
    root /server/www/main-app/web;

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

    location /subdir1/ {
        # alias /server/www/other-app1/web;
        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;
    }
}

Why did I create a variable "$root" ? Because when I was using the root (or
alias) directive in a location block and the variable $document_root, I
found out that this variable has as final value (in the location app.php)
the first root directive in the server or the default root location. 
With this configuration, it almost work. The main app works and the
subdirectories are correctly sent to their directory. The last problem is
that the URI processed by the file app.php also contains the subdirectory in
it, so the others apps send 404 for all the URL. I tried to fix that by
changing "REQUEST_URI" parameter, but with that the app.php generate wrong
URL without the subdirectory.

So is their a way to achieve this configuration ? Thanks you !

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,255806,255806#msg-255806



More information about the nginx mailing list