Nginx routing forwarding and reverse proxy location configuration implementation

Nginx routing forwarding and reverse proxy location configuration implementation

Three ways to configure Nginx

The first method directly replaces the location matching part

The second proxy_pass target address does not have a / by default, which means that only the proxy domain name is used, and the url and parameter parts will not change (the requested path is concatenated with the proxy_pass target domain name as the proxy URL)

The third type of proxy_pass target address adds a / after it, which means that the part of the path that successfully matches the location is cut off and then spliced ​​into the proxy_pass target address.

Location configuration

location [ = | ~ | ~* | ^~ ] uri {...}

The content in square brackets before uri is optional and is explained as follows:

  • "=": used before the standard URI, requiring the request string to strictly match the URI, and stopping once the match is successful
  • "~": used before the regular uri and is case-sensitive
  • "~*": used before the regular uri, but not case-sensitive
  • "^~": used before the standard URI, requiring Nginx to find the location with the highest match between the URI and the request string, and immediately use this location to process the request, instead of using the regular URI in the location block to match the request string.

symbol meaning
= Exact Match
^~ Non-regular matching
~ Regular expression matching (case sensitive)
~* Regular expression matching (case insensitive)
!~ Regular expression does not match (case sensitive)
!~* Regular expression does not match (case insensitive)
Normal matching (when there is no symbol here)

example

For example, the following configuration demonstrates the third configuration scheme. When we visit http://44.179.118.54:80/shop/xxx

When accessing, Nginx will intercept /shop/ and then concatenate the following path to proxy_pass

Then what we actually visit is: http://44.179.118.54:8007/xxx this service

 #shop-service
 # Reverse proxy shop-service service location ^~ /shop/ {
     #proxy_redirect off;
     #proxy_connect_timeout 60;
     #proxy_read_timeout 60;
     #proxy_send_timeout 60;
     #proxy_buffer_size 4k;
     #proxy_buffers 4 32k;
     #proxy_busy_buffers_size 64k;
     #proxy_temp_file_write_size 64k;
     #proxy_max_temp_file_size 128m;
     proxy_pass http://44.179.118.54:8007/;
     #proxy_set_header X-Real-IP $remote_addr;

     #root /var/www/test/user/
     #index index.html
     #proxy_pass https://www.baidu.com;
 }

This is the end of this article about Nginx routing forwarding and reverse proxy location configuration implementation. For more related Nginx routing forwarding and reverse proxy content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Nginx application location routing reverse proxy and rewrite strategy example
  • Nginx configures the location module to implement routing (reverse proxy, redirection) functions

<<:  HTML head structure

>>:  Overview and application of position attributes (absolute|relative|static|fixed) in CSS

Recommend

A simple way to achieve scrolling effect with HTML tag marquee (must read)

The automatic scrolling effect of the page can be...

Implementing circular scrolling list function based on Vue

Note: You need to give the parent container a hei...

Detailed explanation of keepAlive use cases in Vue

In development, it is often necessary to cache th...

How to automatically import Vue components on demand

Table of contents Global Registration Partial Reg...

Detailed explanation of how to use awk in Linux

Before learning awk, we should have learned sed, ...

How to compile and install xdebug in Ubuntu environment

This article describes how to compile and install...

Writing a shell script in Ubuntu to start automatically at boot (recommended)

The purpose of writing scripts is to avoid having...

Example of converting JavaScript flat array to tree structure

Table of contents 10,000 pieces of data were lost...

Detailed explanation of the role of key in React

Table of contents Question: When the button is cl...

The most comprehensive explanation of the locking mechanism in MySQL

Table of contents Preface Global Lock Full databa...

Vue implements Tab tab switching

This article example shares the specific code of ...