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

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

时间:2022-12-21 23:35:01浏览次数:39  
标签:Web node01 重定向 index nginx html Nginx 跳转 root


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 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 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)_nginx_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 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)_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 案例实战(错误页面重定向、虚拟目录别名重定向、域名跳转)_nginx_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



标签:Web,node01,重定向,index,nginx,html,Nginx,跳转,root
From: https://blog.51cto.com/u_14904176/5960225

相关文章

  • Javaweb 登陆与验证码
    本次记录分角色登陆以及验证码的Servlet。1.登陆验证<html><%--CreatedbyIntelliJIDEA.User:jiachenglinDate:2022/11/11Time:14:31Tochangethis......
  • CTFSHOW_菜狗杯_WEB
    web签到<?php/*#-*-coding:utf-8-*-#@Author:h1xa#@Date:2022-11-1017:20:38#@LastModifiedby:h1xa#@LastModifiedtime:2022-11-1109:38:59......
  • 实验八-Web部署
    实验八-Web部署部署过程配置华为云服务器安装程序dnfinstallhttpdmysql-serverphpphp-mysqlndphp-fpm启用Apachesystemctlstarthttpd.servi......
  • 响应式Web设计基础
     1、响应式Web简介响应式Web设计是和HTML5+CSS3互相配合与支持的,技术点包括:(1)HTML5+CSS3:HTML5+CSS3为基本网页设计增添了新的标签与属性内容。(2)HTML5的视口(viewport):提供......
  • webstorm typescript .d.ts文件 使用问题
    问题描述 webstorm中global.d.ts文件当全局变量用,不行。直接上干货,能对上你的问题就恭喜了。global.d.ts内容  tsconfig.json   直接使用,不用导入  ......
  • Go语言使用场景 | go语言与其它开源语言比较 | Go WEB框架选型
     一、Go语言使用场景1.关于go语言2007年,受够了C++煎熬的Google首席软件工程师RobPike纠集RobertGriesemer和KenThompson两位牛人,决定创造一种新语言来取代C++,......
  • ​WEB漏洞渗透测试靶场 资源整理
    整理了一些WEB漏洞本地靶场资源,包括针对性的漏洞专题如SQL注入,XSS等…以及综合类的;1.测试靶场列表2.测试靶场简介 ......
  • FortiWeb 策略数量限制解决方案
    1.背景由于我司FortiWeb设备安全防护策略有数量的限制,只允许新建256条策略,对我司根据每一个二级域名匹配一条安全策略是远远不够的。​​https://www.fortinet.com/conten......
  • 小程序开发与web开发的区别及特殊功能实现
    小程序开发整理使用uni-app跨端开发框架,代码写法与vue2一致。一、与web开发的区别1.运行方式不同npmrundev:mp-weixin后,用微信开发者工具打开dist中工程。2.标......
  • 【php】swoole中websoket创建在线聊天室
    swoole中websoket创建在线聊天室(php)swoole现仅支持Linix,macos创建websocket服务器首先现在服务器创建一个websocket服务器<?php//创建websocket服务器$server=new......