nginx之proxy_pass指令完全拆解

news/2024/7/7 21:44:37 标签: nginx, proxy_pass

一、proxy_passnginx官方指南

nginx中有两个模块都有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_pass
URI的匹配,本文第四部分重点讨论。
语法: proxy_pass address;
场景: server
说明: 设置后端代理服务器的地址。这个地址(address)可以是一个域名或ip地址和端口,或者一个 unix-domain socket路径。  
详见官方文档: http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_pass

二、两个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;
}

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;
        }
    }

}

四、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;
}

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

<?php
echo '$_SERVER[REQUEST_URI]:' . $_SERVER['REQUEST_URI'];

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

小结

情形A和情形B进行对比,可以知道proxy_pass后带一个URI,可以是斜杠(/)也可以是其他uri,对后端request_uri变量的影响。
情形D说明,当location为正则表达式时,proxy_pass不能包含URI部分。
情形E通过变量($request_uri, 也可以是其他变量),对后端的request_uri进行改写。
情形F和情形G通过rewrite配合break标志,对url进行改写,并改写后端的request_uri。需要注意,proxy_pass地址的URI部分在情形G中无效,不管如何设置,都会被忽略。


http://www.niftyadmin.cn/n/1869635.html

相关文章

某些error page不加载_Firefox 登录feedly时出现PR_CONNECT_RESET_ERROR的解决方案

我在将ubuntu从14升级到18之后&#xff0c;之前可以正常访问的http://feedly.com网站却打不开了。打开时出现的错误信息显示PR_CONNECT_RESET_ERROR。判断是否为feedly网站的问题在终端上ping一下http://feedly.com网站&#xff0c;发现可以正常ping通。初步判断是自身firefox问…

ES6之解构赋值用途

1.交换变量的值 let x1; let y2; [x,y] [y,x];2.从函数返回多个值 函数只能返回一个值&#xff0c;如果要返回多个值&#xff0c;只能将它们放在数组或对象里返回。有了解构赋值&#xff0c;取出这些值就非常方便 //返回一个数组 function example() {return [1,2,3]; } let…

eclipse和idea代码通用吗_Python 工匠:写好面向对象代码的原则(中)

文章来源于piglei &#xff0c;作者piglei在这篇文章中&#xff0c;我将继续介绍 SOLID 原则的第三位成员&#xff1a;L(里氏替换原则)。里氏替换原则与继承在开始前&#xff0c;我觉得有必要先提一下 继承(Inheritance)。因为和前面两条非常抽象的原则不同&#xff0c;“里氏替…

阿里巴巴开源项目:分布式数据库同步系统otter(解决中美异地机房)

项目背景 阿里巴巴B2B公司&#xff0c;因为业务的特性&#xff0c;卖家主要集中在国内&#xff0c;买家主要集中在国外&#xff0c;所以衍生出了杭州和美国异地机房的需求&#xff0c;同时为了提升用户体验&#xff0c;整个机房的架构为双A&#xff0c;两边均可写&#xff0c;由…

Tomcat配置与优化(内存、并发、管理)

一、JVM内存配置优化 在开发当中&#xff0c;当一个项目比较大时&#xff0c;依赖的jar包通常比较多&#xff0c;我们都知道&#xff0c;在应用服务器启动时&#xff0c;会将应用引用到的所有类通过ClassLoader依次全部加载到内存当中。Java的逻辑内存模型大致分为堆内存、栈内…

vue学习之vm.$attrs与inheritAttrs的简单理解

问题背景 文档的这段话什么意思&#xff1f; 简单理解来就是父子孙…组件传值用的 简单例子如下&#xff1a; 父组件 子组件 上图中&#xff0c;父组件有两个属性分别是title和message,需要传递给子组件&#xff0c;而子组件接受父组件传来的值是需要props的对吧&#xff0c…

vue路由的scrollBehavior的使用

问题背景 当我们从A页面跳转到B页面&#xff0c;然后在B页面浏览一篇很长的文章&#xff0c;但是这时候我们讲页面滚动到某个位置的时候&#xff0c;我们不小心按了一下浏览器的后退键&#xff0c;这时候我们再按浏览器的前进键的时候会发现&#xff0c;该文章又要从头开始阅读…

在python中获得当前工作路径的方法是_python-获取当前工作路径

1.sys.argv[0] import sys print sys.argv[0]#获得的是当前执行脚本的位置&#xff08;若在命令行执行的该命令&#xff0c;则为空&#xff09; 运行结果&#xff08;在python脚本中执行的结果&#xff09;&#xff1a; F:/SEG/myResearch/myProject_2/test.py 2.os模块 import…