首页 > 系统相关 >Nginx 6个例子理解proxy_pass和rewrite的用法

Nginx 6个例子理解proxy_pass和rewrite的用法

时间:2023-06-26 20:00:52浏览次数:50  
标签:http rewrite break Nginx location pass proxy

Nginx 6个例子理解proxy_pass和rewrite的用法
一、rewrite描述
rewrite 可以重写path,也可以重写整个url(如果存在协议,默认返回302临时跳转,即使加了 last 和 break 也无效)。

rewrite 共有4种flag:last、break、redirect(302)、permanent(301)。

当location 中存在flag时,不会再执行之后的 rewrite 指令集(包括 rewrite 和 return)。

break 和 last 作用相反,break 中止对其它 location 的规则匹配,last 继续向其它 location 进行规则匹配。

当location中存在 rewrite 时,若要使proxy_pass生效, 须和 break 一起使用,否则proxy_pass将被跳过。

与 rewrite 同时存在时,proxy_pass 中的 path 不会替换。

二、proxy_pass描述
proxy_pass 重写的 url 中包含 path 时,会替换 location 块的匹配规则。
proxy_pass 中不含path时,不会发生替换。
三、举例说明
例1 break标记

server {
  listen 9000;
  server_name localhost;

  location /info {
    rewrite ^/.* https://baidu.com permanent;
  }

  location /break {
    rewrite /.* /info break;
    proxy_pass http://127.0.0.1:9000;
    # 此 return 不会执行
    return 200 "ok";
  }
}

输入:http://localhost:9000/break

执行过程:首先会匹配到 /break 的 location 块,执行了 rewrite 和 proxy_pass之后,跳过 return(因为有 break),重定向到 http://127.0.0.1:9000/info;然后再次进入 server 块,匹配到 /info 的 location 块,最终重定向到了baidu。

总结:两次进入 server

例2 break命令

server {
  listen 9000;
  server_name localhost;

  location /info {
    rewrite ^/.* https://www.baidu.com permanent;
  }

  location /break {
    rewrite /.* /info;
    break;
    proxy_pass http://127.0.0.1:9000;
    # 该 return 不执行
    return 200 "ok";
  }
}

输入:http://localhost:9000/break

执行过程:首先会匹配到 /break 的 location 块,执行了 rewrite 和 proxy_pass,跳过 return(因为有 break),重定向到 http://127.0.0.1:9000/info;然后,再次进行 server 块,匹配到 /info 的 location 块,最后重定向到了baidu。

注意:proxy_pass 最后不要写成http://127.0.0.1:9000/,应去掉最后的斜杠,而例子(1)的写法可加斜杠,也可不加斜杠。

总结:两次进入 server

例3 last标记

server {
  listen 9000;
  server_name localhost;

  location /info {
    rewrite ^/.* https://www.baidu.com permanent;
  }

  location /break {
    rewrite /.* /info last;
    # 该 proxy_pass 不执行
    proxy_pass http://127.0.0.1:9000;
    # 该 return 不执行
    return 200 "ok";
  }

}

输入:http://localhost:9000/break

执行过程:首先会匹配到 /break 的 location 块,执行了 rewrite,跳过 return 和 proxy_pass(因为有 last,proxy_pass 需要和 break 一起用);然后继续匹配,匹配到 /info 的 location 块,最后重定向到了baidu。

总结:一次进入 server,两次 location 匹配

例4 包含proxy_pass包含path

location /api/ {
  proxy_pass http://127.0.0.1/proxy/;
}

访问http://example.com/api/data 会被代理到 http://127.0.0.1/proxy/data

location /api/ {
  proxy_pass http://127.0.0.1/proxy;
}

因为包含proxy_pass包含path, 所以发生了替换
http://example.com/api/data 会被代理到 http://127.0.0.1/proxydata

location /api/ {
  proxy_pass http://127.0.0.1/;
}

因为包含proxy_pass包含path, 所以发生了替换
http://example.com/api/data 会被代理到 http://127.0.0.1/data

例5 proxy_pass 中不含path

location /api/ {
  proxy_pass http://127.0.0.1;
}

proxy_pass 中不含path时,则不会发生替换
http://example.com/api/data 会被代理到 http://127.0.0.1/api/data

例6 proxy_pass与 rewrite 同时存在时

location /api/ {
  rewrite /api/(.*) /info/$1 break;
  proxy_pass http://127.0.0.1/proxy/;
}

与 rewrite 同时存在时,proxy_pass 中的 path 不会替换,相当于不起作用
http://example.com/api/data 会被代理到 http://127.0.0.1/info/data

标签:http,rewrite,break,Nginx,location,pass,proxy
From: https://www.cnblogs.com/xzs603/p/17506591.html

相关文章

  • 基于docker部署nginx
    基于docker部署nginx1.拉取nginx镜像文件dockerpullnginx2.创建nginx本地映射文件mkdir-p/root/docker/nginx/logsmkdir-p/root/docker/nginx/conf.dtouchpinter.confvipinter.conf把pinter改为docker中的别名3.启动nginx服务dockerrun-d-p80:80--networkmtx-v/ro......
  • nginx RTMP推拉流,多个音频流合并。
    使用nginxRTMP(nginx的一个插件模块,具体的网上搜一下)做一个简易的多人音频通话流媒体服务器,多个端通话时,客户端无法处理其他端发过来的音频流,比如A、B、C三个端通话,A设备同时持有B、C的音频流,这样对设备端非常的不友好。这时候就需要用到一个强大的工具,FFMPEG,安装网上很多,搜下就......
  • nginx 1.25.1 发布
    nginx1.25.1有一个很不错的特性,就是支持了http2指令,以前这个指令主要是也listen配置使用的(ssl+http2场景)独立指令之后就有了很方便的功能了,比如有些业务希望使用http0.9-1.1协议,有些需要使用http2,当然目前也是支持了http3的,可以做到分离,以前版本存在一个问题就是开启了之......
  • vue-router之hash与history,以及nginx配置
    本篇讲解前端项目的路由模式(以vue-router为例),以及history模式下的项目部署问题。vue-router的路由模式可以通过指定mode属性值控制,可选值:"hash"、"history"、"abstract",默认:"hash"(浏览器环境),"abstract"(Node.js环境)constrouter=newVueRouter({mode:......
  • Nginx配置max_fails fail_timeout 不起作用 - stub_status - 调试 nginx --with-deb
    0.stub_statusconfigurearguments:--prefix=/usr/local/tengine--with-http_realip_module--with-http_gzip_static_module--with-pcre--with-http_stub_status_module--with-http_ssl_module--add-module=/opt/nginx-goodies-nginx-sticky-module-ng[root@slave1con......
  • Nginx-PHP优化设置 + lnmp调优的关键影响因素 + php-fpm + nginx返回码 + tcp调优 +
    最大文件描述符Linux内核本身有文件描述符最大值的**,你可以根据需要更改:系统最大打开文件描述符数:/proc/sys/fs/file-max临时性设置:echo1000000>/proc/sys/fs/file-max永久设置:修改/etc/sysctl.conf文件,增加fs.file-max=10000002、用户级设置vi/etc/security/limits.confhttp......
  • elk 入门 - 分析nginx日志 + json格式 + 有调试的意识 + elk7.2.0
    1.本次采用的一台主机,将所有的软件安装一台上进行测试工作。2.安装部署:https://blog.51cto.com/hwg1227/22999953.简单调试输出rubydebuginput{file{path=>"/usr/local/log_test/*/*/*.log"start_position=>"beginning"}}output{e......
  • 配置文件 proxy_set_header -发往后端服务器的请求头---- nginx日志设置级别调试技巧
    http{includemime.types;default_typeapplication/octet-stream;sendfileon;#tcp_nopushon;#keepalive_timeout0;keepalive_timeout65;server_tokensoff;log_formatmain'$remote_addr-$remote_user[$time_local]......
  • Nginx 根据请求参数代理到指定网址
    1、参考nginx中将某一个请求的路径重定向到其它网址nginx:[emerg]unknowndirective"if($request_uri"in2、小坑if与($间需要有空格。判断请求路径中,是否包含/adminif($request_uri~'/admin'){return404;}3、配置location/xxxxx/client/register{......
  • MacOs Docker nginx.conf 配置
    `server{listen8089;server_namelocalhost;client_max_body_size120M;#php容器项目地址root/var/www/html/shop;indexindex.phpindex.htmlindex.htm;location/{#try_files$uri$uri//index.php?s=$uri&$args;}......