没有不值得去解决的问题,也没有不值得去学习的技术!

Single-Domain Nginx Configuration for the Yii 2 Advanced Project Template

Open the URL: http://www.channel-pub-api.localhost/backend/site/login , backend routing works as expected and responds successfully

1. The current directory structure contains three applications: frontend, backend, and api. Their domains are configured as follows:
: 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

Plaintext

# channel-pub-api
127.0.0.1 channel-pub-api.localhost
127.0.0.1 www.channel-pub-api.localhost

3. Refer to: https://github.com/mickgeek/yii2-advanced-one-domain-config/blob/master/vhosts/nginx.conf , and edit \frontend\config\main.php

PHP

return [
    'components' => [
        'request' => [
            'baseUrl' => '',
            'csrfParam' => '_csrf-frontend',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],
    ],
];

4. Edit \backend\config\main.php to set the baseUrl and separate Session and Cookie

PHP

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

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

PHP

return [
    'class' => yii\web\UrlManager::class,
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
    ],
];

7. Edit \environments\dev\frontend\web\robots.txt and \environments\prod\frontend\web\robots.txt
Before editing:

Plaintext

User-agent: *
Disallow: /

After editing:

Plaintext

User-agent: *
Disallow: /frontend/web
Disallow: /backend/web
Disallow: /api/web

8. Edit the channel-pub-api.conf file

Nginx

## 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 frontend static resources can be accessed successfully, as shown in Figure 1

Open the URL: http://www.channel-pub-api.localhost/css/site.css , frontend static resources can be accessed successfully
Figure 1

10. Open the URL: http://www.channel-pub-api.localhost/site/signup . The frontend routing works as expected and responds successfully, as shown in Figure 2

Open the URL: http://www.channel-pub-api.localhost/site/signup , frontend routing works as expected and responds successfully
Figure 2

11. Open the URL: http://www.channel-pub-api.localhost/debug/default/index . The Debug module for the frontend routing works as expected and responds successfully, as shown in Figure 3

Open the URL: http://www.channel-pub-api.localhost/debug/default/index , the Debug module for frontend routing works as expected and responds successfully
Figure 3

12. Open the URL: http://www.channel-pub-api.localhost/backend/css/site.css . The backend static resources can be accessed successfully, as shown in Figure 4

Open the URL: http://www.channel-pub-api.localhost/backend/css/site.css , backend static resources can be accessed successfully
Figure 4

13. Open the URL: http://www.channel-pub-api.localhost/backend/site/login . The backend routing works as expected and responds successfully, as shown in Figure 5

Open the URL: http://www.channel-pub-api.localhost/backend/site/login , backend routing works as expected and responds successfully
Figure 5

14. Open the URL: http://www.channel-pub-api.localhost/backend/debug/default/index . The Debug module for the backend routing works as expected and responds successfully, as shown in Figure 6

Open the URL: http://www.channel-pub-api.localhost/backend/debug/default/index , the Debug module for backend routing works as expected and responds successfully
Figure 6

15. Open the URL: http://www.channel-pub-api.localhost/api/code/css/main.css . The API static resources can be accessed successfully, as shown in Figure 7

Open the URL: http://www.channel-pub-api.localhost/api/code/css/main.css , API static resources can be accessed successfully
Figure 7

16. Open the URL: http://www.channel-pub-api.localhost/api/v1/users . The API routing works as expected and responds successfully, as shown in Figure 8

Open the URL: http://www.channel-pub-api.localhost/api/v1/users , API routing works as expected and responds successfully
Figure 8

17. Open the URL: http://www.channel-pub-api.localhost/api/debug/default/index . The Debug module for the API routing works as expected and responds successfully, as shown in Figure 9

http://www.channel-pub-api.localhost/api/debug/default/index
Figure 9

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系