Yii2 url重写后缀(php)问题

时间:2016-04-28 17:45:17

标签: php .htaccess nginx yii2

我有一些应该在我的网站上运行的网址模式。

[
'pattern'=>'page/result',
'route'=>'site/index',
'suffix'=>'.html'
],
[
'pattern'=>'page/result',
'route'=>'site/index',
'suffix'=>'.php'
],

在这两个url后缀.html工作正常但.php后缀在MY Nginx Server中不起作用。另请查看我的网站nginx设置。

server {


listen 8081 default_server;
    listen [::]:8081 default_server;

    root /var/www/html/yii2project/frontend/web;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html index.php;


    access_log  /var/log/yii2/access.log;
    error_log   /var/log/yii2/error.log;

    server_name yii2.local;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        #try_files $uri $uri/ =404;
           root   /var/www/html/yii2/frontend/web;
           index  index.html index.htm index.php;
           try_files $uri $uri/ /index.php?$args;#now
           #rewrite ^/(.*)$ /$1 last;
           #if ($http_host ~* "^yii2.local:8081"){
           #rewrite ^(.*)$ http://www.yii2.local:8081$1 redirect;
            #}

       }

        #caching of static files
        location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
           expires 365d;
        }


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #
    #   # With php5-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
                fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;               # include fastcgi.conf;
                include fastcgi.conf;#now 
        fastcgi_read_timeout 300;    

    } 
}

http://yii2.local:8081/page/result.html(工作) http://yii2.local:8081/page/result.php(不工作)

请参阅nginx设置并告诉我配置错误。

1 个答案:

答案 0 :(得分:0)

你有三个不同的文档根,看起来有点奇怪:

/var/www/html/yii2project/frontend/web
/var/www/html/yii2/frontend/web
/usr/share/nginx/html

第一个将用于查找资源文件(.css.js)。第二个将用于查找.html个文件。第三个将用于查找.php个文件。

假设您的所有文件共享一个公共根,通常会在root块中放置server指令,并允许所有location块继承该值。

您应该更改第一个root指令的值。删除root块中的location /指令,并更改SCRIPT_FILENAME的定义。

server {
    ...
    root /var/www/html/yii2/frontend/web;
    ...
    location / { ... }
    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ { ... }
    location ~ \.php$ {
        ...
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}