The Nginx agent proxy pass is configured with an implementation that removes the prefix

  • 2020-05-24 06:47:37
  • OfStack

directory

One scenario is proxy_pass followed by root path /. Another option is to use rewrite

When using Nginx as a proxy, you can simply forward the request to the next service intact.

. For example, access abc com appv2 / a/b html, forward the requirement to the localhost: 8088 / appv2 / a/b html

The simple configuration is as follows:


upstream one {
 server localhost:8088 weight=5;
}

server {
  listen       80;
  server_name     abc.com;
  access_log "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G" main;

  location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP    $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://one;
  }

}

That is, set proxy_pass. The request only replaces the domain name.

But many times, we need to forward to different services based on the url prefix.

Such as

abc. com/user/profile. html localhost forwarded to the user service: 8089 / profile html

abc. com/order/details. html localhost forwarded to order service: 8090 / details html

That is, the url prefix is not required for downstream services, unless downstream services add context-path, which we don't like a lot of the time. If only Nginx had dropped the prefix when forwarding.

One option is proxy_pass followed by root /.


server {
  listen       80;
  server_name     abc.com;
  access_log "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G" main;

  location ^~/user/ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP    $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://user/;
  }

  location ^~/order/ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP    $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://order/;
  }

}

^~/user/ means that the request with the prefix user is matched, and proxy_pass ends with /, then the path after /user/* will be splicing directly to the end, that is, user will be removed.

Another option is to use rewrite


upstream user {
 server localhost:8089 weight=5;
}
upstream order {
 server localhost:8090 weight=5;
}


server {
  listen       80;
  server_name     abc.com;
  access_log "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G" main;

  location ^~/user/ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP    $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    rewrite ^/user/(.*)$ /$1 break;
    proxy_pass http://user;
  }

  location ^~/order/ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP    $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    rewrite ^/order/(.*)$ /$1 break;
    proxy_pass http://order;
  }

}

Notice that proxy_pass ends without /, and rewrite overrides url.

About rewrite


syntax: rewrite regex replacement [flag]
Default:  - 
Context: server, location, if


Related articles: