nginx and symfony

by Dave Dash 20Jan08

[tags]nginx, server, symfony[/tags]

I almost gave up on nginx and was going to settle on lighttpd or Apache, but I decided to check the symfony list and in minutes Kiril Angov (Kupokomapa) answered with a working nginx configuration. This is why I like the symfony community :)

Here's what works (Note: This requires a running FastCGI server):

server {
  listen       80;
  server_name  mysite.com *;
  root /var/www/mysite.com/web;
  index  index.php;

  charset utf-8;

  location / {
    # If the file exists as a static file serve it directly without
    # running all the other rewite tests on it
    if (-f $request_filename) {
      expires max; 
      break; 
    }

    if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
      rewrite ^(.*) /index.php last;
    }
  }

  location ~ \.php($|/) {
    set  $script     $uri;
    set  $path_info  "";

    if ($uri ~ "^(.+\.php)(/.+)") {
      set  $script     $1;
      set  $path_info  $2;
    }

    fastcgi_pass   127.0.0.1:9000;

    include /etc/nginx/fastcgi_params;

    fastcgi_param  SCRIPT_FILENAME  /var/www/mysite.com/web$script;
    fastcgi_param  PATH_INFO        $path_info;
  }
}

This works in my Ubuntu instance of nginx. Other customziations to this might need to occur, but it's fairly solid so far.


Where am I?

This is a single entry in the weblog.

"nginx and symfony" is filed under symfony. It was published in January 2008.

January 2008
M T W T F S S
« Dec   Feb »
 123456
78910111213
14151617181920
21222324252627
28293031  

Tags

&& &&

need more help

If you found our tutorials and articles to be useful, but are still looking for more hands on help, consider hiring us. Find out more about how Spindrop can help you.

 

11 Responses to “nginx and symfony”


  1. 1 Thierry Posted January 21st, 2008 - 6:06 am

    Hey,

    I was not aware of the nginx project. Why did you choose it above apache or lighttpd?

    Cheers, Thierry

  2. 2 digitalbase Posted January 21st, 2008 - 8:35 am

    i am curious why you would choose this above apache or lighthttpd also.

  3. 3 Dave Dash Posted January 21st, 2008 - 2:05 pm

    Hi Thierry/digitalbase,

    So this configuration is on the new server for reviewsby.us. The old server was an Apache2 server. I had been getting frustrated with Apache2 and to some extent my VPS, so my coworkers recommended slicehost and the slicehosters seem to like nginx and lighttpd.

    Nginx however doesn’t suffer from the memory leaks that plague lighttpd. It also has a very good rewrite system (better than lighty AFAIK) and it’s easy to configure. I’ve also heard that you can just throw stuff at nginx and it will keep serving… I like that :)

    It’s not in production yet, I’m switching the DNS as we speak, so it should be online this week.

    Best,

    -d

  4. 4 Symbiat Posted January 27th, 2008 - 11:46 am

    So what provides the fastcgi process on port 9000 ?

  5. 5 Sasa Posted May 20th, 2008 - 3:13 am

    Hi,

    I’ve installed NginX/FastCGI/Symfony. It works fast :) but I have a small problem with rewriting.

    Accessing e.g. http://localhost:90/frontend_dev.php is OK. Loads everything OK (e.g. path to CSS is: href="/sf/sf_web_debug/css/main.css" and a path to an image is /images/flag_greatbritain.png).

    However, accessing e.g. http://localhost:90/frontend_dev.php/home/index shows correct page content, but screws up the links (e.g. same path to CSS becomes: href="/frontend_dev.php/home/index/sf/sf_web_debug/css/main.css" and the image path becomes: /frontend_dev.php/home/index/images/flag_greatbritain.png).

    At the same time, e.g. http://localhost:90/home/index works fine – shows the page with correct paths.

    I’ve tried tweaking the rewrite rules for NginX, but couldn’t make it work :(

    Here’s my nginx.conf file:

    server {
        listen       90;
        server_name  localhost;
    
        location / {
            root   /var/www/cforum/web;
            index  index.php index.html index.htm;
    
            if (-f $request_filename) {
                expires max;
                break;
            }
    
            if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
               rewrite ^(.*) /index.php last;
            }
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:50005;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/cforum/web$fastcgi_script_name;
            include        fastcgi_params;
        }
    
        location ~ \.php/ {
          #set $script $uri;
          #set $path_info "";
          if ($uri ~ "^(.+\.php)(/.*)") {
            set $script $1;
            set $path_info $2;
          }
    
          include        fastcgi_params;
    
          fastcgi_pass   127.0.0.1:50005;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  /var/www/cforum/web$script;
          fastcgi_param  PATH_INFO $path_info;
        }
    }
    

    Any help appreciated :(

    Regards, Sasa

  6. 6 Tim Posted December 4th, 2009 - 1:32 pm

    I have exactly the same config as you. The only problem is frontenddev.php only shows when u enter /frontenddev.php After you click on a link, it goes to a symfony 404 page. The url is thinking that frontenddev.php is /modulename/ rather than the controller. Im using symfony 1.4 and the latest version of nginx on ubuntu/debian – Any other help?

    Thanks

Who's linking?

  1. 1 Pages tagged "css" Pingback on Jan 20th, 2008
    "[...] bookmarks tagged css nginx and symfony saved by 1 others     YUFKGJH bookmarked on 01/20/08 | [...] "
  2. 2 rpsblog.com » A week of symfony #55 (14->20 january 2007) Pingback on Jan 20th, 2008
    "[...] nginx and symfony [...] "
  3. 3 developercast.com » Dave Dash’s Blog: nginx and symfony Pingback on Jan 21st, 2008
    "[...] Dash has posted a configuration that he had shared with him for getting nginx and the symfony framework to ... "
  4. 4 Dave Dash’s Blog: nginx and symfony | MT-Soft Website Development Pingback on Jan 21st, 2008
    "[...] Blog: nginx and symfony Posted in January 22nd, 2008 by admin in Uncategorized Dave Dash has posted ... "
  5. 5 Symfony.es » Blog Archive » Una semana con Symfony #28 (12-20 enero 2008) Pingback on Feb 3rd, 2008
    "[...] nginx and symfony [...] "

Further Help

If you require more hands on assistance, we do offer affordable hands on support.

Leave a Reply


Comment guidelines: No spamming, no profanity, and no flaming. Inappropriate comments will be deleted outright.