【问题标题】:nginx rewrite of cname host mapping url pathsnginx重写cname主机映射url路径
【发布时间】:2022-11-03 01:22:26
【问题描述】:

当前有一个应用程序正在使用 cname 主机映射与第三方应用程序

第三方应用的主机名是mycompany.partner.com 以及我的域help.mycompany.com 下的当前 cname 主机映射

所以当前的路由在下面

users => cloudflare DNS(help.mycompany.com) => cname host mapping(mycompany.partner.com) => partner app

现在我想这样做

                                                             => cname host mapping(mycompany.partner.com) => partner app
                                                            |
users => cloudflare DNS(help.mycompany.com) => my nginx =>  |
                                                            |
                                                             => my frontend app

这可能吗?

所以基本上我希望所有流量现在都通过 nginx 进入我自己的应用程序,然后我根据 url 路径将一些流量路由到第三方应用程序,将其他流量路由到我的前端应用程序

我怎样才能用 nginx 做到这一点?下面是我要路由的 url 路径

这路由到我的前端应用程序

help.mycompany.com/app/test1                  => http://localhost:500/app/test1  
help.mycompany.com/app/test2/test3            => http://localhost:500/app/test2/test3
help.mycompany.com/app/parameter?key=check    => http://localhost:500/app/parameter?key=check 

这会重写/路由到合作伙伴应用程序

help.mycompany.com/app/partner1               => https://mycompany.partner.com/app/partner1 
help.mycompany.com/app/discuss/check          => https://mycompany.partner.com/app/discuss/check

and all other paths 

将所有 url 路径路由到前端应用程序的位置块如下

location ^~ / {

    rewrite ^/(.*)$ /$1 break;
    proxy_pass http://localhost:500;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
}

但现在我需要将一些 url 路径拆分到合作伙伴应用程序,如上所述

所以基本上所有流量都将通过 nginx,因为我将 DNS 指向 help.mycompany.com 现在指向我的 nginx 反向代理,然后路由并重写 url

谢谢

【问题讨论】:

    标签: nginx dns url-rewriting reverse-proxy


    【解决方案1】:

    您必须为 uri 路径创建不同的位置。例如:

    location /app/partner1 {
       proxy_pass https://mycompany.partner.com;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $host;
       proxy_redirect off;
    }
    location /app/discuss/check {
       proxy_pass https://mycompany.partner.com;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $host;
       proxy_redirect off;
    }
    

    在上述情况下,适用于不尝试呈现静态 js 或 html 文件的 REST 调用,这些文件可以是特定于域的。您正在渲染静态 js 或 html ,尝试将这些路径也添加到存在的域中。

    如果您想将特定位置重定向到特定域,请查看以下示例:

    location /app/discuss/check {
       return 301 https://mycompany.partner.com$request_uri;
    }
    

    在这种情况下,所有指定的 uri 都将重定向到指定的域。

    请记住添加通用路径,如果没有匹配项,它将转到此路径。下面的例子:

    location / {
      proxy_pass http://localhost:500;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
      proxy_redirect off;
    }
    

    以下是示例 conf 的示例。

    server {
      listen       8888;
      server_name  localhost;
    
      # default path
      location / {
        proxy_pass http://localhost:500;
      }
      # third party proxy paths
      location /app/discuss/check {
        proxy_pass https://mycompany.partner.com;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
      }
      location /app/partner1 {
        proxy_pass https://mycompany.partner.com;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
      }
      location /test {
        add_header Content-Type text/plain;
        return 200 "All Good !";
      }
      location /search {
        proxy_set_header X-Scope-OrgID cluster1;
        proxy_pass_request_headers on;
        
        proxy_pass   https://www.google.com;
        proxy_redirect off;
      }
    }
    

    在浏览器上点击http://localhost:8888/search?q=zenduty 进行测试。 localhost nginx 服务器将代理谷歌搜索的请求。如果需要,可以添加额外的标头,将代理。

    【讨论】:

    • 我正在尝试将某些特定路径与内部服务匹配,然后将所有其他路径与第三方应用程序匹配 cname 映射。所以不确定你是否很好地回答了我的问题
    • 你可以在定义你的conf时提供server_name help.mycompany.com;。您将仅在 help.mycompany.com 上收到请求,然后将根据 conf 重定向或转发。
    • 所以我必须有不同的server_names?您介意更新答案以查看其外观吗?将不胜感激....nginx 将路由相同的 servern_name 只是一些路径将进入内部应用程序,一些路径将进入第三方
    • 使用示例配置更新了答案。
    • 我如何使用正则表达式捕获需要转到前端应用程序localhost:500 的路径?这就是为什么我列出了那些需要路由到它的路径。我现在还需要匹配第三方应用程序的所有其他路径。这就是我想要做的。认为我试图在问题中很好地解释它
    【解决方案2】:
        we can create multiple HTTP and server blocks and we can separate the traffic according to the path.
    user  nginx;
    worker_processes  2;
    
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  4096;
    }
    
    http {
      gzip on;
      gzip_proxied any;
      gzip_types text/plain application/json;
      gzip_min_length 1000;
      server_tokens off;
    
    
      map $host $record_name {
        "~^(?<record>[^.]+).your.yourcomapany.com$" "$record";
      }
    
      server {
        listen 80;
        server_name yourcompany.com;
        underscores_in_headers on;
    
        set $backend https://$record_name.yourapp-oryourcompanybacckend;
    
       
    
        location /{
          if ($http_x_forwarded_proto = "http") {
            return 301 https://$host$request_uri;
          }
        
          proxy_pass_request_headers on;
          proxy_pass $backend$request_uri;
          proxy_http_version 1.1;
    
    #      proxy_intercept_errors on;
    #      error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 420 422 423 424 426 428 429 431 444 449 450 451 500 501 502 503 504 505 506 507 508 509 510 511 =302 http://maintenance.yourcompany.com/;
    
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
        location /healthy {
            access_log off;
            return 200 "healthy
    ";
        }
      }
    
    
    server {
        listen 80;
        server_name yourcompanysecondpath;
        underscores_in_headers on;
    
        set $backend http://yourapp-oryourcompany-secondbacckend;
    
        
    
        location /{
          if ($http_x_forwarded_proto = "http") {
            return 301 https://$host$request_uri;
          }
        
          proxy_pass_request_headers on;
          proxy_pass $backend$request_uri;
          proxy_http_version 1.1;
    
    #      proxy_intercept_errors on;
    #      error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 420 422 423 424 426 428 429 431 444 449 450 451 500 501 502 503 504 505 506 507 508 509 510 511 =302 http://maintenance.company.com/;
    
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
        location /healthy {
            access_log off;
            return 200 "healthy
    ";
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 2017-07-05
      相关资源
      最近更新 更多