【问题标题】:yii2 using nginx for rewrite urlyii2 使用 nginx 重写 url
【发布时间】:2016-02-22 07:10:27
【问题描述】:

我正在尝试使用 Nginx 将 url 重写为更加用户友好。删除 index.php?r= 是成功的,但问题是,在我尝试访问其他页面后,它显示 404 Not Found。我已经在config/web 中添加了 urlmanager 以获得漂亮的 url,但它不起作用。有人可以帮我弄这个吗?

我会尝试发布代码。

这是 nginx.conf

server {
        listen       88;
        server_name  localhost;


        location / {
            root   html;
            index index.php index.html index.htm;
            rewrite ^(.*[^/])$ $1/ permanent;
            try_files $uri $uri/ /index.php?r=$args;
        }

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

        location ~ .php$ {
            include        fastcgi_params;
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include fastcgi.conf;
        }
    }

这是网址管理器。

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            // Disable index.php
            'showScriptName' => false,
            // Disable r= routes
            'enablePrettyUrl' => true,
            'rules' => array(
                    '<controller:\w+>/<id:\d+>' => '<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),
        ],

我首先在本地主机上尝试了这个。

谢谢。

【问题讨论】:

  • 我认为你必须将根路径作为 root /var/www/html/yii-app/web; 而不是 html;
  • @Selvakumar 当我按照您的建议将根 html 从 html 更改为 /html/yii-app/web 时,这是我在 error.log 中遇到的错误。2016/02/23 10:13:53 [error] 3116#1344: *2 "C:/html/MAPUser/web/MAPUser/web/index.php" is not found (3: The system cannot find the path specified) 我将我的 nginx 放在 C 中:

标签: php nginx yii2


【解决方案1】:

yii2 Basic 的 Nginx 配置:

server {
    server_name localhost;
    root /path/to/localhost/yii2basic/web;
    index index.php;

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

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi.conf;
    }
}

My fastcgi code. - 通常在 nginx 配置文件夹中。

Yii 配置文件:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        '/' => 'site/index',
        '<controller:\w+/?>' => '<controller>/index',
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
    ],

],

【讨论】:

  • 如果我们使用 php-fpm 会怎样?我们还使用 fastcgi.conf 文件吗?
猜你喜欢
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 2016-02-07
  • 2015-03-19
  • 2015-12-20
  • 2015-03-08
  • 1970-01-01
相关资源
最近更新 更多