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

Detailed steps to build an independent mail server on Centos7.9

Table of contents Preface 1. Configure intranet D...

How to install Nginx and configure multiple domain names

Nginx Installation CentOS 6.x yum does not have n...

MySQL Workbench download and use tutorial detailed explanation

1. Download MySQL Workbench Workbench is a graphi...

JavaScript implements Tab bar switching effects

Here is a case that front-end developers must kno...

JS realizes simple picture carousel effect

This article shares the specific code of JS to ac...

Detailed process of installing logstash in Docker

Edit docker-compose.yml and add the following con...

jQuery implements dynamic tag event

This article shares the specific code of jQuery t...

Solution to 1045 error in mysql database

How to solve the problem of 1045 when the local d...

How to display texture at the position of swipe in CocosCreator

Table of contents 1. Project requirements 2. Docu...

Advantages and disadvantages of Table layout and why it is not recommended

Disadvantages of Tables 1. Table takes up more byt...