Table of Contents
一、proxy_pass的nginx官方指南
二、两个proxy_pass的关系和区别
三、proxy_pass的具体用法
四、proxy_pass后,后端服务器的url(request_uri)情况分析
Home Operation and Maintenance Nginx How to use proxy_pass in Nginx

How to use proxy_pass in Nginx

May 16, 2023 pm 08:38 PM
nginx proxy_pass

How to use proxy_pass in Nginx

一、proxy_pass的nginx官方指南

nginx中有两个模块都有proxy_pass指令。

ngx_http_proxy_module的proxy_pass:

语法: proxy_pass URL;
场景: location, if in location, limit_except
说明: 设置后端代理服务器的•协议(protocol)和地址(address),以及location中可以匹配的一个可选的URI。协议可以是"http"或"https"。地址可以是一个域名或ip地址和端口,或者一个 unix-domain socket 路径。  
详见官方文档: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_passURI的匹配,本文第四部分•重点讨论。
Copy after login

ngx_stream_proxy_module的proxy_pass:

语法: proxy_pass address;
场景: server
说明: 设置后端代理服务器的地址。这个地址(address)可以是一个域名或ip地址和端口,或者一个 unix-domain socket路径。  
详见官方文档: http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_pass
Copy after login

二、两个proxy_pass的关系和区别

在两个模块中,两个proxy_pass都是用来做后端代理的指令。

ngx_stream_proxy_module模块的proxy_pass指令只能在server段使用使用, 只需要提供域名或ip地址和端口。可以理解为端口转发,可以是tcp端口,也可以是udp端口。

ngx_http_proxy_module模块的proxy_pass指令需要在location段,location中的if段,limit_except段中使用,处理需要提供域名或ip地址和端口外,还需要提供协议,如”http”或”https”,还有一个可选的uri可以配置。

三、proxy_pass的具体用法

ngx_stream_proxy_module模块的proxy_pass指令

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;
}
Copy after login

ngx_http_proxy_module模块的proxy_pass指令

server {
   listen      80;
   server_name www.test.com;

   # 正常代理,不修改后端url的   location /some/path/ {
       proxy_pass http://127.0.0.1;
   }

   # 修改后端url地址的代理(本例后端地址中,最后带了一个斜线)   location /testb {
       proxy_pass http://www.other.com:8801/;
   }

   # 使用 if in location   location /google {
       if ( $geoip_country_code ~ (RU|CN) ) {
           proxy_pass http://www.google.hk;
       }
   }

   location /yongfu/ {
       # 没有匹配 limit_except 的,代理到 unix:/tmp/backend.socket:/uri/       proxy_pass http://unix:/tmp/backend.socket:/uri/;;

       # 匹配到请求方法为: PUT or DELETE, 代理到9080       limit_except PUT DELETE {
           proxy_pass http://127.0.0.1:9080;
       }
   }

}
Copy after login

四、proxy_pass后,后端服务器的url(request_uri)情况分析

server {
   listen      80;
   server_name www.test.com;

   # 情形A   # 访问 http://www.test.com/testa/aaaa   # 后端的request_uri为: /testa/aaaa   location ^~ /testa/ {
       proxy_pass http://127.0.0.1:8801;
   }
   
   # 情形B   # 访问 http://www.test.com/testb/bbbb   # 后端的request_uri为: /bbbb   location ^~ /testb/ {
       proxy_pass http://127.0.0.1:8801/;
   }

   # 情形C   # 下面这段location是正确的   location ~ /testc {
       proxy_pass http://127.0.0.1:8801;
   }

   # 情形D   # 下面这段location是错误的   #   # nginx -t 时,会报如下错误:   #   # 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   #   # 当location为正则表达式时,proxy_pass 不能包含URI部分。本例中包含了"/"   location ~ /testd {
       proxy_pass http://127.0.0.1:8801/;   # 记住,location为正则表达式时,不能这样写!!!   }

   # 情形E   # 访问 http://www.test.com/ccc/bbbb   # 后端的request_uri为: /aaa/ccc/bbbb   location /ccc/ {
       proxy_pass http://127.0.0.1:8801/aaa$request_uri;
   }

   # 情形F   # 访问 http://www.test.com/namea/ddd   # 后端的request_uri为: /yongfu?namea=ddd   location /namea/ {
       rewrite    /namea/([^/]+) /yongfu?namea=$1 break;
       proxy_pass http://127.0.0.1:8801;
   }

   # 情形G   # 访问 http://www.test.com/nameb/eee   # 后端的request_uri为: /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;
}
Copy after login

文件: /data/www/test/test.php

echo '$_SERVER[REQUEST_URI]:' . $_SERVER['REQUEST_URI'];
Copy after login

通过查看 $_SERVER[‘REQUEST_URI’] 的值,我们可以看到每次请求的后端的request_uri的值,进行验证。

The above is the detailed content of How to use proxy_pass in Nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve nginx403 How to solve nginx403 Apr 14, 2025 am 10:33 AM

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to solve nginx403 error How to solve nginx403 error Apr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to solve nginx304 error How to solve nginx304 error Apr 14, 2025 pm 12:45 PM

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to solve the problem of nginx cross-domain How to solve the problem of nginx cross-domain Apr 14, 2025 am 10:15 AM

There are two ways to solve the Nginx cross-domain problem: modify the cross-domain response header: add directives to allow cross-domain requests, specify allowed methods and headers, and set cache time. Use CORS modules: Enable modules and configure CORS rules that allow cross-domain requests, methods, headers, and cache times.

How to check the running status of nginx How to check the running status of nginx Apr 14, 2025 am 11:48 AM

The methods to view the running status of Nginx are: use the ps command to view the process status; view the Nginx configuration file /etc/nginx/nginx.conf; use the Nginx status module to enable the status endpoint; use monitoring tools such as Prometheus, Zabbix, or Nagios.

See all articles