国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

多子域名路由加上強制路由,無法使用

wybwsk

問題描述

前后端分離的TP項目,想轉webman,基礎的路由實現(xiàn)不了,不知道是不是用法錯了。

"workerman/webman-framework": "^1.4.3",
PHP:8.0
OS: Ubuntu 20....

子域名配合nginx加上默認路由OK,http://wtbis.cn/q/7922
但是想強制自定義的類路由就沒辦法實現(xiàn)。因為我們原來的項目是使用強制路由,就是沒定義路由的方法,寫了也不讓訪問。
目錄結構
app
|--api
|----controller
|------v1
|--------IndexController.php

綁定關系
/config/plugin/webman/domain/app.php

'bind'   => [
    'api.test.com'      => 'api', // 綁定到blog應用
],

使用默認路由
/config/route.php

use Webman\Route;
Route::fallback(function () {
    return response(json_encode(['code' => 404, 'msg' => 'Api Fail']), 404, ['Content-Type' => 'application/json']);
});

訪問http://api.test.com/v1/Index/index.正常.

使用自定義路由 + 默認路由

use Webman\Route;

Route::group('/v1', function () {
    Route::get('/Index/index', [app\api\controller\v1\IndexController::class, 'index']);
});

Route::fallback(function () {
    return response(json_encode(['code' => 404, 'msg' => 'Api Fail']), 404, ['Content-Type' => 'application/json']);
});

訪問http://api.test.com/v1/Index/index.正常,不沖突,或者是沒有到這里.

使用自定義路由 + 關閉默認路由

use Webman\Route;

Route::group('/v1', function () {
    Route::get('/Index/index', [app\api\controller\v1\IndexController::class, 'index']);
});

Route::fallback(function () {
    return response(json_encode(['code' => 404, 'msg' => 'Api Fail']), 404, ['Content-Type' => 'application/json']);
});
Route::disableDefaultRoute();

訪問http://api.test.com/v1/Index/index.異常,404.

使用自定義路由 + 關閉默認路由(去掉分組,用基礎的類路由)

use Webman\Route;
Route::get('/v1/Index/index', [app\api\controller\v1\IndexController::class, 'index']);

Route::fallback(function () {
    return response(json_encode(['code' => 404, 'msg' => 'Api Fail']), 404, ['Content-Type' => 'application/json']);
});
Route::disableDefaultRoute();

訪問http://api.test.com/v1/Index/index.異常,404.

另外,多應用的能否使用項目路由?因為應用多了的話,全寫在route.php里面,文件太多了,要改的時候難找。

為此你搜索到了哪些方案及不適用的原因

搜索了多應用+多域名的解決方案,路由失效的解決方案。
這里寫搜到的方案及不適用原因
單獨的都有,合在一起的沒找到對應的解決方案。

2126 2 2
2個回答

yongdao35

不發(fā)下nginx配置?

  • wybwsk 2022-12-31

    nginx配置和那文件一模一樣的。。http://wtbis.cn/q/7922

    根據(jù)域名重寫url

    if ($host = 'api.test.com') {
    rewrite ^/(.*)$ /api/$1 last;
    }

  • yongdao35 2022-12-31

    這個nginx配置的含義是自動給url的path加一個前綴api。比如你訪問http://api.test.com/v1/Index/index,nginx會自動將地址變?yōu)閔ttp://api.test.com/api/v1/Index/index并轉發(fā)給webman。但是你webman關閉了自動路由,也沒有給/api/v1/Index/index設置路由,所以404了。我覺得你把

    if ($host = 'api.test.com') {
       rewrite ^/(.*)$ /api/$1 last;
    }

    這個配置刪除了就好了

  • yongdao35 2022-12-31

    nginx配置刪除后記得重啟nginx

  • wybwsk 2023-01-01

    對的,可以了。api.test.com/abc api.test.com/test 能夠用到域名綁定了
    在路由外面多加一層route::group('/api')就可以了。
    但是新問題出來了。

    Route::group('/api', function () {
        Route::group('/v1', function () {
            Route::get('/test', [app\api\controller\v1\IndexController::class, 'index']);
        });
    });
    //以上代碼正常。
    
    Route::group('/api', function () {
        Route::group('/v1', function () {
            Route::get('/index/index', [app\api\controller\v1\IndexController::class, 'index']);
        });
    });
    //這個和默認路由是有沖突的,所以這個就直接報404了。

    這塊不知道老大的路由思路是什么,開始還以為,Route::disableDefaultRoute()應該是個總開關。。true,就將所有的默認解析全關了,全用用戶自己的,一個都沒有定義就全部404。。
    false,就混和,默認的先定義,用戶自定義的后定義,那么就是后定義的覆蓋以前定義的進行處理,最終key是唯一的。
    后面發(fā)現(xiàn)這個Route::disableDefaultRoute()是需要放在路由配置最后面,可能是默認和自定義的在兩個地方?還需要多看看源碼。

    感謝大佬,問題解決了。。

  • keytehu 2023-01-01

    我看過源碼,默認路由不會和你的路由有沖突,如果404,說明你地址訪問錯了,或者對應的路由確實沒有設置。
    根據(jù)你的上下文來看,估計是訪問的地址的錯了,你給/api/v1/index/index(注意都是小寫) 設置了路由,但是可能你去訪問/api/v1/Index/index了(Index大寫)

  • wybwsk 2023-01-03

    用tp習慣了,訪問的時候有注意這個大小寫。api/v1/index/index和api/v1/Index/index,這都是自己去定義的訪問路徑。。然后兩種都試過,都是404.
    反正就是用上了域名插件,然后使用多應用,再自定義類路由的時候,關閉默認路由,自定義的路由就不能和以前默認的一樣,如果一樣,就404...
    這個試的步驟是這樣的。每一步都先清空路由配置。
    1,注釋所有自定義路由,打開默認路由。訪問/api/v1/index/index OK
    2,自定義/api/v1/aaaa,打開默認路由。訪問/api/v1/index/index OK ,訪問/api/v1/aaaa OK
    3、自定義/api/v1/aaaa,關閉默認路由。訪問/api/v1/index/index 404,訪問/api/v1/aaaa OK
    3、自定義/api/v1/index/index,關閉默認路由。訪問/api/v1/index/index 404

wybwsk

解決了。。不要安裝webman/domain多域名插件,就使用項目默認的路由。
然后參照http://wtbis.cn/q/7922來配置。

nginx
在nginx配置中,不要按文檔里面的去添加代理。比如我的webman默認端口10086.
按文檔的話要在nginx中添加

upstream webman {
    server 127.0.0.1:10086;
    keepalive 10240;
}

這一段不要加。
就在

server
{
    listen 80;
    server_name adminapi.test.com api.test.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/api.test.com/public;

    include /www/wwwroot/api.test.com/nginx/*.conf;  //加上這一條,代理和rewrite配置都寫在項目文件里。

然后在項目的根目錄下添加nginx目錄,建兩個文件,一個文件也行。。一個代理配置,一個rewrite配置。
/api.test.com
|--nginx
|----proxy.conf
|----rewrite.conf
proxy.conf

location ^~ /
{
        if (!-e $request_filename){
           proxy_pass http://127.0.0.1:10086;
        }
         #設置域名 不加這個webman獲取不到用戶IP
        proxy_set_header Host $host;
        #設置用戶IP 不加這個webman獲取不到用戶IP
        proxy_set_header X-Real-IP $remote_addr;
        #這個我也不知道干啥的反正加上就對了
        proxy_set_header REMOTE-HOST $remote_addr;
        #不需要關閉nginx緩存刪掉下面這行
        proxy_cache off;
}

rewrite.conf
if (-f $request_filename){
break;
}

根據(jù)域名重寫url

if ($host = 'adminapi.test.com') {
rewrite ^/(.*)$ /adminapi/$1 last;
}

根據(jù)域名重寫url

if ($host = 'api.test.com') {
rewrite ^/(.*)$ /api/$1 last;
}

然后這時,/config/route.php里面的路由就跟TP的一樣了,強制路由,子域名綁定。

use Webman\Route;

Route::group('/adminapi', function () {
    Route::get('/test', [app\adminapi\controller\v1\IndexController::class, 'index']);
    Route::get('/v1/Index/index', [app\adminapi\controller\v1\IndexController::class, 'index']);
});

Route::group('/api', function () {
    Route::get('/test', [app\api\controller\v1\IndexController::class, 'index']);
//    Route::get('/v1/Index/index', [app\api\controller\v1\IndexController::class, 'index']);
});
Route::disableDefaultRoute();
  • 暫無評論
年代過于久遠,無法發(fā)表回答
??