Nginx proxy Pass Configuration remove prefix

Source: Internet
Author: User

When using nginx as a proxy, you can simply forward requests to the next service without stopping them.

For example, to access abc.com/appv2/a/ B .html, you must forward the request to 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, Setproxy_passYou can. The request only replaces the domain name.

But in many cases, we need to forward to different services based on the prefix of the URL.

For example

Abc.com/user/profile.htmlresendUser ServiceLocalhost: 8089/profile.html

Abc.com/order/details.htmlresendOrder ServiceLocalhost: 8090/details.html

That is, the URL prefix is not required for downstream services unless the downstream service adds context-path, but we do not like to add this in many cases. If this prefix is removed during nginx forwarding.

One solution is to add the root path after proxy_pass. /.
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/Indicates that the matched prefix isuserThe end of proxy_pass is//user/*The following path is directly spliced to the back to remove the user.

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

Note that proxy_pass does not end/,rewriteThe URL is rewritten.

Rewrite

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

Nginx proxy Pass Configuration remove prefix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.