Nginx reverse proxy configuration removes prefix

Nginx reverse proxy configuration removes prefix

When using nginx as a reverse proxy, you can simply forward the request to the next service intact. Setting the proxy_pass request will only replace the domain name. If you want to access different services based on different URL suffixes, you need to use the following method:

Method 1: Add "/"

server {
    listen 8000;
    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 matching requests with the prefix user . If proxy_pass ends with / , the path after /user/* will be directly concatenated to the end, that is, user will be removed.

Method 2: 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;
 }
}

There is no / at the end of proxy_pass , and rewrite rewrites the URL.

---------------------

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Full process record of Nginx reverse proxy configuration
  • Detailed explanation of Nginx configuration parameters in Chinese (load balancing and reverse proxy)
  • Nginx forward and reverse proxy and load balancing functions configuration code example
  • Simple steps to configure Nginx reverse proxy with SSL
  • Detailed steps for yum configuration of nginx reverse proxy
  • Detailed explanation of nginx reverse proxy webSocket configuration
  • A universal nginx interface to implement reverse proxy configuration
  • Nginx reverse proxy configuration to remove prefix case tutorial

<<:  Solution to forgetting the MYSQL database password under MAC

>>:  Understanding what Node.js is is so easy

Recommend

CentOS 7 installation and configuration tutorial under VMware10

If Ubuntu is the most popular Linux operating sys...

Several ways to store images in MySQL database

Usually the pictures uploaded by users need to be...

Vue development tree structure components (component recursion)

This article example shares the specific code of ...

Various front-end printing methods of web: CSS controls web page printing style

CSS controls the printing style of web pages : Use...

CentOS7 deploys version 19 of docker (simple, you can follow it)

1. Install dependency packages [root@localhost ~]...

Six weird and useful things about JavaScript

Table of contents 1. Deconstruction Tips 2. Digit...

Practical TypeScript tips you may not know

Table of contents Preface Function Overloading Ma...

Detailed explanation of how to use the Vue license plate input component

A simple license plate input component (vue) for ...

mysql method to recursively search for all child nodes of a menu node

background There is a requirement in the project ...

How to create https using nginx and Tencent Cloud free certificate

I have been studying how to get https. Recently I...

Vue implements the function of calling the mobile phone camera and album

This article shares the specific code of Vue to a...

Vue implements countdown between specified dates

This article example shares the specific code of ...