1

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.

1 Answer 1

1

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.

1
  • 1
    There are two important parts here: try_files and fastcgi_split_path_info. May 20, 2013 at 22:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .