;
2.5.3 第三个规则就是通用规则
比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器。非静态文件请求就默认是动态请求。
location / {
proxy_pass http://tomcat_server;
三、访问重新rewrite
3.1rewrite的概述
rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。
rewrite只能放在server{},location{},if{}中,并且默认只能对域名后边的除去传递的参数外的字符串起作用。
例如:
http://www.fzr.com/zzj/index.php?id=1&u=str 只对/zzj/index.php重写。
3.2rewrite 执行顺序如下
(1) 执行 server 块里面的 rewrite 指令
(2) 执行 location 匹配
(3) 执行选定的 location 中的 rewrite 指令
语法: rewrite [flag];
regex :表示正则匹配规则
replacement :表示跳转后的内容
flag :表示 rewrite 支持的 flag 标记
###flag标记说明###
last :本条规则匹配完成后,继续向下匹配新的location URI规则,一般用在 server 和 if 中
break :本条规则匹配完成即终止,不再匹配后面的任何规则,一般使用在 location 中
redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址。
四、rewrite 示例
4.1基于域名跳转
4.1.1 基于域名跳转——操作步骤
现在公司旧域名www.fzr.com有业务需求变更,需要使用新域名www.zzj.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.fzr.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.fzr.com-access.log; #日志修改
location / { #添加域名重定向
if ($host = 'www.fzr.com'){ #$host为rewrite全局变量,代表请求主机头字段或主机名
rewrite ^/(.*)$ http://www.zzj.com/$1 permanent; #$1为正则匹配的内容,即域名后边的字符串
root html;
index index.html index.htm;
echo "192.168.10.10 www.fzr.com www.zzj.com" >> /etc/hosts
systemctl restart nginx #重启服务
浏览器输入模拟访问 http://www.fzr.com/test/index.html会跳转到www.zzj.com/test/index.html,查看元素可以看到返回301,实现了永久重定向跳转,而且域名后的参数也正常跳转
4.1.2 实例操作:基于域名跳转
(1)修改主配置文件
(2)重启服务并添加映射关系
(3)创建网页
(4)浏览器中访问测试
4.2基于客户端 IP 访问跳转
4.2.1 基于客户端 IP 访问跳转的操作步骤
要求:今天公司业务新版本上线,要求所有 IP 访问任何内容都显示一个固定维护页面,只有公司 IP:192.168.10.10访问正常
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.fzr.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.fzr.com-access.log; #日志修改
#设置是否合法的IP标记
set $rewrite true; #设置变量$rewrite,变量值为boole值true
#判断是否为合法IP
if ($remote_addr = "192.168.10.10"){ #当客户端IP为192.168.10.10时,将变量值设为false,不进行重写
set $rewrite false;
#除了合法IP,其它都是非法IP,进行重写跳转维护页面
if ($rewrite = true){ #当变量值为true时,进行重写
rewrite (.+) /index.html; #重写在访问IP后边插入/index.html,例如192.168.10.10/index.html
location = /index.html {
root /var/www/html; #网页返回/var/www/html/index.html的内容
location / {
root html;
index index.html index.htm;
mkdir -p /var/www/html/
echo "<h1>正在维护</h1>" > /var/www/html/index.html
systemctl restart nginx
4.2.2 实例操作:基于客户端 IP 访问跳转
(1)修改配置文件
(2)检查配置文件并重启服务
(3)创建跳转后的网页目录和内容
(4)浏览器访问测试
本机访问:
其它主机访问:
现在域名IP地址做映射:
然后再访问:
4.3基于旧域名跳转到新域名后面加目录
4.3.1基于旧域名跳转到新域名后面加目录的操作步骤
现在访问的是 http://bbs.fzr.com/test,现在需要将这个域名下面的访问都跳转到http://www.fzr.com/bbs/test
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name bbs.fzr.com; #域名修改
charset utf-8;
access_log /var/log/nginx/bbs.fzr.com-access.log;
location /test {
rewrite (.+) http://www.fzr.com/bbs$1 permanent; #这里的$1为位置变量,代表/test
location / {
root html;
index index.html index.htm;
mkdir -p /usr/local/nginx/html/bbs/post
echo "this is web" >> /usr/local/nginx/html/bbs/post/1.html
echo "192.168.10.10 bbs.fzr.com www.fzr.com" >> /etc/hosts
systemctl restart nginx
4.3.2实例操作:基于旧域名跳转到新域名后面加目录
(1)修改主配置文件
(2)重启服务并创建网页文件
(3)添加映射关系并使用浏览器访问测试
本机测试:
4.4基于参数匹配的跳转
4.4.1 基于参数匹配的跳转的步骤
访问http://www.fzr.com/100-(100|200)-100.html 跳转到http://www.fzr.com页面
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.fzr.com; #域名修改
charset utf-8;
access_log /var/log/nginx/fzr.kgc.com-access.log;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.*) http://www.fzr.com permanent;
location / {
root html;
index index.html index.htm;
systemctl restart nginx
解释:
$request_uri: 包含请求参数的原始URI,不包含主机名,如: http://www.fzr.com/abc/bbs/index.html?a=1&b=2中的/abc/bbs/index.php?a=1&b=2
$uri:这个变量指当前的请求URI,不包括任何参数,如: /abc/bbs/index.html
$document_uri: 与$uri相同, 这个变量指当前的请求URI,不包括任何传递参数,如:/abc/bbs/index.html
用浏览器访问 http://www.fzr.com/100-200-100.html 或 http://www.fzr.com/100-100-100.html 跳转到http://www.fzr.com页面
4.4.2 实例操作:基于参数匹配的跳转
(1)修改配置文件
(2)浏览器访问测试
4.5基于目录下所有 php 结尾的文件跳转
要求访问 http://www.fzr.com/upload/123.php 跳转到首页。
4.5.1 基于目录下所有 php 结尾的文件跳转的操作步骤
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.fzr.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.fzr.com-access.log main;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.fzr.com permanent;
location / {
root html;
index index.html index.htm;
systemctl restart nginx
浏览器访问 http://www.fzr.com/upload/888.php 跳转到http://www.fzr.com页面。
4.5.2 实例操作:基于目录下所有 php 结尾的文件跳转
(1)修改配置文件
(2)浏览器访问测试
4.6基于最普通一条 url 请求的跳转
要求访问一个具体的页面如 http://www.fzr.com/abc/888.html 跳转到首页
4.6.1 基于最普通一条 url 请求的跳转的操作步骤
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.fzr.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.fzr.com-access.log;
location ~* ^/abc/888.html {
rewrite (.+) http://www.fzr.com permanent;
location / {
root html;
index index.html index.htm;
systemctl restart nginx
浏览器访问 http://www.fzr.com/abc/888.html 跳转到http://www.fzr.com页面。
4.6.2 实例操作:基于最普通一条 url 请求的跳转
(1)修改配置文件
(2)浏览器访问测试