skip to Main Content

There is currently an app that is using cname host mapping with a third party app

the hostname for the third party app is mycompany.partner.com
and the current cname host map under my domain help.mycompany.com

so current routing is below

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

now i want to do this

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

is this possible?

So basically i want all traffic to come to my own app via nginx now and then i route some traffic based on url path to the third party app and others to my frontend app

how can i achieve this with nginx? below are the url paths i want to route

this routes to my frontend app

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 

this rewrites/routes to partner app

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 

location block to route all url paths to the frontend app is below

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;
}

but now i need to split some url paths to the partner app as stated above

so basically all traffic will pass through nginx as i will point DNS for help.mycompany.com to now point to my nginx reverse proxy and then routes and rewrites the url

Thanks

2

Answers


  1. You have to create different location for uri paths. For Example:

    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;
    }
    

    In above case holds good for REST calls where are not trying to render static js or html files, which can be domain specific. You are rendering static js or html , trying adding those paths also to the domain there are present.

    If you want to redirect specific location to particular domain, please take a look the example below:

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

    In this case all the specified uri will get redirect to the domain one specify.

    Remember to add the generic path, if nothing matches it will go to this one. Example below:

    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;
    }
    

    Below is the example for a sample 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;
      }
    }
    

    Hit http://localhost:8888/search?q=zenduty on your browser to test. localhost nginx server will proxy the request on google search. One can add extra headers if needed, will proxying.

    Login or Signup to reply.
  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 "healthyn";
        }
      }
    
    
    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 "healthyn";
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search