Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am setting up an nginx server to be used with the Zend Framework. For the most part it works. However, if there is a trailing slash on the end of a URL, for example http://localhost/test/ vs http://localhost/test, the URL with a trailing slash will not work. This is unexpected and unfortunate. I want it to work for both URLs.

What should I do to resolve this? My nginx configuration is:

server {

    listen 80;

    server_name mywebsite.com;
    root /var/www/site/public;

    location / {
        rewrite ^/(.*)/$ /$1;

        include /etc/nginx/fastcgi_params;
        fastcgi_param APPLICATION_ENV development;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/site/public/index.php;
      }

}

As you can see I've tried to add a rewrite condition but it isn't seeming to make a difference.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Here's a pair of config files that I use for Zend Framework applications. First, php.conf, which I share between projects on my development workstation. I keep this at a directory above my sites directory, right in the nginx config root (e.g. /usr/local/etc/nginx/php.conf):

fastcgi_intercept_errors on;
# this will allow Nginx to intercept 4xx/5xx error codes
# Nginx will only intercept if there are error page rules defined
# -- This is better placed in the http {} block as a default
# -- so that in the case of wordpress, you can turn it off specifically
# -- in that virtual host's server block

location ~ \.php$
{
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    fastcgi_param APPLICATION_ENV   development;

    fastcgi_param PATH_INFO         $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED   $document_root$fastcgi_path_info;
    fastcgi_param QUERY_STRING      $query_string;
    fastcgi_param REQUEST_METHOD    $request_method;
    fastcgi_param CONTENT_TYPE      $content_type;
    fastcgi_param CONTENT_LENGTH    $content_length;
    fastcgi_param SCRIPT_NAME       $fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    fastcgi_param REQUEST_URI       $request_uri;
    fastcgi_param DOCUMENT_URI      $document_uri;
    fastcgi_param DOCUMENT_ROOT     $document_root;
    fastcgi_param SERVER_PROTOCOL   $server_protocol;
    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE   nginx;
    fastcgi_param REMOTE_ADDR       $remote_addr;
    fastcgi_param REMOTE_PORT       $remote_port;
    fastcgi_param SERVER_ADDR       $server_addr;
    fastcgi_param SERVER_PORT       $server_port;
    fastcgi_param SERVER_NAME       $server_name;

    fastcgi_read_timeout            60;

    fastcgi_pass                    127.0.0.1:9001;
    fastcgi_index                   index.php;
}

And then the server block that makes use of that:

server
{
    listen 80;
    server_name local.example.com;

    root /var/www/example/public;

    location /
    {
        index index.php;
        try_files $uri $uri/ /index.php$is_args$args;
    }

    include php.conf;
}

In your case, I think the key bit is try_files $uri $uri/ /index.php$is_args$args;, where we just pass the URL and query string wholesale to index.php, and let it do the parsing.

share|improve this answer
1  
There are two important parts here: try_files and fastcgi_split_path_info. –  Michael Hampton May 20 '13 at 22:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.