如何在 Zend 2 和 nginx 中路由 "domain/jobs/index.php"?

Howto route in Zend 2 and nginx a route "domain/jobs/index.php"?

这是我的 nginx 配置:

server {
    listen       80;
    server_name  mtg.v4.anwalt.de;
    error_log /var/log/nginx/mtg/error.log warn;

    root /opt/mtg/platform/public;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        fastcgi_param APPLICATION_ENV development;
        include fastcgi_params;
        }
}

这是我正在使用的路由配置:

'router' => [
        'routes' => [
            'jobs-home' => [
                'type' => 'literal',
                'options' => [
                    'route' => '/jobs/index.php',
                    'defaults' => [
                        'controller' => 'jobs-controller-index',
                        'action' => 'index',
                    ],
                ],
                'may_terminate' => true
            ]
        ]
    ]

我得到的结果是:

A 404 error occurred
Page not found.

The requested URL could not be matched by routing.

只有当我的路线中有 "index.php" 时,我才会遇到这个问题。这是一个非常严重的问题,因为我们必须支持旧的 Url。

非常感谢您的想法!

你可以在 ZF2 配置中避免很多旧的 urls,只需使用 nginx 重定向指令。尝试使用任何旧方法 /path/index.php url:

location ~ ^/(.*)/index.php$ {    
    return 301 /;    
}

然后使用 zf2-way 路由 (w/o index.php)

'router' => [
        'routes' => [
            'jobs-home' => [
                'type' => 'literal',
                'options' => [
                    'route' => '/jobs',
                    'defaults' => [
                        'controller' => 'jobs-controller-index',
                        'action' => 'index',
                    ],
                ],
                'may_terminate' => true
            ]
        ]
    ]

因此您可以通过 nginx(快速且 SEO 有效)轻松将旧的 /jobs/index.php 重定向到新的 /jobs 并使用 ZF2 进行处理。

现在我将配置更改为并且它有效:-)

server {
    listen       80;
    server_name  mtg.v4.anwalt.de;
    access_log /var/log/nginx/mtg/access.log main;
    error_log /var/log/nginx/mtg/error.log warn;

    root /opt/mtg/platform/public;
    index index.php;

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

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_param SCRIPT_NAME /index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        fastcgi_param APPLICATION_ENV development;
        }


}

问题是这个条目:"fastcgi_param SCRIPT_NAME /index.php;" 丢失了。