nginx之重写
rewrite可以写在server段、location段和if段。语法:
rewrite regexp replacement [flag]
flag是标记。有4种标记,它们的作用如下表。
| flag | 说明 |
|---|---|
| last | 停止处理当前上下文中的其他重写模块指令,并为重写后的uri再次进行上下文的匹配 |
| break | 和break指令一样,都是停止处理当前上下文中的其他重写模块指令 |
| redirect | 返回临时重定向状态码302。当replacement部分不是以"http://"或者"https://"或者"$schema"开头的时候使用,"$schema"变量表示使用的是什么协议 |
| permanent | 返回永久重定向状态码301 |
注意:
last和break用来实现URL改写,此时浏览器中的地址不会改变,但实际上在服务器上访问的资源和路径已经改变了。
redirect和permanent用来实现URL跳转,浏览器中的地址会改变为跳转后的地址。
在使用proxy_pass指令时要使用break标记。last标记在本条rewrite规则执行完后,继续在当前上下文对重写后的地址发起匹配请求,而break则在本次匹配完成后停止再次匹配。
例如下面的两条重写规则。
rewrite "^/bbs/(.*)/images/(.*)\.jpg$" www.linuxidc.com/bbs/$2/images/$1.jpg last;
rewrite "^/bbs/(.*)/images/(.*)\.jpg$" www.linuxidc.com/bbs/$2/images/$1.jpg break;
如果访问的是www.linuxidc.com/bbs/a/images/b.jpg则重写后为www.linuxidc.com/bbs/b/images/a.jpg,
但是重写后的地址仍然可以匹配到规则^/bbs/(.*)/images/(.*)\.jpg$,此时如果使用last标记,则会再次进行重写,最终导致URL重写循环,nginx默认支持10次循环,然后返回500状态码。
而如果使用break标记,则在重写完成后不会再次匹配重写。
例子1. 在server字段中写rewrite,使得任意以linuxidc.com结尾的访问重定向到www.linuxidc.com。
server_name www.linuxidc.com;
rewrite (.*).linuxidc.com www.linuxidc.com permanent;
例子2. 在location字段中rewrite,使得localhost/bbs/*的访问都重定向到localhost/forum/*。
server {
listen 80;
server_name localhost;
location /bbs {
root html/;
index index.html;
rewrite "/bbs/(.*)" "/forum/$1" last;
}
}
可以测试一下,比如nginx安装目录为:/usr/local/nginx,那么在/usr/local/nginx/html中新建2个文件夹,bbs和forum,并且分别在这两个文件夹中都建立文件1.txt,分别写入
hello, bbs
hello, forum
然后访问http://localhost/bbs/1.txt, 可以看到返回hello, forum。 表示重写成功
附加一个基于openrestry的https方案 https://www.cnblogs.com/shihuc/p/7150900.html
nginx之重写的更多相关文章
- 很不错的NGINX URL重写实例
转自:http://www.jbxue.com/article/2187.html 本文介绍nginx URL重写的相关知识,包括301重定向的内容等,希望对大家有所帮助. nginx rewrite ...
- nginx url 重写 [转]
本文转自 http://www.jbxue.com/article/2187.html 本文介绍nginx URL重写的相关知识,包括301重定向的内容等,希望对大家有所帮助. nginx rewri ...
- Nginx URL重写(rewrite)配置及信息详解
URL重写有利于网站首选域的确定,对于同一资源页面多条路径的301重定向有助于URL权重的集中 Nginx URL重写(rewrite)介绍 和apache等web服务软件一样,rewrite的组要功 ...
- web服务器-Nginx URL重写
web服务器-Nginx URL重写 一. URL重写介绍 和apache等web服务软件一样,rewrite的主要功能是实现URL地址的重定向.Nginx的rewrite功能需要PCRE软件的支持, ...
- nginx url重写 rewrite实例
本文介绍下,在nginx中实现Url重写,学习rewrite的具体用法,有需要的朋友参考下吧. 原文地址:http://www.360doc.com/content/14/0202/20/142341 ...
- nginx URL重写
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- nginx rewrite重写
通过官方文档可以看到,rewrite的作用上下文是 server location,可以写在 server里面 亦或location里面; 命令: if (条件) {} 条件判断 set #设置 ...
- 16、Nginx Rewrite重写
1.Rewrite基本概述 1.1.什么是rewrite Rewrite主要实现url地址重写, 以及地址重定向,就是将用户请求web服务器的地址重新定向到其他URL的过程. 1.2.Rewrite使 ...
- nginx rewrite重写与防盗链配置
nginx rewrite重写规则与防盗链配置方法 时间:2016-02-04 15:16:58来源:网络 导读:nginx rewrite重写规则与防盗链配置方法,rewrite规则格式中flag标 ...
随机推荐
- Android提交自己的作品到GitHub上
最近在做一个期待上架的我个人写的App,我打算将它开源出去,托管到GitHub上.看了一下网上的教程,其实五花八门,我照着做了,还是没法提交到GitHub上.自己研究了一下,其实非常的简单.这里决定介 ...
- Python next() 函数
Python next() 函数 Python 内置函数 描述 next() 返回迭代器的下一个项目. 语法 next 语法: next(iterator[, default]) 参数说明: ite ...
- CTC 的工作原理
CTC 的工作原理 Fig. 1. How CTC combine a word (source: https://distill.pub/2017/ctc/) 这篇文章主要解释CTC 的工 ...
- JS 语法大全
来源http://blog.csdn.net/newegg2009/article/details/6230582 js语法2008年03月19日 星期三 11:14一.js的数据类型和变量 Java ...
- MySqlDBHelper
代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...
- asp.net 自定义节配置 (configSections下的section)
<configuration> <configSections> <!--.自定义一个节 CustomSection --> <section name=&q ...
- mybatis Generator 生成配置文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE generatorConfiguration ...
- ios9出现的问题
升级后需要注意两个地方 1 在build Settings 搜索bitcode 设置成no 2 在info.plist里添加以下属性 程序中报错: App Transport Security ...
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...
- [label][翻译][JavaScript-Translation]七个步骤让你写出更好的JavaScript代码
7 steps to better JavaScript 原文链接: http://www.creativebloq.com/netmag/7-steps-better-javascript-5141 ...