【问题标题】:NGINX: How to concatenate two servers with same address and nameNGINX:如何连接两个具有相同地址和名称的服务器
【发布时间】:2021-04-16 06:22:41
【问题描述】:

情况:

  1. 我有许多(未定义数量)Web 应用程序(app1 的 URI 为“/app1”,app2 的 URI 为“/app2”)在同一台机器上侦听端口 80,并在配置文件中使用相同的 server_name。
  2. 有一个自动化工具可以绝对独立地管理每个项目的配置文件。因此我需要两个不同的配置文件。
  3. 如果我尝试将 server 指令包含在相同的 listenserver_name 指令中,期望服务器连接(来自服务器的所有指令都像它们位于一台服务器中一样工作,我会收到警告 @ 987654324@,其中一台服务器将被忽略。
  4. 可以说“只需将location /app1 {...} 放入app1.conf,location /app2 {...} 放入app2.conf,...,然后将此配置包含在一个server 指令中”。但这不是一个选项,因为我需要每个应用程序都有自己的 map 指令,而 map 指令不能合并到 server 指令中。
  5. 我不能使用不同的 server_name,因为它已经有一个三级域,而四级域并不是一个真正的选择。

问题:如何实现所描述的“串联”?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    我想出了两个主意

    第一个想法:单独的配置部分(不是通常的视图,没有隔离)

    Inner-server 和outer-server 指令可以放在另外两个文件中。

    这里没有出现隔离问题,即 app2 覆盖来自 app1 的内容,并针对不可覆盖的内容发出错误警报。

    注意:以下示例仅用于简单演示,并非实际练习。

    文件系统的树

    | /etc/nginx
    |-- nginx.conf
    |-- conf.d
    |--|-- some_server_name_and_port_80.conf
    |--|-- some_server_name_and_port_80.outer_server.d
    |--|--|-- app1.outer_server.conf
    |--|--|-- app2.outer_server.conf
    |--|-- some_server_name_and_port_80.inner_server.d
    |--|--|-- app1.inner_server.conf
    |--|--|-- app2.inner_server.conf
    

    文件内容

    nginx.conf:

    ...
    http {
        ...
        # If default_type is already defined, just replace its value
        default_type text/plain ;
    
        include /etc/nginx/conf.d/*.conf ;
    }
    

    some_server_name_and_port_80.conf:

    include /etc/nginx/conf.d/some_server_name_and_port_80.outer_server.d/*.conf ;
    server {
        listen      80 ;
        server_name some_server_name ;
    
        include /etc/nginx/conf.d/some_server_name_and_port_80.inner_server.d/*.conf ;
    }
    

    app1.outer_server.conf(对于 app2 将 app1 替换为 app2):

    map $request_uri $app1_var {
        ~^/app1(.*) /app$1 ;
        default    $request_uri ;
    }
    

    app1.inner_server.conf(对于 app2 将 app1 替换为 app2):

    location /app1/ {
        return 200 "app1: Hello, World! The universal uri is $app1_var" ;
    }
    

    第二种方式:使用不同的服务器和使用proxy_pass的简单路由(需要注意proxy_pass的特性)

    注意:以下示例仅用于简单演示,并非实际练习。

    文件系统的树

    | /etc/nginx
    |-- nginx.conf
    |-- conf.d
    |--|-- some_server_name_and_port_80.conf
    |--|-- some_server_name_and_port_80.d
    |--|--|-- app1.router.conf
    |--|--|-- app2.router.conf
    |--|-- app1.conf
    |--|-- app2.conf
    

    文件内容

    nginx.conf:

    ...
    http {
        ...
        include /etc/nginx/conf.d/*.conf ;
    }
    

    some_server_name_and_port_80.conf:

    server {
        listen      80 ;
        server_name some_server_name ;
        
        # If default_type is already defined, just replace its value
        default_type text/plain ;
    
        include /etc/nginx/conf.d/some_server_name_and_port_80.d/*.conf ;
    }
    

    app1.router.conf(对于 app2,将 app1 替换为 app2,将 81 替换为 82):

    location /app1/ {
        proxy_pass http://127.0.0.1:81 ;
    }
    

    app1.conf(对于 app2,将 app1 替换为 app2,将 81 替换为 82):

    map $request_uri $app1_var {
        ~^/app1(.*) /app$1 ;
        default    $request_uri ;
    }
    
    server {
        listen      81 ;
        server_name some_server_name ;
    
        location /app1/ {
            return 200 "app1: Hello, World! The universal uri is $app1_var" ;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多