khernik
khernik

Reputation: 2091

Nginx configuration with sf2 routing

I want to configure the nginx server so that I can use app.php or app_dev.php in my links. Right now without them, with just:

www.foobar.com/controller/method/param1/param2

It loads app.php by defaults. Same happens when I add app.php as a prefix, but when I add app_dev.php like that:

www.foobar.com/app_dev.php/controller/method/param1/param2

I get an error that the route could not be found. So it treats app_dev.php like a controller. Why is that so? How can I change it?

The configuration file is:

location /foobar/ {
   index app_dev.php app.php
   if (!-e $request_filename)
   {
      rewrite ^/(.*)$ /foobar/(app|app_dev).php?$1 last;
   }
   try_files $uri $uri/ app.php app_dev.php;
}

Upvotes: 0

Views: 71

Answers (1)

sas
sas

Reputation: 2597

simply use this config below. this is work for both with app_dev.php and without.

location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }
    # DEV
    # This rule should only be placed on your development environment
    # In production, don't include this and don't deploy app_dev.php or config.php
    location ~ ^/(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;
    }
    # 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;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/app.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

Depending on your PHP-FPM config, the fastcgi_pass can also be fastcgi_pass 127.0.0.1:9000.

Upvotes: 1

Related Questions