首页 > 解决方案 > nginx中的Yii2 Url配置

问题描述

我在 apache 服务器上创建和开发了我的 yii 应用程序,所有配置在 apache 上运行良好,但现在我试图将我的项目部署到一个带有 nginx 的新服务器,但只有主页可以访问任何其他链接,我单击 ii 得到 404 页面未找到。我使用下面的 .htaccess 作为我在 apache 中的重写规则,现在正在寻找在 nginx 中做同样的事情

RewriteEngine On

# deal with admin first
RewriteCond %{REQUEST_URI} ^/myweb/(admin) 
RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [L]
RewriteRule ^admin/js/(.*)$ backend/web/js/$1 [L]
RewriteRule ^admin/images/(.*)$ backend/web/images/$1 [L]
RewriteRule ^admin/img/(.*)$ backend/web/img/$1 [L]
RewriteRule ^admin/plugins/(.*)$ backend/web/plugins/$1 [L]
RewriteRule ^admin/uploads/(.*)$ backend/web/uploads/$1 [L]


RewriteCond %{REQUEST_URI} ^/myweb/(admin)  
RewriteRule ^.*$ backend/web/index.php [L]



RewriteCond %{REQUEST_URI} ^/myweb/(assets|css)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
RewriteRule ^plugins/(.*)$ frontend/web/plugins/$1 [L]
RewriteRule ^imgages/(.*)$ frontend/web/images/$1 [L]
RewriteRule ^img/(.*)$ frontend/web/img/$1 [L]
RewriteRule ^item_img/(.*)$ frontend/web/item_img/$1 [L]
RewriteRule ^uploads/(.*)$ frontend/web/uploads/$1 [L]
RewriteRule ^itemImg/(.*)$ frontend/web/itemImg/$1 [L]


RewriteCond %{REQUEST_URI} !^/myweb/(frontend|backend)/themes/
RewriteCond %{REQUEST_URI} !^/dajubet/(frontend|backend)/web/(uploads|img|item_img|itemImg)/
RewriteCond %{REQUEST_URI} !^/dajubet/(frontend|backend)/web/(assets|css|js|plugins)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php

我的前端和后端配置文件分别如下

frontend config file
return [
'id' => 'app-frontend',
//'language' => 'zh-CN',
'name' => 'dajubetFrontend',
'basePath' => dirname(__DIR__),
'defaultRoute'=>'home/index',
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',

'components' => [
    'request' => [
        'csrfParam' => '_csrf-frontend',
        'class' => 'common\components\Request',
        'web'=> '/frontend/web',
    ],
    
    'session' => [
        // this is the name of the session cookie used for login on the frontend
        'name' => 'advanced-frontend',
    ],
    'log' => [
        'traceLevel' => YII_DEBUG ? 3 : 0,
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
                'levels' => ['error', 'warning'],
            ],
        ],
    ],
    'errorHandler' => [
        'errorAction' => 'home/error',
    ],

    
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
                        '<controller:\w+>/<id:\d+>' => '<controller>/view',
                        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                        '<members:\w+><payment:\w+>/<test:[^\/]*>/*' => '<controller>/view'
                        //'<members:\w+>/<payment:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>/views',
                        //'<members:\w+><payment:\w+>/<test:[^\/]*>/*' => '<controller>/view'
                        
        ],
    ],
        
],

'as beforeRequest' => [
    'class' => 'common\components\LanguageHandler',
],

'params' => $params,
];


backend config file
return [
'id' => 'app-backend',
//'language' => 'zh-CN',
'name' => 'dajubetBackend',
'defaultRoute'=>'home/index',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],

'components' => [
    'request' => [
        'csrfParam' => '_csrf-backend',
        'class' => 'common\components\Request',
        'web'=> '/backend/web',
        'adminUrl' => '/admin'
    ],
  
    'session' => [
        // this is the name of the session cookie used for login on the backend
        'name' => 'advanced-backend',
    ],
    'log' => [
        'traceLevel' => YII_DEBUG ? 3 : 0,
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
                'levels' => ['error', 'warning'],
            ],
        ],
    ],
    'errorHandler' => [
        'errorAction' => 'home/error',
    ],
    'urlManager' => [
         'enablePrettyUrl' => true,
         'showScriptName' => false,
         'rules' => [
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 
         ],
   ],
   

   'authManager' => [
        'class' => 'yii\rbac\DbManager',
        'defaultRoles' => ['users'],
    ],
    


],



 'as beforeRequest' => [
    'class' => 'common\components\LanguageHandler',
],
'params' => $params,
];

最后是我的 nginx 服务器配置文件

server {

    set $project_root /var/www/myweb.com/html/dajubet;
    set $fcgi_server unix:/run/php-fpm/www.sock;

    charset utf-8;
    client_max_body_size 128M;

    listen 80;
    listen [::]:80;

    root $project_root/frontend/web;
    index index.php index.htmli index.htm index.nginx-debian.html;

    server_name myweb.com  www.myweb.com;

     location ^~ /admin {
             rewrite ^/backend(.*)$ /backend/web$1 last;
     }

      location ^~ /backend/web {
              root $project_root;

              # uncomment the following, if you want to enable speaking URL in the backend
              try_files $uri $uri/ /backend/web/index.php$is_args$args;

              location ~ /\.(ht|svn|git) {
                     deny all;
              }

             location ~ \.php$ {
                    try_files $uri =404;
                        try_files $uri =404;
                    include fastcgi_params;
                    fastcgi_pass $fcgi_server;
             }
     }

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

      location ~ /\.(ht|svn|git) {
            deny all;
     }

    location ~* \.php$ {
            fastcgi_pass unix:/run/php-fpm/www.sock;
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }

    location / {
            try_files $uri $uri/ =404;
    }

    access_log /var/log/nginx/myweb.com.access.log;
    error_log /var/log/nginx/myweb.com.error.log;

 }

任何有关如何解决此问题的帮助将不胜感激,谢谢你们

标签: nginxyii2

解决方案


推荐阅读