Nginx reverse proxy configuration to remove prefix case tutorial

Nginx reverse proxy configuration to remove prefix case tutorial

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 there is a / at the end of proxy_pass, 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.

This is the end of this article about the nginx reverse proxy configuration to remove the prefix case tutorial. For more relevant nginx reverse proxy configuration to remove the prefix content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Explanation of nginx load balancing and reverse proxy
  • Getting Started with Nginx Reverse Proxy
  • Full process record of Nginx reverse proxy configuration
  • How to implement Nginx reverse proxy for multiple servers
  • About nginx to implement jira reverse proxy

<<:  61 Things Every Web Developer Should Know

>>:  Detailed explanation of Vue life cycle functions

Recommend

Examples of using temporary tables in MySQL

I've been a little busy these two days, and t...

CentOS 8 custom directory installation nginx (tutorial details)

1. Install tools and libraries # PCRE is a Perl l...

Solution to the problem of var in for loop

Preface var is a way to declare variables in ES5....

Slot arrangement and usage analysis in Vue

The operating environment of this tutorial: Windo...

JavaScript Canvas implements Tic-Tac-Toe game

This article shares the specific code of JavaScri...

Detailed steps to configure my.ini for mysql5.7 and above

There is no data directory, my-default.ini and my...

Detailed explanation of how to use zabbix to monitor oracle database

1. Overview Zabbix is ​​a very powerful and most ...

MySQL learning database backup detailed explanation

Table of contents 1.DB,DBMS,SQL 2. Characteristic...

Detailed explanation of the usage and difference between nohup and & in Linux

Example: We use the Python code loop_hello.py as ...

Summary of common operation skills of MySQL database

This article summarizes common operating techniqu...

Implementation of element shuttle frame performance optimization

Table of contents background Solution New Questio...

Detailed explanation of MySQL foreign key constraints

Official documentation: https://dev.mysql.com/doc...