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

Method for comparing the size of varchar type numbers in MySQL database

Create a test table -- --------------------------...

Linux command line quick tips: How to locate a file

We all have files stored on our computers -- dire...

Vue3+script setup+ts+Vite+Volar project

Table of contents Create a vue + ts project using...

Start a local Kubernetes environment using kind and Docker

introduce Have you ever spent a whole day trying ...

Detailed explanation of the execution process of mysql update statement

There was an article about the execution process ...

Mysql splits string into array through stored procedure

To split a string into an array, you need to use ...

Example of implementing circular progress bar in Vue

Data display has always been a demand that all wa...

How to limit the value range of object keys in TypeScript

When we use TypeScript, we want to use the type s...

Axios cancels repeated requests

Table of contents Preface 1. How to cancel a requ...

Implementation and optimization of MySql subquery IN

Table of contents Why is IN slow? Which is faster...

Sample code for implementing markdown automatic numbering with pure CSS

The origin of the problem The first time I paid a...