Running symfony on Nginx

A few days ago we published a website built using the symfony php framework.

We had some problems making symfony work correctly on nginx. In particular there were issues with url rewriting: everything worked well for the frontend, but in the backend links to css and images were wrong.

We made a lot of googling and a lot trial and errors. The problem seems well know, but none of the solutions found online worked for us, so I've decided to write down our solution for future reference and for others who might have the same problem.

What worked for us was using two different virtual servers: one for the frontend and one for the backend.

We defined a virtual server named (e.g. www.example.com) with a configuration that looks more or less like this:

 1 server {
 2   listen   80;
 3   server_name  www.example.com;
 4 
 5   access_log  /var/log/nginx/example.com.access.log;
 6   error_log   /var/log/nginx/example.com.error.log warn;
 7 
 8   root        /var/www/example.com/web;
 9   index       index.php;
10   charset utf-8;
11 
12   location / {
13     # If file exists as is static serve it directly
14     if (-f $request_filename) {
15       expires max; 
16       break; 
17     }
18     if ($request_filename !~ "\.(js|ico|gif|jpg|png|css)$") {
19       rewrite ^(.*) /index.php last;
20     }
21   }
22 
23   location ~ \.php($|/) {
24     set  $script     $uri;
25     set  $path_info  "";
26     if ($uri ~ "^(.+\.php)($|/)") {
27       set $script $1;
28     }
29     if ($uri ~ "^(.+\.php)(/.+)") {
30       set  $script     $1;
31       set  $path_info  $2;
32     }
33 
34     fastcgi_pass   127.0.0.1:9000;
35 
36     include /etc/nginx/fastcgi_params;
37 
38     fastcgi_param  SCRIPT_FILENAME  /var/www/example.com/web$script;
39     fastcgi_param  PATH_INFO        $path_info;
40   }
41 }

And another one (e.g. admin.example.com) for the backend. admin.example.com looks exactly the same, except the index.php is replaced with backend.php.

 1 server {
 2   listen   80;
 3   server_name  admin.example.com;
 4 
 5   access_log  /var/log/nginx/example.com.access.log;
 6   error_log   /var/log/nginx/example.com.error.log warn;
 7 
 8   root        /var/www/example.com/web;
 9   index       backend.php;
10   charset utf-8;
11 
12   location / {
13     # If file exists as is static serve it directly
14     if (-f $request_filename) {
15       expires max; 
16       break; 
17     }
18     if ($request_filename !~ "\.(js|ico|gif|jpg|png|css)$") {
19       rewrite ^(.*) /backend.php last;
20     }
21   }
22 
23   location ~ \.php($|/) {
24     set  $script     $uri;
25     set  $path_info  "";
26     if ($uri ~ "^(.+\.php)($|/)") {
27       set $script $1;
28     }
29     if ($uri ~ "^(.+\.php)(/.+)") {
30       set  $script     $1;
31       set  $path_info  $2;
32     }
33 
34     fastcgi_pass   127.0.0.1:9000;
35 
36     include /etc/nginx/fastcgi_params;
37 
38     fastcgi_param  SCRIPT_FILENAME  /var/www/example.com/web$script;
39     fastcgi_param  PATH_INFO        $path_info;
40   }
41 }
blog comments powered by Disqus