Nginx-based single domain name configuration on Yii 2 Advanced Project Template
1. There are 3 applications in the directory structure at this stage, namely: Frontend, Backend, and API, and their domain names are configured as
:http://www.channel-pub-api.localhost/,http://www.channel-pub-api.localhost/backend,http://www.channel-pub-api.localhost/api
2. Edit the hosts file
# channel-pub-api
127.0.0.1 channel-pub-api.localhost
127.0.0.1 www.channel-pub-api.localhost
3. Reference:https://github.com/mickgeek/yii2-advanced-one-domain-config/blob/master/vhosts/nginx.conf, edit \frontend\config\main.php
return [
'components' => [
'request' => [
'baseUrl' => '',
'csrfParam' => '_csrf-frontend',
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
],
];
4. Edit \backend\config\main.php, set the baseURL, separate session and 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. Edit \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. Edit \api\config\urlmanager.php
return [
'class' => yii\web\UrlManager::class,
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
],
];
7. Edit \environments\dev\frontend\web\robots.txt, \environments\prod\frontend\web\robots.txt
Before editing:
User-agent: *
Disallow: /
After editing:
User-agent: *
Disallow: /frontend/web
Disallow: /backend/web
Disallow: /api/web
8. Edit the channel-pub-api.conf file
## 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. Open the URL:http://www.channel-pub-api.localhost/css/site.css, the front-end static resources can be successfully accessed, as shown in Figure 1
10. Open the URL:http://www.channel-pub-api.localhost/site/signup, the front-end route is in line with expected, and the response is successful, as shown in Figure 2
11. Open the URL:http://www.channel-pub-api.localhost/debug/default/index, the debug of the front-end route is in line with expected, and the response is successful, as shown in Figure 3
12. Open the URL:http://www.channel-pub-api.localhost/backend/css/site.css, the back-end static resources can be successfully accessed, as shown in Figure 4
13. Open the URL:http://www.channel-pub-api.localhost/backend/site/login, the back-end routing is as expected, and the response is successful, as shown in Figure 5
14. Open the URL:http://www.channel-pub-api.localhost/backend/debug/default/index, the debug of the backend route is in line with the expected, and the response is successful, as shown in Figure 6
15. Open the URL:http://www.channel-pub-api.localhost/api/code/css/main.css, the interface static resources can be successfully accessed, as shown in Figure 7
16. Open the URL:http://www.channel-pub-api.localhost/api/v1/users, the interface route is in line with the expected, and the response is successful, as shown in Figure 8
17. Open the URL:http://www.channel-pub-api.localhost/api/debug/default/index, the debug of the interface routing is in line with the expected, and the response is successful, as shown in Figure 9








