Differences between proxy_pass in two modules in nginx

Differences between proxy_pass in two modules in nginx

1. The proxy_pass directive of the 1.ngx_stream_proxy_module module

It can only be used in the server segment, and only requires the domain name or IP address and port.

It can be understood as port forwarding, which can be either a TCP port or a UDP port.

server {
    listen 127.0.0.1:12345;
    proxy_pass 127.0.0.1:8080;
}
 
server {
    listen 12345;
    proxy_connect_timeout 1s;
    proxy_timeout 1m;
    proxy_pass example.com:12345;
}
 
server {
    listen 53 udp;
    proxy_responses 1;
    proxy_timeout 20s;
    proxy_pass dns.example.com:53;
}
 
server {
    listen [::1]:12345;
    proxy_pass unix:/tmp/stream.socket;

2. The proxy_pass directive of the 2.ngx_http_proxy_module module

  • It needs to be used in the location segment, the if segment in location, and the limit_except segment.
  • In addition to the domain name or IP address and port, the protocol, such as "http" or "https", is also required.
  • There is also an optional uri that can be configured.

3. Analysis of the url (request_uri) of the backend server after proxy_pass

server {
    listen 80;
    server_name www.test.com;
 
    # Scenario A
    # Visit http://www.test.com/testa/aaaa
    # The backend request_uri is: /testa/aaaa
    location ^~ /testa/ {
        proxy_pass http://127.0.0.1:8801;
    }
    
    # Scenario B
    # Visit http://www.test.com/testb/bbbb
    # The backend request_uri is: /bbbb
    location ^~ /testb/ {
        proxy_pass http://127.0.0.1:8801/;
    }
 
    # Scenario C
    # The following location is correct ~ /testc {
        proxy_pass http://127.0.0.1:8801;
    }
 
    # Scenario D
    # The following location is wrong#
    # When running nginx -t, the following error will be reported: 
    #
    # nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular 
    # expression, or inside named location, or inside "if" statement, or inside 
    # "limit_except" block in /opt/app/nginx/conf/vhost/test.conf:17
    # 
    # When location is a regular expression, proxy_pass cannot contain a URI part. This example contains "/"
    location ~ /testd {
        proxy_pass http://127.0.0.1:8801/; # Remember, when location is a regular expression, you cannot write it like this! ! !
    }
 
    # Scenario E
    # Visit http://www.test.com/ccc/bbbb
    # The backend request_uri is: /aaa/ccc/bbbb
    location /ccc/ {
        proxy_pass http://127.0.0.1:8801/aaa$request_uri;
    }
 
    # Scenario F
    # Visit http://www.test.com/namea/ddd
    # The backend request_uri is: /yongfu?namea=ddd
    location /namea/ {
        rewrite /namea/([^/]+) /yongfu?namea=$1 break;
        proxy_pass http://127.0.0.1:8801;
    }
 
    # Scenario G
    # Visit http://www.test.com/nameb/eee
    # The backend request_uri is: /yongfu?nameb=eee
    location /nameb/ {
        rewrite /nameb/([^/]+) /yongfu?nameb=$1 break;
        proxy_pass http://127.0.0.1:8801/;
    }
 
    access_log /data/logs/www/www.test.com.log;
}
 
server {
    listen 8801;
    server_name www.test.com;
    
    root /data/www/test;
    index index.php index.html;
 
    rewrite ^(.*)$ /test.php?u=$1 last;
 
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
 
    access_log /data/logs/www/www.test.com.8801.log;
}

This is the end of this article about the difference between the two proxy_pass modules in nginx. For more relevant nginx proxy_pass module 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:
  • Detailed explanation of various usages of proxy_pass in nginx
  • Summary of Nginx location and proxy_pass path configuration issues
  • Detailed explanation of the difference between url ending with / and without / in nginx proxy_pass configuration
  • Proxy_pass method in multiple if in nginx location
  • Implementation of proxy_pass in nginx reverse proxy

<<:  10 very good CSS skills collection and sharing

>>:  Introducing ECharts into the Vue project

Recommend

Summary of the 10 most frequently asked questions in Linux interviews

Preface If you are going to interview for a Linux...

Detailed explanation of JS variable storage deep copy and shallow copy

Table of contents Variable type and storage space...

Detailed example of MySQL (5.6 and below) parsing JSON

MySQL (5.6 and below) parses json #json parsing f...

How to use domestic image warehouse for Docker

1. Problem description Due to some reasons, the d...

Linux installation Redis implementation process and error solution

I installed redis today and some errors occurred ...

Pure CSS3 realizes the effect of div entering and exiting in order

This article mainly introduces the effect of div ...

WeChat applet + ECharts to achieve dynamic refresh process record

Preface Recently I encountered a requirement, whi...

Detailed explanation of mysql record time-consuming sql example

mysql records time-consuming sql MySQL can record...

Implementation of Node connection to MySQL query transaction processing

Table of contents Enter the topic mysql add, dele...

First experience of creating text with javascript Three.js

Table of contents Effect Start creating text Firs...

Introduction to the B-Tree Insertion Process

In the previous article https://www.jb51.net/arti...

HTML table border control implementation code

Generally, when we use a table, we always give it...

A brief discussion on the $notify points of element

My original intention was to encapsulate the $not...

Analysis of problems caused by MySQL case sensitivity

MYSQL is case sensitive Seeing the words is belie...