Yii2 使用 nginx 重写 URL


yii2 using nginx for rewrite url

我正在尝试使用 Nginx 重写网址以使其更加用户友好。删除 index.php?r= 是成功的,但问题是,在我尝试访问其他页面后,它说 404 未找到。我已经在config/web添加了漂亮的网址管理器,但它不起作用.有人可以帮助我吗?

我会尝试发布代码.

这是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>',
            ),
        ],

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

谢谢。

Nginx config for yii2 Basic:

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

我的快速cgi代码。 - 通常在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>',
    ],

],

server {
  listen 84 default_server;
  listen [::]:84 default_server;
  root /var/www/your_project_dir_name/html;
  # Add index.php to the list if you are using PHP
  index index.html index.htm index.php;
  server_name your_domain_name.com www.your_domain_name.com;
  location / {
    try_files $uri $uri/ /index.php?$args;
  }
  # pass PHP scripts to FastCGI server
  #
  location ~ '.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
  }
  location ~ /'.ht {
    deny all;
  }
}