nginx|Yii2.0路由管理实现版本控制

由于公司并发上来了,改基础的项目的架构,
由nginx+apache+php改成nginx+php-fpm+php,涉及到项目中的接口版本控制的架构和index.php伪静态需要,进行了重写改变套餐,以下是示例

仅使用nginx作为转发及伪静态index.phpurl重写,实现版本控制
可优化点1.nginx中的@phphtaccess块会携带(v\d)在request,
如果能在这个@phphtaccess块就去掉,就可以去掉urlManager中路由转化了

目录结构示例

image.png

modules参数示例

$version = isset($_GET['v_api'])?$_GET['v_api']:'v0';
return [
    'nice' => [
        'class' => 'app\modules\nice\NiceModule',
    ],
    'app'   => [
        'class' => 'app\modules\\'.$version.'\app\AppModule'
    ],
];

urlManager示例

'urlManager'   => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => false,  //不启用严格解析
            'showScriptName'  => false,
            'rules'           => [
                'GET /app/down.html' => '/mobile/sem/start-down',
                'GET /lei/' => '/mobile/home/category-list',
                '/app//' =>  '/app//',
                '/app/' =>  '/app/',
            ],
        ],

nginx配置示例

server {
        listen       80;
        server_name  100nice.com;
        root   G:\code\project\web;
        index  index.htm index.html index.php;
        
        location / {
            if ($request_method = 'OPTIONS' ) {
                add_header Access-Control-Allow-Origin "*";
                add_header Access-Control-Allow-Methods "POST,GET,PUT,OPTIONS,DELETE";
                add_header Access-Control-Max-Age "3600";
                add_header Access-Control-Allow-Headers "Origin,X-Requested-With,Content-Type,Accept,Authorization,No-Cache,If-Modified-Since,Pragma,Last-Modified,Cache-Control,Expires,token,X-E4M-With";
                add_header Content-Length 0;
                add_header Content-Type text/plain;
                return 200;
            }
            default_type    text/html;
            
            location ~* /(v\d) {
                if ($is_args = "?") {
                    rewrite /(v\d+)/(.*) /$2?$args&v_api=$1 last;
                }
                if ($is_args = '') {
                    rewrite /(v\d+)/(.*) /$2?v_api=$1 last;
                }
            }
            
            #return 200 $uri;
            try_files $uri @phphtaccess;
        }
        
        location @phphtaccess {
            rewrite ^(.*)$ /index.php last;
        }
        
        
        location = /favicon.ico {
            try_files $uri = 204;
            log_not_found off;
            access_log off;
        }
        
        location = /robots.txt {
            allow all;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
    }
  • 也可能是整体的架构有问题,整体逻辑可以改,如有好的架构,可以共同分享!!

你可能感兴趣的:(nginx|Yii2.0路由管理实现版本控制)