如何解决Nginx中的Yii2网址配置
我在apache服务器上创建并开发了yii应用程序,并且所有配置在apache上都运行良好,但是现在我正尝试使用nginx将项目部署到新服务器上,但是只有主页可以访问,我单击ii即可得到404页。未找到。我将以下.htaccess用作我在apache中的重写规则,现在正在寻找在nginx中具有相同功能的东西
RewriteEngine On
# deal with admin first
RewriteCond %{REQUEST_URI} ^/dajubet/(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} ^/dajubet/(admin)
RewriteRule ^.*$ backend/web/index.php [L]
RewriteCond %{REQUEST_URI} ^/dajubet/(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} !^/dajubet/(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','name' => 'dajubetBackend','controllerNamespace' => 'backend\controllers','components' => [
'request' => [
'csrfParam' => '_csrf-backend','web'=> '/backend/web','adminUrl' => '/admin'
],'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend','urlManager' => [
'enablePrettyUrl' => true,'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view','authManager' => [
'class' => 'yii\rbac\DbManager','defaultRoles' => ['users'],];
最后是我的Nginx服务器配置文件
server {
set $project_root /var/www/dajubet.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 dajubet.com www.dajubet.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/dajubet.com.access.log;
error_log /var/log/nginx/dajubet.com.error.log;
}
感谢您提供有关如何解决此问题的帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。