基于域名的跳转
现在公司旧域名www.wt.com有业务需求变更,需要使用新域名www.dw.com代替,但是旧域名不能废除,需要跳转
到新域名上,而且后面的参数保持不变。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.wt.com #域名修改
charset utf-8
access_log /var/log/nginx/www.wt.com-accsee.log; #日志修改
location / {
#添加域名重定向
if ($host = 'www.wt.com'){ #$host为rewrite全局变量。代表请求主机头字段或主机名
rewrite ^/(.*)$ http://www.dw.com /$1 permanent; #$1为正则匹配的内容,即域名后边的字符串
}
root html;
index index.html index.htm;
}
}
mkdir -p /var/log/ngingx
echo "192.168.153.10 www.wt.com www.dw.com" >> /etc/hosts
systemctl restart nginx
mkdir -p /usr/local/nginx/html/test/
echo 'this ih test web!' > /usr/local/nginx/html/test/1.html
systemctl restart nginx.service
浏览器输入模拟访问 http://www.wt.com/test/1.html
会跳转到www.dw.com/test/1.html,查看元素可以看到返回301.实现了永久重定向跳转,而且域名后的参数也正常跳转。
基于域名客户端 IP 跳转访问
今天公司业务新版本上线,要求所有IP 范围任何内容都显示一个固定维护页面,只有公司IP:192.168.153.10访问正常
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.wt.com #域名修改
charset utf-8
access_log /var/log/nginx/www.wt.com-accsee.log; #日志修改
#设置是否合法的IP标记
set $rewirte true; #设置变量$rewirte,变量值为boole值true
#判断是否为合法IP
if ($remote_addr = "192.168.153.10"{ #当客户端IP为192.168.153.10时,将变量值设为false,不进行重写
set $rewirte false;
}
#除了合法IP,其他都是非法IP,进行重写跳转维护页面
if ($rewrite = true) { #当变量值为true时,进行重写
rewrite (.+) /weihu.html; #重写在访问IP后边插入/weihu.html,例如192.168.153.10/weihu.html
}
location = /weihu.html {
root /var/www/html; #网页返回/var/www/html/weihu.html的内容
}
location / {
root html;
index index.html index.htm;
}
}
mkdir -p /var/www/html/
echo 'weihu!'> /var/www/html/weihu.html
systemctl restart nginx
只有 IP 为 192.168.153.10 能正常访问,其他地址都是维护页面
基于旧域名跳转到新域名后面加目录
现在访问的是http://dw.wt.com,现在需要将这个域名下面的访问都跳转到http://www.wt.com/dw
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name dw.wt.com; #域名修改
charset utf-8;
access_log /var/log/nginx/ www.wt.com-access.log;
#添加
location /post {
rewrite (.+) http://www.wt.com/dw$1 permanent; #这里的$1为位置变量,代表/post
}
location / {
root html;
index index.html index.html;
}
}
mkdir -p /usr/local/nginx/html/dw/post
echo "this is 1.html" >> /usr/local/nginx/html/dw/post/1.html
echo "192.168.153.10 dw.wt.com" >> /etc/hosts
systemctl restart nginx
使用浏览器访问http://dw.wt.com/post/1.html 跳转到http://www.wt.com/dw/post/1.html
基于参数匹配的跳转
访问http://www.wt.com/100-(100|200)-100.html跳转到http//www.wt.com页面。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server name www.wt.com; #域名修改
charset utf-8;
access_ log / var/ log/ nginx/www.wt.com-access.log;
location ~ ^/100\-(100|200)\-(\d)+\.html$ {
rewrite (.+) http://www.wt.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
使用浏览器访问http://www.wt.com/100-200-100.html 或http://www.wt.com/100-100-100.html跳转到
http://www.wt.com页面。
基于目录下所有php结尾的文件跳转
要求访问http://www.wt.com/upload/123.php 跳转到首页。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server name www.wt.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.wt.com-access.log;
location ~* ^/upload/.*\.php$ {
rewrite (.+) http://www.wt.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
浏览器访问http://www.wt.com/upload/123.php 跳转到 http://www.wt.com 页面。
基于最普通一条url请求的跳转
要求访问一个具体的页面如http://www.wt.com/abc/123.html 跳转到首页
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server name www.wt.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.wt.com-access.log;
location ~* ^/abc/123.html {
rewrite (.+) http://www.wt.com permanent;
}
location / {
root html ;
index index.html index.html;
}
}
systemctl restart nginx
浏览器访问http://www.wt.com/abc/123.html 跳转到 http://www.wt.com 页面。