V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
among
V2EX  ›  NGINX

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

  •  
  •   among · 2022-07-18 23:58:03 +08:00 · 1833 次点击
    这是一个创建于 619 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有三个系统, 需要捆绑在一起提供服务,一个 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 是否可以不改后端,同时支持。

    4 条回复    2022-07-19 10:04:10 +08:00
    aaa5838769
        1
    aaa5838769  
       2022-07-19 00:10:41 +08:00   ❤️ 3
    如果是公网,我建议你上几个三级域名,通过请求头的 host 来跳转不同的系统入库。
    t1nyf00
        2
    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
        3
    dcsuibian  
       2022-07-19 02:19:26 +08:00 via Android   ❤️ 2
    同意#1 。
    你这需求让我感觉好像回到了 tomcat 时代:一个网站多个 web application

    这种模式不是特别方便。
    首先,要小心绝对路径,比如 /xxx.js 之类的,可能得改成相对路径。现代化前端有 publicPath ,但后端我就不确定了。
    另外,同源限制、cookie 之类的东西不关注下面的路径,而是看主机。对于你这种原本分开的应用,隔离性不是很好。
    shadowsll
        4
    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,都要单独配置一下。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5351 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 07:08 · PVG 15:08 · LAX 00:08 · JFK 03:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.