问下大家,这种情况下路由应该如何配置,或者配置重定向

我现在有两个域名, 一套代码,比如
test.com, admin.test.com
代码一套,前台和后台,前台是laravel提供接口,然后前端用vue写的,后台是laravel-admin搭建的,里面是blade模版

现在我想 test.com 访问 public下的 index.html, 前端代码也直接放到public里面了,然后 admin.test.com 访问 后台,admin/index/index
我应该怎么配置nginx呢?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 11

前端代码不一定要放在public下的,admin.test.com创建一个新的网站就行

1年前 评论
donggan (楼主) 1年前
donggan (楼主) 1年前
my38778570 (作者) 1年前

可以使用nginx 的反向代理,需要了解路由是如何解析到php服务的,这样不管是path模式还是进程端口模式,都难不倒你。以下供参考:

test.com

  server {
    listen       80;
    server_name  test.com;

    location / {
        root    vue index.html文件所在路径;
        index   index.html;
    }

    # 如果接口前缀是 /api 代理到php server
    location /api {
        proxy_set_header client-real-ip $remote_addr;
        proxy_pass http://localhost:8001/api;
    }
}

admin.test.com

新建一个 server

  server {
    listen       80;
    server_name  admin.test.com;

    # 如果路由是 /admin 代理到 php server
    location /admin {
        proxy_set_header client-real-ip $remote_addr;
        proxy_pass http://localhost:8001/admin;
    }
}

php server

server {
    listen      8001;
    listen      [::]:8001;
    server_name _;
    root        项目路径;
    index       index.php;

    location / {
        try_files       $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass    unix:/var/opt/remi/php81/run/php-fpm/www.sock;
        fastcgi_param   SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include         fastcgi_params;
    }
}
1年前 评论
donggan (楼主) 1年前
donggan (楼主) 1年前
chowjiawei

用一个nginx即可

 location /admin {
        alias /var/www/前端项目;
        index index.htm index.html;
        try_files $uri $uri/ /index.html last;
    }

location /admin 比如你的网站是platform.test    那么 platform.test/admin 即为你的前端项目 需要注意  你的后端项目将不可使用admin开头的路由
1年前 评论
donggan (楼主) 1年前

domain.Admin=>’admin.test.com’;
Route::group([‘domain’ => config(‘domain.Admin’)], function () {
//里边包括你后端的路由
// Route::get(‘/‘, ‘AdminController@index’);// 后台
// Route::view(‘/‘, ‘admin.index.index’);
});

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!