一个 ip 根据 url,配置 3 个平台

2022-07-18 23:58:03 +08:00
 among

有三个系统, 需要捆绑在一起提供服务,一个 ip ,根据 url 来区分

当前的场景 三个机器,三个 nginx ,分别配置了这样的三个服务

location / {
    root /data/dist;
    try_files $uri $uri/ /index.html;
}

location /api {
client_max_body_size 1000m;
    proxy_pass http://127.0.0.1:1111/api/;
    proxy_read_timeout 1200;
}

现在需要,捆绑到一个 ip 中去。

http://10.10.10.10/index ,会作为首页

http://10.10.10.10/aaa ,会作为 a 系统的入口

http://10.10.10.10/bbb ,会作为 b 系统的入口

http://10.10.10.10/ccc ,会作为 c 系统的入口

同时希望后端改造越少越好,通过 nginx 是否可以不改后端,同时支持。

1872 次点击
所在节点    NGINX
4 条回复
aaa5838769
2022-07-19 00:10:41 +08:00
如果是公网,我建议你上几个三级域名,通过请求头的 host 来跳转不同的系统入库。
t1nyf00
2022-07-19 00:28:35 +08:00
或许可以这样子 (
```
location / {
# 不知道你的首页准备放什么内容
root /data/dist;
try_files $uri $uri/ /index.html;
}

location /aaa/ {
# 使用 alias 的原因是, 请求 /aaa/index.html 时会访问 /data/aaa/dist/index.html 文件
# 如果使用 root /data/aaa/dist 的话, 在访问 /aaa/index.html 时会访问 /data/aaa/dist/aaa/index.html
# ref: https://nginx.org/en/docs/http/ngx_http_core_module.html#alias
alias /data/aaa/dist;
try_files $uri $uri/ /aaa/index.html
}

location /aaa/api/ {
# 注意最后的 / 以及你跑在同一台机器上后端口需要做区分
proxy_pass http://10.10.10.10:1111/api/;
}


location /bbb/ {
alias /data/bbb/dist;
try_files $uri $uri/ /bbb/index.html
}

location /bbb/api/ {
proxy_pass http://10.10.10.10:1112/api/;
}


location /ccc/ {
alias /data/ccc/dist;
try_files $uri $uri/ /ccc/index.html
}

location /ccc/api/ {
proxy_pass http://10.10.10.10:1113/api/;
}
```
dcsuibian
2022-07-19 02:19:26 +08:00
同意#1 。
你这需求让我感觉好像回到了 tomcat 时代:一个网站多个 web application

这种模式不是特别方便。
首先,要小心绝对路径,比如 /xxx.js 之类的,可能得改成相对路径。现代化前端有 publicPath ,但后端我就不确定了。
另外,同源限制、cookie 之类的东西不关注下面的路径,而是看主机。对于你这种原本分开的应用,隔离性不是很好。
shadowsll
2022-07-19 10:04:10 +08:00
根据不同的子目录设置即可!
location /aaa/
{
root /home/wwwroot/;
index index.php index.html;
if (!-e $request_filename){
rewrite ^/aaa/(.*)$ /aaa/index.php?s=$1 last;
}
}

其他子目录同样设置,需要注意的是项目下面的 js,css,image,都要单独配置一下。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/867133

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX