1、现阶段的目录结构中有3个应用,分别为:frontend、backend、api,其域名分别配置为
:http://www.channel-pub-api.localhost/ 、http://www.channel-pub-api.localhost/backend 、http://www.channel-pub-api.localhost/api
2、编辑 hosts 文件
# channel-pub-api 127.0.0.1 channel-pub-api.localhost 127.0.0.1 www.channel-pub-api.localhost
3、参考:https://github.com/mickgeek/yii2-advanced-one-domain-config/blob/master/vhosts/nginx.conf ,编辑 /frontend/config/main.php
return [
'components' => [
'request' => [
'baseUrl' => '',
'csrfParam' => '_csrf-frontend',
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
],
];
4、编辑 /backend/config/main.php,设置 baseUrl,分离 Session 和 Cookie
return [
'homeUrl' => '/backend',
'components' => [
'request' => [
'baseUrl' => '/backend',
'csrfParam' => '_csrf-backend',
'csrfCookie' => [
'httpOnly' => true,
'path' => '/backend',
],
],
'user' => [
'identityClass' => 'backend/models/User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_identity-backend',
'path' => '/backend',
'httpOnly' => true
],
],
'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend',
'cookieParams' => [
'path' => '/backend',
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
],
];
5、编辑 /api/config/main.php
return [
'homeUrl' => '/api',
'components' => [
'request' => [
'baseUrl' => '/api',
'csrfParam' => '_csrf-api',
'parsers' => [
'application/json' => 'yii/web/JsonParser',
]
],
'urlManager' => require __DIR__ . '/urlManager.php',
],
];
6、编辑 /api/config/urlManager.php
return [
'class' => yii/web/UrlManager::class,
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
],
];
7、编辑 /environments/dev/frontend/web/robots.txt、/environments/prod/frontend/web/robots.txt
编辑前:
User-agent: * Disallow: /
编辑后:
User-agent: * Disallow: /frontend/web Disallow: /backend/web Disallow: /api/web
8、编辑 channel-pub-api.conf 文件
## FRONTEND ##
server {
charset utf-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
server_name www.channel-pub-api.localhost;
root E:/wwwroot/channel-pub-api;
index index.php;
access_log logs/www.channel-pub-api.localhost.access.log;
error_log logs/www.channel-pub-api.localhost.error.log;
location / {
root E:/wwwroot/channel-pub-api/frontend/web;
try_files $uri $uri/ /frontend/web/index.php$is_args$args;
# omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
#location ~ ^/.+/.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
# log_not_found off;
# access_log off;
# try_files $uri =404;
#}
location ~ ^/assets/.+/.php(/|$) {
deny all;
}
}
location /backend {
alias E:/wwwroot/channel-pub-api/backend/web/;
# redirect to the URL without a trailing slash (uncomment if necessary)
#location = /backend/ {
# return 301 /backend;
#}
# prevent the directory redirect to the URL with a trailing slash
location = /backend {
# if your location is "/backend", try use "/backend/backend/web/index.php$is_args$args"
# bug ticket: https://trac.nginx.org/nginx/ticket/97
try_files $uri /backend/backend/web/index.php$is_args$args;
}
# if your location is "/backend", try use "/backend/backend/web/index.php$is_args$args"
# bug ticket: https://trac.nginx.org/nginx/ticket/97
try_files $uri $uri/ /backend/backend/web/index.php$is_args$args;
# omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
#location ~ ^/backend/.+/.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
# log_not_found off;
# access_log off;
# try_files $uri =404;
#}
location ~ ^/backend/assets/.+/.php(/|$) {
deny all;
}
}
location /api {
alias E:/wwwroot/channel-pub-api/api/web/;
# redirect to the URL without a trailing slash (uncomment if necessary)
#location = /api/ {
# return 301 /api;
#}
# prevent the directory redirect to the URL with a trailing slash
location = /api {
# if your location is "/api", try use "/api/api/web/index.php$is_args$args"
# bug ticket: https://trac.nginx.org/nginx/ticket/97
try_files $uri /api/api/web/index.php$is_args$args;
}
# if your location is "/api", try use "/api/api/web/index.php$is_args$args"
# bug ticket: https://trac.nginx.org/nginx/ticket/97
try_files $uri $uri/ /api/api/web/index.php$is_args$args;
# omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
#location ~ ^/api/.+/.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
# log_not_found off;
# access_log off;
# try_files $uri =404;
#}
location ~ ^/api/assets/.+/.php(/|$) {
deny all;
}
}
location ~ ^/.+/.php(/|$) {
rewrite (?!^/((frontend|backend|api)/web|backend|api))^ /frontend/web$uri break;
rewrite (?!^/backend/web)^/backend(/.+)$ /backend/web$1 break;
rewrite (?!^/api/web)^/api(/.+)$ /api/web$1 break;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $fastcgi_script_name =404;
}
location ~ //. {
deny all;
}
}
## MISC ##
### WWW Redirect ###
server {
listen 80;
server_name channel-pub-api.localhost;
return 301 http://www.channel-pub-api.localhost$request_uri;
}
9、打开网址:http://www.channel-pub-api.localhost/css/site.css ,前端静态资源可以成功访问,如图1
10、打开网址:http://www.channel-pub-api.localhost/site/signup ,前端路由符合预期,成功响应,如图2
11、打开网址:http://www.channel-pub-api.localhost/debug/default/index ,前端路由的 Debug 符合预期,成功响应,如图3
12、打开网址:http://www.channel-pub-api.localhost/backend/css/site.css ,后端静态资源可以成功访问,如图4
13、打开网址:http://www.channel-pub-api.localhost/backend/site/login ,后端路由符合预期,成功响应,如图5
14、打开网址:http://www.channel-pub-api.localhost/backend/debug/default/index ,后端路由的 Debug 符合预期,成功响应,如图6
15、打开网址:http://www.channel-pub-api.localhost/api/code/css/main.css ,接口静态资源可以成功访问,如图7
16、打开网址:http://www.channel-pub-api.localhost/api/v1/users ,接口路由符合预期,成功响应,如图8
17、打开网址:http://www.channel-pub-api.localhost/api/debug/default/index ,接口路由的 Debug 符合预期,成功响应,如图9
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/250429.html
