处理静态资源:nginx、apache、Lighttpd
处理动态资源:tomcat(java语言编写)、php(php语言编写)、python(python语言编写)
nginx网站服务软件:
(1)nginx服务程序能力强大: 支持更高并发访问(静态资源小文件),占用资源少(内存)
(2)nginx服务程序功能强大: web服务器 负载均衡服务器 缓存服务器(用户访问图片---web服务器(图片资源缓 存)---nfs)
apache-select(高并发能力较弱) vs nginx-epoll(高并发能力较强)
使用的网络模型: select epoll
宿管阿姨:
select宿管: 一个一个房间找人 ---遍历过程
epoll 宿管: 人员登记表 ---快速索引调取数据
幼儿园阿姨:
select阿姨: 一个一个询问上厕所 ---遍历过程
epoll阿姨: 画个圈,检查圈里是否有人想上厕所---回调机制
apache-select和nginx-epoll的技术对比php
指标 | select | epoll |
---|---|---|
性能 | 随着链接数的增长性能急剧降低。处理成千上万的并发链接数,性能不好 | 随着链接数的增长,性能基本上没有降低。处理成千上万链接时性能很好 |
链接数 | 链接数有限制,处理的最大链接数不超过1024,若是要处理的链接数超过1024个,则须要修改FD_SETSIZE宏,并从新编译 | 链接数无限制 |
内在处理机制 | 线性轮询 | 回调callback |
开发复杂性 | 低 | 中 |
Nginx 是一个开源的,支持高性能、高并发的 WWW服务器和代理服务软件。它是由俄罗斯人 Igor Sysoev开发的,最初被应用在俄罗斯的大型网站 www.rambler.ru 上,后来做者将源代码以类BSD许可证的形式开源出来供全球使用。
Nginx因具备高并发(特别是静态资源)占用系统资源少等特性,且功能丰富而逐渐流行起来。
在功能应用发面,Nginx不可是一个优秀的Web服务软件,还具备反向代理负载均衡功能和缓存服务功能。在反向代理负载均衡功能方面,它相似于大名鼎鼎的LVS负载均衡及Haproxy等专业代理软件,可是Nginx部署起来更为简单、方便;在缓存服务功能方面,它又相似于Squid等专业的缓存服务软件。
Nginx 能够运行在 UNIX、Linux、BSD、Mac 0S X、Solaris,以及 Microsoft Windows 等操做系统中。随着Nginx在国内不少大型网站中的稳定高效运行,近两年它也逐渐被愈来愈多的中小型网站所使用。当前流行的Nginx Web组合被称为LNMP或LEMP(即Linux Nginx MySQL PHP),其中 LNMP 里的 N 取自Nginx
Nginx 的官方介绍见 http://nginx.org
html
(1)支持高并发特性
(2)资源消耗少特性
(3)有负载代理特性
(4)有数据缓存特性
(5)有异步网络特性
java
(1)支持网站页面请求处理功能
(2)支持反向代理负载均衡功能
(3)支持前端业务数据缓存功能
node
(1)yum安装:历史稳定版本 python
[root@web03 ~]#yum install -y nginx #### rpm -e --nodeps nginx 卸载nginx
(2)官方源安装:最新稳定版本
修改yum源nginx
[root@web03 ~]#vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key
下载nginxweb
[root@web03 ~]#yum install -y nginx
(3)编译安装:灵活安装软件
编译安装:将源代码变为机器可执行的代码文件再将执行文件安装到操做系统里,才可使用
下载nginx源码包正则表达式
[root@web03 ~]#cd /server/tools [root@web03 ~]#wget http://nginx.org/download/nginx-1.16.0.tar.gz
解决软件依赖shell
[root@web03 ~]#yum install -y pcre-devel openssl-devel # pcre-devel: perl语言兼容正则表达式 # openssl-devel: 实现HTTPS访问
解压软件程序 建立虚拟管理用户
[root@web03 ~]#tar xf nginx-1.16.0.tar.gz [root@web03 ~]#useradd -M -s /sbin/nologin www [root@web03 ~]#cd nginx-1.16.0
编译配置
./configure --prefix=(软件安装目录) ./configure --user=worker (worker=进程管理用户) ./configure --group=worker (worker=进程管理用户组) ./configure --with-http_stub_status_module(状态监控模块) ./configure --with-http_ssl_module(实现HTTPs访问功能) ./configure --without-xxxx : 编译配置关闭什么指定功能 ./configure --wiht-xxx : 编译设置开启什么指定功能
修改编译配置
./configure --prefix=/application/nginx-1.16 ./configure --user=nginx ./configure --group=nginx ./configure --with-http_stub_status_module ./configure --with-http_ssl_module 也能够一次性执行因此命令 ./configure --prefix=/application/nginx-1.16 --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
编译过程:==翻译过程---使机器识别信息
[root@web03 nginx-1.16.0]#make
编译安装:
[root@web03 nginx-1.16.0]#make install
建立软连接
[root@web03 application]#ln -s /application/nginx-1.16/ /application/nginx [root@web03 application]#ll total 0 lrwxrwxrwx 1 root root 24 Jul 31 20:47 nginx -> /application/nginx-1.16/
去除空格和#号开头的
[root@web03 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
启动程序并检查端口信息
[root@web03 conf]#/application/nginx/sbin/nginx [root@web03 conf]#ps -ef|grep nginx root 4881 1 0 20:52 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx nginx 4882 4881 0 20:52 ? 00:00:00 nginx: worker process root 4884 4830 0 20:53 pts/0 00:00:00 grep --color=auto nginx [root@web03 conf]#netstat -lntup|grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4881/nginx: master
至此。nginx编译安装完成
命令加入环境变量
/etc/profile
[root@web03 sbin]#echo 'export PATH=/application/nginx/sbin:$PATH'>>/etc/profile [root@web03 sbin]#source /etc/profile [root@web03 sbin]#which nginx /application/nginx/sbin/nginx
2.6 nginx主要目录
master进程: 管理服务是否运行
worker进程: 处理用户访问请求
car /etc/nginx/nginx.conf
[root@web01 nginx]#cat nginx.conf user nginx; # 指定worker进程管理用户 worker_processes 1; # 指定worker进程数量 进程数量<= 服务器总核心数 error_log /var/log/nginx/error.log warn; # 指定错误日志存放路径 pid /var/run/nginx.pid; # 指定程序pid文件存放路径 记录进程号 events { worker_connections 1024; # 一个worker进程最大并发处理能力 socket文件 1x1024 } http { include /etc/nginx/mime.types; # 加载媒体资源类型文件 default_type application/octet-stream; # 默认识别应用类型文件 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # 访问日志文件格式信息 '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; # 访问日志路径 调取什么格式信息 main能够修改 sendfile on; keepalive_timeout 65; # 设置nginx为长链接 65秒链接超时 include /etc/nginx/conf.d/*.conf; # 加载扩展配置文件 }
nginx配置文件分为不一样区域:
(1)主配置区域:服务运行参数信息
(2)事件区域:服务程序性能参数
(3)http区域:配置网站访问参数
(4)server区域:针对每一个网站进行配置
(5)location区域:匹配信息区域
(6)if区域: 判断信息区域
/etc/nginx/conf.d
[root@web01 conf.d]# grep -vE "#|^$" default.conf >www.conf [root@web01 conf.d]# cat www.conf server { --- 能够配置网站信息 每一个网站==server==每一个虚拟主机 listen 80; --- 网站服务监听端口# 能够再这里写ip地址 server_name www.yang.com; --- 定义网站主机域名 如www.yang.com location / { root /html/www; --- 指定站点目录(存放网站全部资源) 如/html/www index oldboy.jpg index.htm; --- 首页文件 如oldboy.jpg } error_page 500 502 503 504 /lol.jpg; --- 错误页面优雅显示 如/lol.jpg location = /50x.html { root /usr/share/nginx/html; } }
客户端经过域名访问服务器时会将域名与被解析的ip一同放在请求中。当请求到了nginx中时。nginx会先去匹配ip,若是listen中没有找到对应的ip,就会经过域名进行匹配,匹配成功之后,再匹配端口。当这三步完成,就会找到对应的server的location对应的资源。
[root@web01 ~]# nginx -h nginx version: nginx/1.16.0 Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v :显示软件版本并退出 -V :显示软件版本和配置参数并退出 -t :测试配置文件并退出 -T :测试配置文件, 保存所有配置信息并退出 -q :在配置测试时, 抑制正确信息输出 -s signal :nginx -s stop:中止程序 nginx -s reload:滑重启 -p prefix :设置加载配置文件路径(default: /etc/nginx/) -c filename :设置加载指定配置文件(default: /etc/nginx/nginx.conf) -g directives :从配置文件中设置全局指令
第一步:编写多个虚拟主机配置文件
[root@web01 conf.d]#vim www.conf server { listen 80; server_name www.oldboy.com; location / { root /html/www; index index.html index.htm; } error_page 404 500 502 503 504 https://www.baidu.com/search/error.html; } ########################################################################## [root@web01 conf.d]#vim bbs.conf server { listen 80; server_name bbs.oldboy.com; location / { root /html/bbs; index index.html index.htm; } error_page 404 500 502 503 504 https://www.baidu.com/search/error.html; } ########################################################################## [root@web01 conf.d]#vim blog.conf server { listen 80; server_name blog.oldboy.com; location / { root /html/blog; index index.html index.htm; } error_page 404 500 502 503 504 https://www.baidu.com/search/error.html; }
第二步:建立站点目录和首页文件
[root@web01 conf.d]#mkdir /html{www,bbs.blog} -p [root@web01 conf.d]# for name in {www,bbs,blog};do echo $name.oldboy.com >/html/$name/index.html;done [root@web01 conf.d]# for name in {www,bbs,blog};do cat /html/$name/index.html;done www.oldboy.com bbs.oldboy.com blog.oldboy.com
第三步:重启nginx服务
[root@web01 conf.d]#systemctl restart/reload nginx
nginx配置文件规范:
(1)区域模块信息,必须有一对花括号
(2)指令信息后面必须有分号
(3)相应指令信息必须放置在正确区块中
检查配置文件语法格式
[root@web01 conf.d]#nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
第四步:进行网站页面访问测试
配置Windows hosts文件。实际工做中,不能配置本地的hosts文件。
(1)基于域名信息访问
server_name www.yang.com;
[root@web01 conf.d]#cat www.conf server { listen 80; server_name www.yang.com; location / { root /html/www; index index.html index.htm; } error_page 404 500 502 503 504 https://www.baidu.com/search/error.html; }
(2)基于端口信息访问
listen 80;
[root@web01 conf.d]#cat www.conf server { listen 80; server_name www.yang.com; location / { root /html/www; index index.html index.htm; } error_page 404 500 502 503 504 https://www.baidu.com/search/error.html; }
(3)基于ip地址信息访问
准备工做: 主配置文件
include /etc/nginx/conf.d/www.conf; --- 高可用
listen 10.0.0.7:80;
[root@web01 conf.d]#cat www.conf server { listen 10.0.0.7:80; server_name www.yang.com; location / { root /html/www; index index.html index.htm; } error_page 404 500 502 503 504 https://www.baidu.com/search/error.html; } [root@web01 conf.d]#systemctl restart nginx
PS: nginx配置文件中只要涉及到IP地址修改,必须重启nginx服务,不能平滑重启
访问hosts文件没有匹配的虚拟主机,回英文字母顺序加载第一个虚拟主机
第一步:编写配置文件 www.conf
[root@web01 conf.d]#vim www.conf server { listen 80; server_name www.yang.com; location / { root /html/www; index index.html index.htm; autoindex on; --- 开启网站站点目录信息显示功能 charset utf-8; --- 设置中文字符集信息,避免页面中文出现乱码 } error_page 404 500 502 503 504 https://www.baidu.com/search/error.html; } ========================================== [root@web01 conf.d]#systemctl restart nginx
第二步:建立站点目录,将指定的首页文件删除
[root@web01 conf.d]#rm index.html -f
第三步:修改媒体资源类型文件 /etc/nginx/mime.types
sed -r '/jpg\;$|gif\;$|txt\;$/s@(.*)@#\1@g' /etc/nginx/mime.types # 将jpg gif txt 注销 systemctl restart nginx
第四部:对网站页面信息进行控制
编辑配置文件
[root@web01 conf.d]#vim www.conf server { listen 80; server_name www.yang.com; location / { root /html/www; index index.html index.htm; autoindex on; charset utf-8; } location /VIP专区/ { --- 匹配uri操做 root /html/www; deny 10.0.0.1; --- 进行访问控制 allow all; } error_page 403 404 500 502 503 504 /error.html; location = /error.html { root /html/www; } } ====================================== [root@web01 conf.d]#systemctl restart nginx
第五步:根据用户登陆密码进行控制
(1)修改配置文件
[root@web01 conf.d]#vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.yang.com; location / { root /html/www; index index.html index.htm; autoindex on; charset utf-8; } location /VIP专区/ { root /html/www; autoindex on; charset utf-8; auth_basic yang; auth_basic_user_file /etc/password.txt; } error_page 403 404 500 502 503 504 /error.html; location = /error.html { root /html/www; } } # auth_basic --- 开启登陆认证功能 # auth_basic_user_file --- 指定加载的密码文件
auth_basic yang: 再用网站进行登陆时,会显示来自yang。谷歌浏览器不显示
(2)生成密码文件
yum install -y httpd-tools ---生成密文命令软件 htpasswd -bc /etc/password.txt oldgirl oldboy123 ---第一次建立 htpasswd -b /etc/password.txt oldboy oldboy123 ---添加新的用户 # -b 免交互输入密码 # -c 建立密码文件
(1)编写配置文件
vim www.conf location = /basic_status{ stub_status; # 开启状态监控功能 }
(2)浏览器检查
Active connections: 1 server accepts handled requests 1 1 1 Reading: 0 Writing: 1 Waiting: 0 curl www.oldboy.com/basic_status -s|awk 'NR==1{print $3}' # 取出connections数值:1 Active connections # 激活链接 The current number of active client connections including Waiting connections. # 客户端目前链接数量/包含等待链接 nginx:异步网络通信模型机制 # 客户端 ---- 服务端 链接达到最大限制,其余的链接放入队列中等待 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ · accepts 接受 The total number of accepted client connections. # 接受客户端链接总的链接数量 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ · handled 处理 The total number of handled connections. # 处理客户端链接总的链接数量 Generally, the parameter value is the same as accepts unless some resource limits have been reached (for example, the worker_connections limit). # 特殊状况,到达服务器链接限制,也会形成处理数值和接收数值不一致 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ · requests (长链接) The total number of client requests. # 总的客户端请求数量 发送了多个HTTP请求报文 vim /etc/nginx/nginx.conf keepalive_timeout 0; --- 表示短链接 PS: requests数量 == 处理链接数量 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ · Reading The current number of connections where nginx is reading the request header. # 目前读取用户请求头数量, 负载压力不大时, 数值几乎0或者1 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ · Writing The current number of connections where nginx is writing the response back to the client. # 目前响应信息发送数量 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ · Waiting ***** The current number of idle client connections waiting for a request. # 客户端链接请求信息等待处理的数量
(1)日志路径:/var/log/nginx/error.log
(2)错误级别:
[root@web01 nginx]#cat nginx.conf error_log /var/log/nginx/error.log warn; # debug 日志调试级别、显示的信息会更多 # info 日志信息级别 # notice 日志通知级别 # warn 日志警告级别、出现错误 ***** # error 日志错误级别、服务没法正常运行 ***** # crit 日志严重级别 # alert 日志报警级别、服务程序异常 # emerg 日志灾难级别
(1)日志路径:/var/log/nginx/access.log
(2)配置信息:
[root@web01 nginx]#cat nginx.conf log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main;
(3)访问日志格式信息
10.0.0.1 - - [01/Aug/2019:18:31:23 +0800] "GET /favicon.icoHTTP/1.1" 302 145 "http://bbs.oldgirl.com/" "Mozilla/5.0 #换行 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" "-"
$remote_addr | 10.0.0.1 | 记录客户端源IP地址 |
---|---|---|
$remote_user | -(网站有认证功能才会显示) | 记录认证用户信息 |
[$time_local] | [01/Aug/2019:18:31:23 +0800] | 记录访问网站时间信息 |
$request | GET /favicon.icoHTTP/1.1 | 记录请求行信息 |
$status | 302 | 记录响应状态码信息 |
$body_bytes_sent | 145 | 记录响应数据信息流量多少 |
$http_referer | http://bbs.oldgirl.com/ | 显示盗取资源网站信息 |
$http_user_agent | Chrome/69.0.3497.100 | 记录用户浏览器客户端信息 |
$http_x_forwarded_for | 用于收集用户真实 IP | |
favicon.ico | 是一个图标 |
favicon.ico:以下图标题中的图标
(4)编写一个简单的盗链代码文件
[root@web02 bbs]#vim /html/bbs/index.html <html> <meta charset="utf-8"> <head> <title>一每天教育 </title> </head> <body bgcolor=green> 一每天教育博客 <br>一每天教育博客是 <a href="https://www.cnblogs.com/basa/" target="_blank">博客地址 </a> <img src="http://www.oldboy.com/oldboy.jpg"> </body> </html>
(5)awk取访问日志流量,换成M(单位)
awk '{i=i+$9}END{print i*8/1024/1024}' /var/log/nginx/access.log
做用说明:匹配不一样的uri,作出不一样处理动做
匹配方式:
~ | 区分大小写匹配信息 | 3 |
---|---|---|
~* | 不区分大小写匹配信息 | 3 |
= | 精准匹配 | 1 匹配优先级最高 |
^~ | 优先匹配信息 | 2 |
/yang/---------目录 /yang.jpg/ ----文件 |
直接匹配指定uri | 4 |
/ | 默认匹配 | 5 |
用法说明;:
location ~ /oldboy/ { return 200; } location ~* \.jpg$ { return 301; } location = / { return 302; } location / { return 401; } location ^~ /image/ { return 403; } location /old/ { return 501; }
PS:
在指定目录信息时, 能够精准匹配,在指定文件信息时, 不能够精准匹配
实际应用:能够灵活管理网站资源路径信息
跳转方法:
(1)实现uri信息跳转
(2)实现url信息跳转
(3)实现https跳转
(4)实现伪静态跳转
跳转语法:
rewrite: Syntax: rewrite regex replacement [flag]; Default: — Context: server, location, if # regex: 要替换的信息/正则方式匹配 # replacement 替换成什么信息 # flag: 设置标记
四种flag:
last 相似于shell中continue stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI; # 实现跳转以后, 会从新发起访问,匹配其余location, 执行相应动做 |
||
---|---|---|
break 相似于shell中exit stops processing the current set of ngx_http_rewrite_module directives as with the break directive; #** 实现跳转以后, 不会从新发起访问, 直接到指定站点目录进行访问 PS: last、break 实现地址跳转后, 不会修改uri信息** |
||
redirect 302 应用比较广 ****** returns a temporary redirect with the 302 code; used if a replacement string does not start with "http://", "https://", or "$scheme"; #redirect 进行临时跳转 rewrite.oldboy.com/break/ --> rewrite.oldboy.com/test01/ rewrite.oldboy.com/break/ -服务端- rewrite.oldboy.com/test01/ --- web服务器 # 服务端记录跳转方式 |
||
permanent 301 returns a permanent redirect with the 301 code. permanent 进行永久跳转 rewrite.oldboy.com/break/ --> rewrite.oldboy.com/test/ --- 让浏览器记录跳转方式 rewrite.oldboy.com/break/ -浏览器- rewrite.oldboy.com/test/ --- web服务器 www.360buy.com --- www.jd.com www.360buy.com -浏览器- www.jd.com --- web服务器 PS: redirect、permanent 实现地址跳转后, 会修改uri信息 |
server { listen 80; server_name www.oldbaby.com; root /html; location ~ ^/ceshi/ { rewrite ^/ceshi/ /test/ break; } location ~ ^/shiyan/ { rewrite ^/shiyan/ /test/ last; } location /test/ { default_type application/json; return 200 'ok'; } }
break在跳转时,直接去站点目录(/html)下寻找test,但站点目录下没有test,因此会报404
last在跳转时,去寻找其它的location区域寻找test,其它区域有test,因此会跳转到test
例1: 用户访问/oldboy/oldboy.html实际上真实访问是/oldboy/oldboy01/oldboy.html
方法一:rewrite
server { listen 80; server_name rewrite.oldboy.com; location / { root /html; #rewrite /oldboy/oldboy.html /oldboy/oldboy01/oldboy.html redirect; rewrite (.*) /oldboy/oldboy01/oldboy.html redirect; } } # rewrite /oldboy/oldboy.html /oldboy/oldboy01/oldboy.html redirect; # rewrite (.*) /oldboy/oldboy01/oldboy.html redirect; 以上两个都能实现跳转
方法二:return
server { listen 80; server_name rewrite.oldboy.com; location / { root /html; return 302 http://rewrite.oldboy.com/oldboy/oldboy01/oldboy.html; } }
例2: 用户访问/2014/oldboy/oldgirl/oldboy.html实际上真实访问是/2018/oldboy/oldgirl/oldboy.html
(1): 建立uri目录结构信息
mkdir 2014/oldboy/oldgirl/ -p --- 跳转前目录结构 echo oldboy62 >2014/oldboy/oldgirl/oldboy.html mkdir 2018/oldboy/oldgirl/ -p --- 跳转后目录结构 echo oldboy62 >2018/oldboy/oldgirl/oldboy.html /2014/oldboy/oldgirl/oldboy.html /2018/oldboy/oldgirl/oldboy.html (.*)$ $1
(2)编写配置文件
server { listen 80; server_name rewrite.oldboy.com; location / { root /html; } location /2014/ { rewrite ^/2014/(.*)$ /2018/$1 redirect; return 302 http://rewrite.oldboy.com/2018/oldboy/oldgirl/oldboy.html; } } # rewrite ^/2014/(.*)$ /2018/$1 redirect; # return 302 http://rewrite.oldboy.com/2018/oldboy/oldgirl/oldboy.html; 以上两种都能进行跳转
例3:用户访问/test/lol.html目录下任何内容, 实际上真实访问是http://www.oldboy.com/oldboy.html
方式一:mkdir test
echo yang >/html
编写配置文件
[root@web01 html]#vim /etc/nginx/conf.d/www.conf server { listen 80; server_name rewrite.oldboy.com; location / { root /html; } location /test/{ rewrite ^/test/(.*)$ http://rewrite.oldboy.com/$1 redirect; } } systemctl restart nginx
方式二:mv /html/lol.html /html/test
编写配置文件
[root@web01 html]#vim /etc/nginx/conf.d/www.conf server { listen 80; server_name rewrite.oldboy.com; location / { root /html/test; } location /test/{ rewrite ^/test/(.*)$ http://rewrite.oldboy.com/$1 redirect; } } systemctl restart nginx
例4:用户访问course-11-22-33.html实际上真实访问是/course/11/22/33/course_33.html
意思是在浏览器上输入http://rewrite.oldboy.com/course-11-22-33.html,
可是访问的内容是/course/11/22/33/course_33.html的内容
第一步: 准备站点目录环境
mkdir course/11/22/33/ -p cd course/11/22/33/ echo oldboy62 >course_33.html
第二步:编写配置文件
[root@web01 33]#vim /etc/nginx/conf.d/www.conf server { listen 80; server_name rewrite.oldboy.com; location / { root /html; # rewrite ^/course-(.*)-(.*)-(.*) /course/$1/$2/33/course_$3 last; rewrite ^/course-(.*) /course/11/22/33/course_33.html last; } } rewrite ^/course-(.*)-(.*)-(.*) /course/$1/$2/33/course_$3 last; rewrite ^/course-(.*) /course/11/22/33/course_33.html last; 以上两个均可以
例5: 访问rewrite.oldboy.com --- www.jd.com 实现域名(url)跳转
真正跳转到www.jd.com
root@web01 33]#vim /etc/nginx/conf.d/www.conf server { listen 80; server_name rewrite.oldboy.com; rewrite ^/(.*) http://www.jd.com/$1 redirect; }
跳转到本身在本地建立的www.jd.com
方式一:
[root@web01 html]#vim /etc/nginx/conf.d/rewrite.conf server { listen 80; server_name rewrite.oldboy.com; rewrite ^/(.*) http://www.jd.com/$1 redirect; } server { listen 80; server_name www.jd.com; location / { root /html; index index.html; } }
方式二:
[root@web01 html]#vim /etc/nginx/conf.d/rewrite.conf server { listen 80; server_name rewrite.oldboy.com www.jd.com; location / { root /html; index index.html; if ($http_host ~* ^rewrite.oldboy.com$) { rewrite ^/(.*) http://www.jd.com/$1 redirect; } } }