Article Directory
First, prepare the environment
Two, Rewrite introduction
2.1, Rewrite jump scene
2.2, Rewrite practical scene
2.3, commonly used regular expression metacharacters
2.4, Rewrite command
2.5, location classification
2.6, location priority
Third, the specific scene
3.1, Scene One: name-based Jump
3.2, Scene 2: based on the client IP address to access the Jump
3.3, Scene Three: based on the old, the new domain name and add a table of contents to jump
3.4, Scene Four: Jump-based parameter matching
3.5, Scene Five: based directory of all the php file Jump
First, prepare the environment
A nginx web server provides the www.accp.com.
1, the installation source rpm
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2, and is mounted directly bind with nginx yum
yum install nginx bind -y
3, DNS name resolution
vim /etc/named.conf
vim /etc/named.rfc1912.zones
##复制添加一段
zone "accp.com" IN {
type master;
file "accp.com.zone";
allow-update { none; };
};
cd /var/named
cp -p named.localhost accp.con.zone
vim accp.con.zone
4, modify configuration files nginx
vim /etc/nginx/conf.d/default.conf
5, start the service, turn off the firewall
systemctl stop firewalld.service
setenforce 0
systemctl start named
systemctl start nginx
Two, Rewrite introduction
2.1, Rewrite jump scene
URL looks more standardized and reasonable;
Dynamic URL address of the company will be disguised as a static address to provide services;
After the Web site domain name change, let the old access jump to the new domain name;
Server and certain operational adjustments
2.2, Rewrite practical scene
1, Nginx jumps implementation needs:
Use rewrite matching jump;
If the matching using global variables to go;
Use location and then jump match
2, rewrite on the server {}, if {}; location {} in section;
3, the domain name or parameter string:
Use match if global variables;
Use reverse proxy proxy_pass
2.3, commonly used regular expression metacharacters
2.4, Rewrite command
grammar:
flag label Definitions:
last break and compare:
2.5, location classification
classification:
location = patt {} [precise match]
location patt {} [General match]
location ~ patt {} [regular match]
Common regular expression matching:
2.6, location priority
Expression of the same type, the string length of the matching priority;
Arranged by priority:
Type =
^ ~ Type expression
Regular Expressions (~ and ~ *) type
Conventional string matching type press prefix matching
General match (/), if no other matches, any requests are matched to the
Third, the specific scene
3.1, Scene One: name-based Jump
Experimental environment: old company domain name www.accp.com, because business needs have changed, you need to use the new domain name www.newaccp.com instead.
demand:
We can not repeal the old domain
Jump from the old domain to the new domain name and its parameters remain unchanged
1, modify configuration files nginx
vim /etc/nginx/conf.d/default.conf
//添加一段
if ($host = 'www.accp.com') {
rewrite ^/(.*)$ http://www.newaccp.com/$1 permanent;
}
//域名重定向:就是当访问www.wang.com时,将激动跳转到www.new.wang.com域名。
//permanent:表示永久的意思。
2、DNS服务提供新域名的解析
vim /etc/named.rfc1912.zones
//复制之前的accp域名声明段修改
cd /var/named
cp -p accp.com.zone newaccp.com.zone
3、重启服务
systemctl stop nginx
systemctl start nginx
systemctl restart named
验证:在win10的浏览器中输入新域名www.accp.com
3.2、场景二:基于客户端IP地址访问跳转
实验要求:今天公司业务版本上线,所有IP访问任何内容都显示一个固定维护页面,只有公司的IP才能访问正常。
公司IP地址:192.168.111.146
PC客户端:192.168.111.140
把上一个实验的nginx配置部分删除,以防影响下面的实验。
1、修改nginx的配置文件,重启服务
vim /etc/nginx/conf.d/default.conf
listen 80;
server_name www.accp.coom;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
#设置是否合法的IP标志
set $rewrite true;
#判断是否为合法IP
if ($remote_addr = "192.168.111.146"){
set $rewrite false;
}
#非法IP进行判断打上标记
if ($rewrite = true){
rewrite (.+) /main.html;
}
#匹配标记进行跳转站点
location = /main.html {
root /usr/share/nginx/html;
}
systemctl stop nginx
systemctl start nginx
2、给 main.html 添加自定义页而内容
cd /usr/share/nginx/html
vim main.html
<html>
<head>
<meta charset="utf-8">
<title>test网站</title>
</head>
<body>
<h1>网站维护中,请稍等~~~</h1>
</body>
</html>
systemctl restart nginx
用公司的IP地址访问:
通过客户端IP地址访问:
3.3、场景三:基于旧、新域名跳转并加目录
例如:现在访问的是 http://bbs.accp.com ,现在需要将这个域名下面的发帖都跳转到 http://www.accp.com/bbs ,注意保持域名跳转后的参数不变。
1、在nginx配置文件中添加以下代码
vim /etc/nginx/conf.d/default.conf
listen 80;
server_name bbs.accp.coom;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log;
#添加一段
location /post {
rewrite (.+) http://www.accp.com/bbs$1 permanent;
}
2、修改域名,重启服务
cd /var/named
vim accp.com.zone #把里面的 www 换成 bbs 不然无法解析。
systemctl restart nginx
systemctl restart named
echo "nameserver 192.168.111.145" > /etc/resolv.conf
3、在浏览器上访问 http://bbs.accp.com/post/a.html ,会帮我们自动跳转 http://www.accp.com/bbs/post/a.html , 此时域名跳转后的参数并没有变还是bbs
3.4、场景四:基于参数匹配跳转
例如:浏览器访问http://www.accp.com/100-(100|200)-100.html,会自动跳转到 http://www.accp.com 的页面。
1、修改nginx的配置文件,添加以下代码
listen 80;
server_name www.accp.coom;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
## 添加一段
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.*) http://www.accp.com permanent;
}
server_name www.accp.com;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.*) http://www.test.com permanent;
}
123456
2, DNS resolve www. And restart the service
cd /var/named
vim accp.com.zone
systemctl restart nginx
systemctl restart named
3, browser access http://www.accp.com/100-100-100.html, will help us to automatically jump to www.accp.com website
3.5, Scene Five: based directory of all the php file Jump
For example, we visit http://www.accp.com/upload/1.php, will automatically jump to home www.accp.com .
1, modify nginx configuration file, add the following code
vim /etc/nginx/conf.d/default.conf
listen 80;
server_name www.accp.coom;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
## 添加
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.accp.com permanent;
}
2, restart the service
systemctl restart nginx