Zend Framework 不适用于带有尾部斜杠的 URL - 如何配置 nginx 以正确重写 URL?

Zend Framework 不适用于带有尾部斜杠的 URL - 如何配置 nginx 以正确重写 URL?

我正在设置一个nginx用于 Zend Framework 的服务器。在大多数情况下,它可以正常工作。但是,如果 URL 末尾有一个斜杠,例如http://localhost/test/vs http://localhost/test,则带有斜杠的 URL 将不是工作。这是意料之外的,也是不幸的。我希望它对两个 URL 都有效。

我该怎么做才能解决这个问题?我的 nginx 配置是:

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;
      }

}

正如您所看到的,我尝试添加重写条件,但似乎没有什么区别。

答案1

这是我用于 Zend Framework 应用程序的一对配置文件。首先,php.conf我在开发工作站上的项目之间共享它。我将其保存在我的sites目录上方的目录中,就在 nginx 配置根目录中(例如/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;
}

然后server是利用该块:

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;
}

就您而言,我认为关键点是try_files $uri $uri/ /index.php$is_args$args;,我们只需将 URL 和查询字符串批发传递给index.php,然后让它进行解析。

相关内容