请教:网站部分静态资源的路由配置

lyiply

问题描述

我的项目使用了多应用插件,这是我正常的路由【1】
Route::group('/test', function () {
Route::get('/',[app\test\controller\TestController::class, 'index']);
}}
这是我有问题的路由【2】
Route::group('/test', function () {
Route::get('/', function(){ return response('hello webman'); });
}}
我想把首页改成静态文件index.html输出,但路由【2】只会返回404,不会显示hello webman,
其实我想要的是路由【3】
Route::group('/test', function () {
Route::get('/', function(){ return response()->file(public_path() . '/test/index.html'); });
}}
我想要把网站部分静态化,部分动态化,首页我静态化时直接访问域名web.test.com.cn就404,如果域名web.test.com.cn/index.html就正常

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

1> 多应用的关系nginx那边不好动,location不能单独对域名处理,会影响所有的,如果有方案只影响一个应用也行,我的配置是if ($host = 'web.test.com.cn'){
set $flag "test";
}
rewrite ^/(.*)$ /$flag/$1 last;
2> webman路由【2】不能正常工作,路由【1】是正常的【动态的】,我想要实现路由【3】的效果没成功,反复测试路由【2】这里就卡死了;路由【2】、路由【3】只会返回404

我要直接访问域名web.test.com.cn与就是访问web.test.com.cn/index.html的效果,麻烦大神指点一下!

90 2 0
2个回答

muyu

没明白,如果想首页是index.html,调整nginx文件顺序不就行了吗,当不是访问首页的时候,走路由

  • lyiply 5天前

    无用,早测试过了,现在资源访问都是通过webman框架接管了

  • six 4天前

    截图
    问AI,加这句就行了

muyu

一下为文心一言生成代码

server {
    listen 80;
    server_name your_domain.com; # 替换为你的域名或IP地址

    root /path/to/your/webman/public; # Webman的public目录路径
    index index.html index.php; # 设置index.html为首页,同时保留index.php以支持Webman的路由

    location / {
        try_files $uri $uri/ /index.html; # 尝试直接访问文件或目录,如果找不到则重定向到index.html

        # 对于Webman的WebSocket等特定请求,需要转发到指定的端口(例如9501)
        # 下面的配置假设你的Webman运行在9501端口
        if ($request_method = "POST" ) {
            set $webman_script $uri;
        }

        if ($query_string ~* ".*_route=.*") {
            set $webman_script $uri?$query_string;
        }

        if (-s $document_root$webman_script) {
            rewrite ^(.*)$ $webman_script last;
        }

        # 将所有非静态文件请求转发到Webman的监听端口
        location ~ \.php$ {
            proxy_pass http://127.0.0.1:9501; # Webman运行的端口
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param QUERY_STRING $query_string;
            fastcgi_param REQUEST_METHOD $request_method;
            fastcgi_param CONTENT_TYPE $content_type;
            fastcgi_param CONTENT_LENGTH $content_length;
            fastcgi_intercept_errors on;
        }

        # 如果不是PHP文件,则尝试作为静态文件服务,如果找不到则回退到index.html
        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.html last;
        }
    }

    # 静态文件处理,如CSS、JS、图片等
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
    }

    # 错误页面
    error_page 404 /404.html;
    location = /404.html {
        root /path/to/your/webman/public; # 指定错误页面的路径
        internal;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /path/to/your/webman/public; # 指定错误页面的路径
        internal;
    }
}
  • lyiply 5天前

    DEEPSEEK的回答更准确一点,但执行不了,所以来提问

×
🔝