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

在 Yii 2 高级项目模板 上的基于 Nginx 的单域名配置

打开网址:http://www.channel-pub-api.localhost/backend/site/login ,后端路由符合预期,成功响应
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
打开网址:http://www.channel-pub-api.localhost/css/site.css ,前端静态资源可以成功访问
图1
10、打开网址:http://www.channel-pub-api.localhost/site/signup ,前端路由符合预期,成功响应,如图2
打开网址:http://www.channel-pub-api.localhost/site/signup ,前端路由符合预期,成功响应
图2
11、打开网址:http://www.channel-pub-api.localhost/debug/default/index ,前端路由的 Debug 符合预期,成功响应,如图3
打开网址:http://www.channel-pub-api.localhost/debug/default/index ,前端路由的 Debug 符合预期,成功响应
图3
12、打开网址:http://www.channel-pub-api.localhost/backend/css/site.css ,后端静态资源可以成功访问,如图4
打开网址:http://www.channel-pub-api.localhost/backend/css/site.css ,后端静态资源可以成功访问
图4
13、打开网址:http://www.channel-pub-api.localhost/backend/site/login ,后端路由符合预期,成功响应,如图5
打开网址:http://www.channel-pub-api.localhost/backend/site/login ,后端路由符合预期,成功响应
图5
14、打开网址:http://www.channel-pub-api.localhost/backend/debug/default/index ,后端路由的 Debug 符合预期,成功响应,如图6
打开网址: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
打开网址:http://www.channel-pub-api.localhost/api/code/css/main.css ,接口静态资源可以成功访问
图7
16、打开网址:http://www.channel-pub-api.localhost/api/v1/users ,接口路由符合预期,成功响应,如图8
打开网址:http://www.channel-pub-api.localhost/api/v1/users ,接口路由符合预期,成功响应
图8
17、打开网址:http://www.channel-pub-api.localhost/api/debug/default/index ,接口路由的 Debug 符合预期,成功响应,如图9
http://www.channel-pub-api.localhost/api/debug/default/index
图9

PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理