0
点赞
收藏
分享

微信扫一扫

Web服务器基础 -- Nginx rewrite 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)


Nginx rewrite 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)

  • ​​一、Nginx 中的正则​​
  • ​​二、Nginx rewrite 案例实战​​
  • ​​1、错误页面重定向​​
  • ​​2、虚拟目录别名重定向​​
  • ​​3、域名跳转​​


本环境是基于 Centos 7.8 系统构建Nginx学习环境

具体构建,请参考 ​​Nginx-1.18.0 环境部署​

Nginx rewrite和 Apache 等 Web 服务软件一样, Nginx rewrite 的主要功能也是实现 URL 地址重写。Nginx的rewrite 规则需要 PCRE 软件的支持, 即通过 Perl 兼容正则表达式语法进行规则匹配。

一、Nginx 中的正则

Nginx 中的正则匹配

  • ~ 与~* 的区别
  • ~ 匹配内容区分大小写
  • ~* 匹配内容不区分的小写
  • !~ 取反
  • ^~ 但多个匹配同时存在,优先匹配 ^~匹配的内容;不做正则表达式的检查 (优先处理)

使用语法

ocation 语法说明表
location 指令的作用是根据用户请求的URI来执行不同的应用。
不同uri及特殊字符组合匹配的顺序说明
location [=||*|^~] uri {

}

location

[=、~ 、*、^]

uri

{…}

指令

匹配标识

匹配的网站地址

匹配URI后要执行的配置段

Web服务器基础 --  Nginx rewrite 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)_nginx

二、Nginx rewrite 案例实战

rewrite 指令结尾的 flag 标记说明

flag 标记符号

说明

last

本条规则匹配完成后, 继续向下匹配新的 location URI 规则

break

本条规则匹配完成即终止, 不再匹配后面的任何规则

redirect

返回 302 临时重定向, 浏览器地址栏会显示跳转后的 URL 地址

permanent

返回 301 永久重定向, 浏览器地址栏会显示跳转后的 URL 地址

Nginx rewrite 的企业应用场景

  • 可以调整用户浏览的URL,看起来更规范,合乎开发及产品人员的需求。
  • 为了让搜索引擎搜录网站内容及用户体验更好,企业会将动态URL地址伪装成静态地址提供服务。
  • 网址换新域名后,让旧的访问跳转到新的域名上。例如,访问京东的360buy.com会跳转到jd.com。
  • 根据特殊变量、目录、客户端的信息进行URL调整等

配置基于Nginx的web服务

[root@node01 ~]# vim /etc/nginx/conf.d/host.conf 
server {
listen 192.168.5.11:80;
server_name news.123.cn;
location / {
root /usr/share/nginx/html/news;
index index.html index.htm;
}
}

[root@node01 ~]# vim /usr/share/nginx/html/news/index.html
news test page...

[root@node01 ~]# systemctl enable --now nginx
[root@node01 ~]# netstat -lnutp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2219/nginx: master

浏览器访问:http://news.123.cn/

Web服务器基础 --  Nginx rewrite 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)_运维_02

1、错误页面重定向

[root@node01 ~]# vim /etc/nginx/conf.d/host.conf 
server {
listen 192.168.5.11:80;
server_name news.123.cn;
location / {
root /usr/share/nginx/html/news;
index index.html index.htm;
if (!-f $request_filename){
rewrite /.* err.html permanent;
}
}
}
[root@node01 ~]# nginx -s reload

[root@node01 news]# echo 'this page is not exists...' > err.html
[root@node01 news]# ll
total 8
-rw-r--r-- 1 root root 27 Feb 22 22:45 err.html
-rw-r--r-- 1 root root 18 Feb 22 14:23 index.html

测试:http://news.123.cn/big_date.jpg

Web服务器基础 --  Nginx rewrite 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)_运维_03

2、虚拟目录别名重定向

[root@node01 bbs]# vim /etc/nginx/conf.d/host.conf 
server {
listen 192.168.5.11:80;
server_name bbs.123.cn;
location / {
root /usr/share/nginx/html/bbs;
autoindex on;
index index.html index.htm;
rewrite ^/virtual_dir/(.*) /first/secoend/web_age/$1 last;
location /nginx_status {
stub_status on;
access_log off;
}
}
}

[root@node01 bbs]# nginx -s reload

[root@node01 bbs]# cd /usr/share/nginx/html/bbs/
[root@node01 bbs]# mkdir first/secoend/web_age -p
[root@node01 bbs]# echo 'this is virtual dir test page...' > /first/secoend/web_age/virtusl_dir.html

测试:http://bbs.123.cn/virtual_dir/virtusl_dir.html

Web服务器基础 --  Nginx rewrite 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)_Web集群_04

3、域名跳转

[root@node01 ~]# vim /etc/nginx/conf.d/host.conf
server {
listen 192.168.5.11:80;
server_name bbs.123.cn;
rewrite .* http://mirrors.aliyun.com/;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
}
}
[root@node01 ~]# nginx -s reload

测试:
浏览器访问:http://bbs.123.cn/

Web服务器基础 --  Nginx rewrite 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)_nginx_05


跳转成功

Web服务器基础 --  Nginx rewrite 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)_html_06

[root@node01 ~]# vim /etc/nginx/conf.d/host.conf 
server {
listen 192.168.5.11:80;
server_name bbs.123.cn;
rewrite .* https://blog.csdn.net/XY0918ZWQ;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
}
}

[root@node01 ~]# nginx -s reload

测试:

浏览器访问:http://bbs.123.cn/

Web服务器基础 --  Nginx rewrite 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)_nginx_05


跳转成功

Web服务器基础 --  Nginx rewrite 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)_Nginx rewrite_08



举报

相关推荐

0 条评论