Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx with multiple symfony2 apps

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 :

http://mydomain/         -> main app
http://mydomain/subdir1  -> another app
http://mydomain/subdir2  -> yet another app

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 http://mydomain/subdir1/app.php/my/route), the server return 404.

This is what I did until now :

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

Thanks you for your help !

EDIT 26/12/2014 : 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.

I did more search and found out that "alias" and "try_files" aren't good friends (see this bug report : http://trac.nginx.org/nginx/ticket/97). 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 /subdir1/login instead of /login. So this is my new configuration :

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

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.

Thanks you again for your help !

like image 249
Claros Avatar asked Dec 24 '14 13:12

Claros


3 Answers

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)(/.+)$;
    } 
} 
like image 172
Nilz11 Avatar answered Nov 02 '22 23:11

Nilz11


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 !

like image 21
Claros Avatar answered Nov 02 '22 23:11

Claros


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;
}
like image 42
Canser Yanbakan Avatar answered Nov 03 '22 01:11

Canser Yanbakan