title: nginx with multiple symfony2 apps
body: <p>I saw that many people had problem configuring one nginx server to have multiple symfony2 applications. However, none wanted the same things and had the same problem as me. 
  What I want to do is to have multiple applications on the same domain. One main application will answer directly to the domain, and the others will be on alias subdirectory. 
  With a schema :</p>
  
  <pre><code>http://mydomain/         -&gt; main app
  http://mydomain/subdir1  -&gt; another app
  http://mydomain/subdir2  -&gt; yet another app
  </code></pre>
  
  <p>I tried by myself to do that and the main app works perfectly. But the subdirectories are most of the time intercepted by the main app, which throws 404. When I try to add app.php in the URL of a subdirectory (like <code>http://mydomain/subdir1/app.php/my/route</code>), the server return 404.</p>
  
  <p>This is what I did until now :</p>
  
  <pre><code>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;
          }
      }
  }
  </code></pre>
  
  <p>Thanks you for your help !</p>
  
  <p><strong>EDIT 26/12/2014 :</strong>
  For those who did not understand exactly what I want : I want to host multiple symfony2 apps on the same domain name without subdomain. Without subdomain, I must use subdirectory. Before that I tried nginx, I used Apache2 and it was easy to do the trick with Alias.</p>
  
  <p>I did more search and found out that "alias" and "try_files" aren't good friends (see this bug report : <a href="http://trac.nginx.org/nginx/ticket/97" rel="noreferrer">http://trac.nginx.org/nginx/ticket/97</a>). So I activated debug mode and did many tests. 
  Now I almost did it. The main apps no longer intercepts subdirectories and the others apps answer. 
  But those others apps answer by 404 so I looked in their logs. And I found out that they looked for URL pattern with the subdirectory in it. For instance they searched <code>/subdir1/login</code> instead of <code>/login</code>.
  So this is my new configuration :</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>As you can see, the trick was to not use $document_root for the SCRIPT_FILENAME and I created my own instead. I don't know how the symfony2 router search the pattern in the URL, but with my previous configuration (Apache2) I never had this problem. So maybe their is another trick to send the correct path to script app.php. </p>
  
  <p>Thanks you again for your help !</p>