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

时间:2015-03-03 16:02:51

标签: nginx zend-framework2

这是我的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's。

非常感谢想法!

2 个答案:

答案 0 :(得分:1)

你可以在ZF2配置中避免很多旧网址,只需使用nginx重定向指令。对任何旧路/path/index.php url:

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

然后使用zf2-way路由(没有index.php)

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

因此,您可以轻松地将旧的/jobs/index.php重定向到nginx(快速和SEO有效)到新/作业并使用ZF2处理它。

答案 1 :(得分:1)

现在我将配置更改为并且它可以工作: - )

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;"那是失踪的。