NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
among
0.01D
V2EX  ›  NGINX

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

  •  
  •   among · Jul 18, 2022 · 3025 views
    This topic created in 1416 days ago, the information mentioned may be changed or developed.

    有三个系统, 需要捆绑在一起提供服务,一个 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 replies    2022-07-19 10:04:10 +08:00
    MilkShake
        1
    MilkShake  
       Jul 19, 2022   ❤️ 3
    如果是公网,我建议你上几个三级域名,通过请求头的 host 来跳转不同的系统入库。
    t1nyf00
        2
    t1nyf00  
       Jul 19, 2022
    或许可以这样子 (
    ```
    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  
       Jul 19, 2022 via Android   ❤️ 2
    同意#1 。
    你这需求让我感觉好像回到了 tomcat 时代:一个网站多个 web application

    这种模式不是特别方便。
    首先,要小心绝对路径,比如 /xxx.js 之类的,可能得改成相对路径。现代化前端有 publicPath ,但后端我就不确定了。
    另外,同源限制、cookie 之类的东西不关注下面的路径,而是看主机。对于你这种原本分开的应用,隔离性不是很好。
    shadowsll
        4
    shadowsll  
       Jul 19, 2022
    根据不同的子目录设置即可!
    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,都要单独配置一下。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1306 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 17:24 · PVG 01:24 · LAX 10:24 · JFK 13:24
    ♥ Do have faith in what you're doing.