首页 > 系统相关 >nginx 之 proxy_pass详解 切割url

nginx 之 proxy_pass详解 切割url

时间:2024-08-09 18:07:52浏览次数:12  
标签:0.1 http url nginx location pass test proxy

原文链接: https://www.jianshu.com/p/b010c9302cd0

在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。

假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问。

第一种:
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
代理到URL:http://127.0.0.1/test.html

第二种(相对于第一种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1;
}
代理到URL:http://127.0.0.1/proxy/test.html

第三种:
location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html

第四种(相对于第三种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}
代理到URL:http://127.0.0.1/aaatest.html

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

  • ngx_http_proxy_moduleproxy_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的匹配,本文第四部分重点讨论。
  • ngx_stream_proxy_moduleproxy_pass
语法: 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中无效,不管如何设置,都会被忽略。



标签:0.1,http,url,nginx,location,pass,test,proxy
From: https://www.cnblogs.com/fswhq/p/17587195.html

相关文章

  • 如何正确配置 Nginx 来防止任意文件读取攻击?
    限制alias指令的使用:使用alias指令时,确保路径不会导致路径遍历漏洞。避免使用用户输入作为alias的一部分。组合使用 root 和 aliasserver{     root/var/www;     location/static{     alias/var/www/public/static;   ......
  • Nginx教程(一):Windows环境下载Nginx和安装详细教程
    Nginx下载下载地址:nginx:download解压查看配置文件启动Nginx双击nginx.exe文件或者在CMD窗口输入 nginx.exe访问Nginx浏览器访问http://localhost:80,查看是否启动成功 看到Welcometonginx!说明已经启动......
  • windows 编译 openssl + libcurl libcurl库
    主要参考https://blog.icrystal.top/archives/11.htmlopenssl编译网址: https://www.openssl.org/source下载的是3.0.13版本的这边是说Perl:需要Perl来运行OpenSSL的构建脚本。你可以从StrawberryPerl或ActivePerl下载并安装Perl。NASM(可选):如果你要编译支持x86......
  • docker 构建nginx
     1、查看可用的Nginx版本 dockersearchnginx 2、取最新版的Nginx镜像这里我们拉取官方的最新版本的镜像:$dockerpullnginx:latest3、查看本地镜像使用以下命令来查看是否已安装了nginx:$dockerimages 4、运行容器安装完成后,我们可以使用以下命令来运......
  • C#使用HttpUtility,HttpServerUtility、HttpUtility对URL编码、解码
    1、HttpUtility.UrlEncode方法:对URL字符串进行编码,以便实现从Web服务器到客户端的可靠的HTTP传输。重载列表:[1]将字节数组转换为已编码的URL字符。[C#]publicstaticstringUrlEncode(byte[]);[2]对URL字符串进行编码。[C#]publicstaticstringUrlEncode......
  • haproxy的安装和服务信息
    为什么要使用haproxy?因为LSV无后端检测,当webserver有一台状态异常,则运作异常;所以用haproxy来解决。haproxy是一款具备高并发(万级以上)、高性能的TCP和HTTP负载均衡器,它支持基于cookie的持久性,自动故障切换,支持正则表达式及web状态统计。目录一、实验环境二、haproxy基......
  • nginx文件下载防盗链
    文件下载防盗链使用场景客户端发起下载申请--->app服务器(文件上传会生成自己格式的文件名)app服务器返回下载地址--->客户端客户端下载--->web服务器(nginx)nginx配置###开启secure_link模块编译开启--with-http_secure_link_module###配置nginxserver......
  • nginx返回指定数据
    nginx返回指定数据返回json###配置指定路径返回相应json信息location~^/get_info{default_typeapplication/json;return200'{"status":"success","result":"helloworld!"}';}注意:当开发某个接口固定是一个返回值时,可以用此方法返回。节省后端处理过程......
  • nginx状态页面
    开启nginx状态页面安装指定模块nginx-V查看是否安装--with-http_stub_status_module模块#编译安装时添加如下参数./configure--with-http_stub_status_module配置实例server{listen80;server_name127.0.0.......
  • nginx location 和 proxy_pass 带 / 和不带 / 的区别
    nginx服务器地址及端口:127.0.0.1:80后端服务地址及端口:127.0.0.1:8080测试URL:http://127.0.0.1:80/api/upload一nginx配置:location/api/{proxy_passhttp://127.0.0.1:8080/;}实际访问:http://127.0.0.1:8080/upload二nginx配置:location/api{proxy......