nginx with multiple symfony2 apps nginx with multiple symfony2 apps symfony symfony

nginx with multiple symfony2 apps


After many hours of debugging, I finally solved the problem. This is my final configuration :

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;    }}

Thanks you all for your help !


This solved it finally for me (thanks to Claros answer), after million things i tried. Like this, urls like the following work:

/abc/path/to/endpoint

but not /abc/app.php/path/to/endpoint. Config.php and App_dev.php, if in web folder are given back as plain text.

I still try to figure out to get /abc to work (/abc/ works but /abc not). There i get a Symfony exception that route /abc can not be found.

Also some font urls (for bootstrap) are still incorrect but styles, routing etc works.

  location /abc {    set $subpath /abc;    set $sfPath /var/www/abc/current/web;    alias      $sfPath;    try_files $uri @rewrite;  }   location / {    set $subpath "";    set $sfPath /var/www/def/current/web;    alias      $sfPath;    try_files $uri @rewrite;  }   location @rewrite {    rewrite ^(.*)$ $subpath/app.php$1 last;  }   location ~ /app\.php(/|$) {    internal;    include       /etc/nginx/fastcgi_params;    fastcgi_index app.php;    fastcgi_pass  unix:/var/run/php5-fpm.sock;    fastcgi_param DOCUMENT_ROOT  $sfPath;    fastcgi_param SCRIPT_FILENAME $sfPath/app.php;    fastcgi_split_path_info ^(.+\.php)(/.+)$;  }

If you want also the app_dev.php to work in demo environment the best way i found is the following (to have the php block everytime inside the location block):

location /xyz {    set $subpath /xyz;    set $sfPath /var/www/xyz/current/web;    alias      $sfPath;    try_files $uri @rewrite;    #Change the match for app_dev.php to work    location ~ /(app|app_dev|config)\.php(/|$) {      #Drop the internal for App_dev.php to work      #internal;      include       /etc/nginx/fastcgi_params;      fastcgi_index app.php;      fastcgi_pass  unix:/var/run/php5-fpm.sock;      fastcgi_param DOCUMENT_ROOT  $sfPath;      fastcgi_param SCRIPT_FILENAME $sfPath/app.php;      fastcgi_split_path_info ^(.+\.php)(/.+)$;    } } 


Seperate your applications with-in another server tag in your sites-enabled file.

For example:

#Site 1

server { #Configuration}server { #Configuration 2}server { #Configuration 3}

Sample configuration:

server {    listen 80;    root /var/www/yourdomain.com/web;    server_name yourdomain.com www.yourdomain.com;    add_header X-UA-Compatible "IE=Edge,chrome=1";    location ~* \.(css|js|gif|jpe?g|png)$ {        expires 1y;        add_header Pragma public;        add_header Cache-Control "public, must-revalidate, proxy-revalidate";    }    location / {        try_files $uri @rewriteapp;    }    location @rewriteapp {        rewrite ^(.*)$ /app_dev.php/$1 last;    }    location ~ ^/(app|app_dev|config)\.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;    }    error_log /var/log/nginx/yourdomain.com.error.log;    access_log /var/log/nginx/yourdomain.com.access.log;}server {    listen 80;    root /var/www/yourdomain.com/anotherproject/web;    server_name sub1.yourdomain.com www.sub1.yourdomain.com;    add_header X-UA-Compatible "IE=Edge,chrome=1";    location ~* \.(css|js|gif|jpe?g|png)$ {        expires 1y;        add_header Pragma public;        add_header Cache-Control "public, must-revalidate, proxy-revalidate";    }    location / {        try_files $uri @rewriteapp;    }    location @rewriteapp {        rewrite ^(.*)$ /app_dev.php/$1 last;    }    location ~ ^/(app|app_dev|config)\.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;    }    error_log /var/log/nginx/sub1.yourdomain.com.error.log;    access_log /var/log/nginx/sub1.yourdomain.com.access.log;}