linux第二阶段web

WEB网站服务

day01 http协议原理

1.用户访问网站的原理过程

1.用户访问域名
2.完成域名dns解析过程
3.完成tcp三次握手建立
4.向网站服务器访问请求信息 (HTTP请求)
  网站架构接收到用户请求 (负载均衡 web集群 存储 数据库 缓存...)
5.向客户端响应数据信息 (HTTP响应)
6.用户利用浏览器解析响应信息,显示最终页面
7.完成tcp四次挥手过程

2.用户访问网站HTTP协议原理

#HTTP称为:超文本传输协议
#HTTP协议请求:报文结构 4个部分
 1.请求行:标题	1行
   请求方法:get(获取) post(提交)
   请求内容:默认index.html (首页文件)
   协议版本:HTTP1.0 HTTP1.1 HTTP2.0(高并发访问效率更高)
     TCP协议长连接和短连接的概念:
     短连接:一次连接 一次请求
     长连接:一次连接 多次请求
 2.请求头:中心思想 多行
   请求主机信息:根据请求主机信息显示不同页面www bbs blog
   请求user-agent信息:打开手机---腾讯浏览器APP---设置---浏览器UA:iphone
 3.空行
 4.请求主体:详细内容 多行
   使用get方法:没有请求主体
   使用post方法:产生请求主体 (提交的信息 用户注册信息  用户登录信息) ???

#HTTP协议响应:报文结构 4个部分
 1.响应行:
   响应状态码:影响结果
   200	正常结果信息
   301  跳转永久跳转
   302  临时跳转
   401  认证失败了
   403  用户禁止访问 ip被屏蔽
        网站首页不存在
   404	你访问的页面不存在
   500 502 503 504	网站结构服务有关
 2.响应头部:
   响应的服务程序:web服务程序名称 nginx/tenginx/bfe/apache
 3.空行
 4.响应请求主体
   请求响应过来的代码信息:html代码信息

3.HTTP协议资源

媒体资源类型:mime.types 媒体资源类型文件
媒体资源类型文件中有的信息:会直接解析,显示页面
媒体资源类型文件没有的信息:会进行下载,不会解析

访问网站地址类型:
www.oldboy.com      /oldboy.jj
url              uri       
url: 全称为Uniform Resource Location,中文翻译为统一资源定位符,也被称为网页地址
uri: 全称为Uniform Resource Identifier,中文翻译为统一资源标识符,是一个用于标识某一互联网资源名称的字符串

访问资源分类:
静态资源: 所见即所得  见到的代码是什么样,浏览器解析就会解析成什么页面
1)	访问资源uri扩展名信息
每个页面都有一个固定的URL地址,且URI一般以.html、.htm、.shtml等常见形式为后缀,而且地址中不含有问号“?”或“&”等特殊符号。
2)	网页内容一经发布到网站服务器上,无论是否有用户访问,每个网页的内容都是保存在网站服务器文件系统上的,也就是说,静态网页是实实在在保存在服务器上的文件实体,每个网页都是一个独立的文件。
*3)	网页内容是固定不变的,因此,容易被搜索引擎收录(容易被用户找到)(优点)。
4)	因为网页没有数据库的支持,所以在网站制作和维护方面的工作量较大,当网站信息量很大时,完全依靠静态网页比较困难(缺点)。
5)	网页的交互性较差,在程序的功能实现方面有较大的限制(缺点)。
*6)	网页程序在用户浏览器端解析,如IE浏览器,程序解析效率很高,由于服务器端不进行解析,并且不需要读取数据库,因此服务器端可以接受更多的并发访问。当客户端向服务器请求数据时,服务器会直接从磁盘文件系统上返回数据(不做任何解析)。待客户端拿到数据后,在浏览器端解析并展现出来(优点)。
用户请求 --- web服务器 -- index.html  oldboy.html oldboy.jpg --- 用户

动态资源: 会调取数据库资源,显示页面信息
1)	网页扩展名后缀常见为:.asp、.aspx、.php、.js、.do、.cgi等。	※
*2)	网页一般以数据库技术为基础,大大降低了网站维护的工作量。	
*3)	采用动态网页技术的网站可以实现更多的功能,如用户注册、用户登录、在线调查、投票、用户管理、订单处理、发博文等。
*4)	动态网页并不是独立存在于服务器上的网页文件,当用户请求服务器上的动态程序时,服务器解析这些程序并可能通过读取数据库来返回一个完整的网页内容。
5)	动态网页中的“?”在搜索引擎的收录方面存在一定的问题,搜索引擎一般不会从一个网站的数据库中访问全部网页,或者出于技术等方面的考虑,搜索蜘蛛一般不会去抓取网址中“?”后面的内容,因此在企业通过搜索引擎进行推广时,需要针对采用动态网页的网站做一定的技术处理(伪静态技术),以便适应搜索引擎的抓取要求。
6)  动态资源需要和数据进行交互,网页面显示效率较低
用户请求 ---  web服务器 --- oldboy.php  ---> php服务处理    --> 数据库  --- php --- web --- 用户 
用户请求 ---  web服务器 --- oldboy.js   ---> tomcat服务处理 --> 数据库

伪静态资源:
1)有数据库支持,可以进行交互
2)便于被搜索引擎收录	

4.网站度量方式

IP:根据用户访问的源IP地址  awk数组  ELK---access.log(记录用户源IP地址(公网IP地址))
参考数据值:192.168(南方 10万)  10.0.0(北方 1万)  彩票
PV: 页面访问量
参考数据值:用户点击网站页面的数量
UV:记录独立用户访客数量
cookie: 钥匙  会员卡      网站给用户分配身份标识信息,保存在主机本地
session:锁头  会员登记表  网站产生的用户访问记录信息,保存在网站服务器中
程序代码进行统计
度量数值参考:http://alexa.chinaz.com/alexa_more.aspx

5.企业网站常见web服务

官方参考:https://w3techs.com/technologies/overview/web_server/all
处理静态资源网站:
nginx  web服务软件:
apache web服务软件:zabbix
Tengine
处理动态资源网站
php    web服务软件:
tomcat 

6.安装ginx

第一个历程:更新yum源--nginx官方yum仓库下载
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
第二个历程:下载安装
yum install -y nginx

day02安装部署nginx

1.nginx程序功能介绍

1.特点:nginx因具有高并发(静态资源),占用系统资源少等特点
2.功能:nginx程序功能强大
  1.可以满足web服务应用
  2.可以满足负载均衡应用
  3.可以满足缓存停用
3.局限:无法处理动态资源请求 需配合php python来使用

2.防盗链概念

用户访问:					    	用户访问:
	↓						    	↓
	↓						    	↓
   网站A				    		  网站B
www.123.com                        www.abc.com
1234.jpg                            index.html
5678.avi                           调用www.123.com/1234.jpg

B网站调用的链接是A网站的,在A网站可以正常显示,但不走流量

3.网站服务apache和nginx区别

1.从特点进行说明
2.从功能方面说明
3.从软件网络模型说明
  1.select:apache
   事件01: 宿舍管理员  找人---一个一个房间进行查找(遍历)
   事件02:幼儿园阿姨  负责看住小孩上厕所---一个一个进行确认
  2.epoll:nginx
   事件01: 宿舍管理员  找人---查找名单册
   事件02:幼儿园阿姨  负责看住小孩上厕所---有感觉就站到教室圈里

4.nginx软件安装三种方式

1.利用yum安装软件:
官方源安装:最新稳定版软件  目录结构信息(企业环境相符)
其他源安装:稳定版软件	   目录结构信息(企业环境不太相符)  
2.利用编译方式安装:自定义安装功能 自定义程序安装的目录
第一步:解决软件依赖关系
第二步:进行软件配置过程  配置软件目录  指定软件功能
第三步:进行软件编译过程  翻译解释的过程   C-gcc  python-python解释器
第四步:进行软件编译安装

web01:采用官方源安装
第一个历程:修改yum源
官方参考:http://nginx.org/en/linux_packages.html#RHEL-CentOS
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
第二个历程:清除yum缓存信息
yum clean all
第三个历程:yum安装软件
yum install -y nginx

web02:采用其他源安装
第一个历程:直接yum安装软件
yum install -y nginx

web03:编译安装软件
第一步:下载源码包
mkdir /server/tools -p
cd /server/tools
wget http://nginx.org/download/nginx-1.16.0.tar.gz
补充:解决软件依赖:
yum install -y pcre-devel
说明:pcre-perl兼容正则表达式
未安装报错信息:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

yum install -y openssl-devel
说明:openssl-devel 实现支持HTTPs  需要有私钥 公钥(证书)
未安装报错信息:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

yum install -y gcc*
说明:gcc c语言的解释器(nginx -- c语言 python)
checking for OS
+ Linux 2.6.32-642.el6.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found


第二步:解压源码包
tar xf nginx-1.16.0.tar.gz
第三步:软件编译配置过程
cd nginx-1.16.0/
useradd www -s /sbin/nologin -M
./configure  --prefix=/application/nginx-1.16.0  --user=www --group=www --with-http_ssl_module  --with-http_stub_status_module
--prefix=PATH                      set installation prefix
								指定软件程序安装目录(不需要创建)
--user=USER                        set non-privileged user for worker processes
								为worker进程设置一个非特权用户(必须存在)
--group=GROUP                      set non-privileged group for worker processes
								为worker进程设置一个非特权用户组(必须存在)
--with-http_ssl_module             enable ngx_http_ssl_module
								启用HTTPS功能
--with-http_stub_status_module     enable ngx_http_stub_status_module
								启动nginx状态监控功能
第四步:编译过程
make

第五步:编译安装
make install

第六步:程序目录创建软链接
ln -s /application/nginx-1.16.0/ /application/nginx

5.nginx程序目录结构

etc/nginx/nginx.conf       --- 主配置文件
/etc/nginx/*cgi(通用接口)  --- nginx(无法处理动态请求) 
                          ---fast_cgi- php   
                          ---uwsgi   - python 

[外链图片转存失败(img-70UctOja-1562167232978)(file:///C:\Users\86186\Pictures\markdown\0626.png)]

6.nginx程序配置文件说明

#准备工作:
cp nginx.conf nginx.conf.default
grep -v "^$" nginx.conf.default >nginx.conf
/etc/nginx/nginx.conf 	
user  nginx;                                   #--- 指定worker进程管理用户
worker_processes  1;                           #--- 指定worker进程的数量    建议调整数量=服务器CPU核心数(2倍)
error_log  /var/log/nginx/error.log warn;      #--- 指定错误日志保存路径
pid        /var/run/nginx.pid;                 #--- 指定程序pid文件保存路径
events {
    worker_connections  1024;                  #--- worker进程连接数 调整按照2的倍数  服务总的连接数=worker_processes*worker_connections<系统打开文件数
}
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  ;   #--- 定义访问日志存储路径
    sendfile        on;                        #--- 和nginx优化有关
    #tcp_nopush     on;                        #--- 和nginx优化有关
    keepalive_timeout  65;                     #--- 连接超时时间  默认单位秒
    #gzip  on;                                 #--- 和nginx优化有关
    include /etc/nginx/conf.d/*.conf;          #--- 加载conf.d下面的所有.conf扩展配置文件
}

#疑问解答:
01. nginx程序worker进程是什么?
    master process:控制服务可以正常运行  老板
	worker process:处理用户访问请求      员工
02. nginx配置文件结构:
    1)核心配置部分     main   区域 
    2)事件配置部分     event  区域	
    3)网站配置部分     http   区域     指定网站功能参数
    4)主机配置区域     server 区域 	指定每个网站信息(域名 站点目录 首页文件)
    5)location配置区域                 指定网站特殊功能		


#准备工作: 
grep  -Ev "^$|#" default.conf >www.conf
/etc/nginx/conf.d/www.conf
server {
    listen       80;                    #--- 指定服务监听信息  默认监听端口信息
    server_name  www.oldboy.com;        #--- 指定网站域名信息
    location / {                        #--- 匹配不同uri,进行不同的配置 ???
        root   /usr/share/nginx/html;   #--- 指定站点目录路径
        index  index.html index.htm;    #--- 指定首页文件信息
    }
    error_page   500 502 503 504  /oldboy,jpg;   #--- 优雅显示错误信息
    location = /oldboy.jpg {                     #--- 匹配指定50x.html的uri
        root   /html;                            #--- 指定站点目录
    }
}

#实践配置:
#vim www.conf 
server {
    listen       80;
    server_name  www.oldboy.com;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   404 500 502 503 504  /oldboy.jpg;
    location = /oldboy.jpg {
        root   /html;
    }
}

#创建目录和数据信息:
mkdir /html  --- 放入oldboy.jpg图片

#本地域名解析:
C:\Windows\System32\drivers\etc\hosts

7.网站代码上线流程

#vim /etc/nginx/conf.d/www.conf

server {
   listen       80;
   server_name  www.oldboy.com;
   location / {
       root   /html/www;
       index  index.html index.htm;
}
systemctl restart nginx	

#第二个历程:根据配置文件调整系统环境
mkdir /html/www 

#第三个历程:将开发人员写的代码上传到站点目录  开发人员-- gitlab(版本控制服务)-- 运维人员
#将代码包放入站点目录---解压 
#代码更新,要进行备份(回退还原)

#第四个历程:确认域名解析
aliyun进行域名配置

#第五个历程:修改调整数据库
SQL语句

#第六个历程:访问网站进行测试

day03 nginx配置

1.利用nginx搭建多个网站

#创建多个网站www bbs blog
#第一步:编写配置文件
#配置文件一www
server {
    listen       80;
    server_name  www.oldboy.com;
    location / {
        root   /html/www;
        index  index.html index.htm;
    }
}
#配置文件二bbs
server {
    listen       80;
    server_name  bbs.oldboy.com;
    location / {
        root   /html/bbs;
        index  index.html index.htm;
    }
}
#配置文件三blog
server {
    listen       80;
    server_name  blog.oldboy.com;
    location / {
        root   /html/blog;
        index  index.html index.htm;
    }
}

#第二步创建不同网站站点目录/创建网站页面信息index.html
mkdir /html/www   #blog bbs
cd /html/www
echo www.oldboy.com >index.html

#第三步物理机设置爱hosts解析
10.0.0.7  www.oldboy.com  bbs.oldboy.com blog.oldboy.com

#第四步重启nginx
利用systemctl 重启
systemctl restart nginx

利用nginx命令重启
nginx 
nginx -s reload
nginx -t 检查配置文件语法
		检查每个指令参数编写正确/正常位置正常
		每个区域信息需要有一对花括号
		每个指令参数最后要有分号;
注意:两种方法不要混用
补充:网站页面异常排错思路:
1:检查DNS解析信息
2:检查HTTP请求信息 抓包
3:检查HTTP响应信息 抓包

2.访问页面

1.基于域名访问
2.基于端口访问

server {
    listen       8080;    #--- 修改端口号
    server_name  www.oldboy.com;
    location / {
        root   /html/www;
        index  index.html index.htm;
    }

}

访问原理:
01. 和指定IP地址建立连接  --- 10.0.0.7
02. 向指定端口发出请求    --- 8080
03. 找寻匹配80端口的主机信息
04. 匹配用户的请求主机信息
    将配置的第一个主机信息进行显示

基于地址访问 
[root@web01 conf.d]# cat www.conf 
server { 
    listen       172.16.1.7:80;   --- 修改地址信息
    server_name  www.oldboy.com;
    access_log  /var/log/nginx/access_oldboy.log  oldboy;
    location / {
        root   /html/www;
        index  index.html index.htm;
    }
}
PS:nginx程序涉及到IP地址的修改,都需要重启服务(不是平滑重启)

3.利用nginx显示网站目录索引

server {
    listen       80;
    server_name  www.oldboy.com;
    access_log  /var/log/nginx/access_oldboy.log  oldboy;
    location / {
        root   /html/www;
        index  index.html index.htm;
        autoindex  on;          #--- 当首页文件不存在,会显示站点目录索引结构信息
	    charset    UTF-8;
    }
}

4.利用nginx实现安全访问控制

1.基于用户访问信息进行控制 /images  /av
/images   允许  oldboy.jpg
/av       禁止  oldboy.html
第一个历程:部署站点目录环境
mkdir  /html/www/{images,av}
第二个历程:编辑配置文件
[root@web01 conf.d]# cat www.conf

server {
    listen       80;
    server_name  www.oldboy.com;
    access_log  /var/log/nginx/access_oldboy.log  main;
    root   /html/www;   #--- 全局配置
    location / {
        root   /html/www;   #--- 局部配置
        index  index.html index.htm;
    }
    location /images {
        allow all;
        root   /html/www;
        index  oldboy.jpg index.html index.htm;
    }
    location /av {
        allow  10.0.0.1;
        deny all;
        root   /html/www;
    }
}

2.基于用户认证信息进行控制
#第一步:创建密码文件信息
[root@web01 conf.d]# htpasswd -bc /etc/nginx/nginx.password oldboy oldboy123
Adding password for user oldboy
[root@web01 conf.d]# cat /etc/nginx/nginx.password 
oldboy:$apr1$.RxGBSH0$ePH61Rs8PuAuhv9boxvYz0
#第二步:编写主机配置文件
server {
    listen       80;
    server_name  www.oldboy.com;
    access_log  /var/log/nginx/access_oldboy.log  main;
    auth_basic  "oldboy61";
    auth_basic_user_file /etc/nginx/nginx.password;
    location / {
        root   /html/www;
        index  index.html index.htm;
        autoindex  on;
        charset  UTF-8;
    }
}

5.利用nginx实现网站状态监控

#1.编写配置文件
server {
    listen       80;
    server_name  stat.oldboy.com;
    location / {
        stub_status;
    }
}

#2.页面监控信息
Active connections       --- 总的激活并发连接数
accepts                  --- 总的接收连接数信息
handled                  --- 总的处理连接数信息
requests                 --- 总的请求数量
Reading: 读取请求报文数量   
Writing: 回复响应报文数量
Waiting: 等待队列

6.nginx程序日志配置说明

错误日志记录服务常见错误
01. 服务运行错误信息
02. 用户访问页面的错误
如何配置:
error_log  /var/log/nginx/error.log warn(记录错误级别);
debug    --- 调试级别    产生的日志信息最多
info     --- 信息级别
notice   --- 通知级别 
warn     --- 警告级别
error    --- 错误级别  
crit     --- 严重级别
alert    --- 非常严重级别
emerg    --- 灾难级别    产生的日志信息最少


访问日志:记录用户访问信息
01. 记录用户访问网站什么信息
02. 记录访问访问网站用户信息

如何配置: 
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_oldboy.log  oldboy;
nginx默认变量:
$remote_addr:    --- 访问网站客户端源IP地址
$remote_user      --- 表示认证用户名称信息
[$time_local]     --- 显示响应时间信息
$request          --- 显示请求行信息
$status           --- 显示状态码
$body_bytes_sent  --- 回复数据大小(流量)信息 字节
$http_referer     --- 盗链人地址信息/用于推广
$http_user_agent  --- 显示用户访问网站客户端程序 (电脑 手机)

day04 nginx程序特设功能

1.location特殊应用

1.功能:匹配不同的uri信息,做出相应处理
Syntax:	location [ = | ~ | ~* | ^~ ] uri { ... }       语法结构
                       匹配          uri  执行什么动作
	    awk       '模式{动作}'
Default:	—
Context:server, location   --- 可以配置在什么区域中
=:精确匹配     			= /oldboy   www.oldboy.com  /oldboy
~:模糊匹配(区分大小写)
~*:模糊匹配(不区分大小写)
^~:优先匹配 不识别uri信息中正则符号

如何应用:
location = / {                        01
    return  301;
}

location / {                          05   默认匹配
    return  302;
}

location /documents/ {                04
    return  404;
}

location ^~ /images/ {                02
    return  502;
}

location ~* \.(gif|jpg|jpeg)$ {       03
    return  520;
}

样例:测试 ~ 和 ~*
location ~ /test/ {
    return  301;
}
location ~* /test/ {
    return  302;
}

规范站点目录结构信息:
[root@web01 conf.d]# cat www.conf 
server {
    listen       80;
    server_name  www.oldboy.com;
    location ~* \.jpg$ {
        root  /html/www/jpg;
    }
    location ~* \.png$ {
        root /html/www/png;
    }
    location / {
        root  /html;
        index index.html;
    }
}

2.nginx程序重写功能

rewrite  url/uri/伪静态

Syntax:	 rewrite regex          replacement   [flag];
                正则信息匹配   修改成的信息   标记
Default: —
Context: server, location(推荐), if
last:      跳转完毕,会在执行其他动作
break:     跳转完毕,不在执行其他动作
redirect    302  临时跳转   *****
permanent: 301  永久跳转 

1). 跳转配置中last与break区别对比示例
server {
   listen            80;
   server_name       www.oldboy.com;
   root              /html;
   location  ~ ^/break/ {
       rewrite  ^/break/  /test/  break;
   }
   location  ~ ^/last/  {
       rewrite  ^/last/  /test/  last;
   }
   location   /test/ {
       default_type   application/json;
       return 200 'ok';
   }
}


2) 临时跳转和永久跳转配置
server {
   listen            80;
   server_name       www.oldboy.com;
   root              /html;
   location  ~ ^/oldboy {
       rewrite  ^(.*)$  https://www.etiantian.org redirect;
	   rewrite  ^(.*)$  https://www.etiantian.org permanent;
       # return 301 http://bbs.etiantian.org;
	   # return 302 http://bbs.etiantian.org;
   }
}

永久跳转记录跳转信息:  301
临时跳转不记录跳转信息:302

永久跳转记录跳转信息:  301
临时跳转不记录跳转信息:302

301永久: 用户浏览器  -请求信息->  www.360buy.com --> 301 --> www.jd.com
           客户端                              服务端
		 用户浏览器  -请求信息->  www.360buy.com
		                       ->  www.jd.com     ---> 服务端
总结:在浏览器上记录跳转的缓存信息
          www.jd.com/oldboy.jpg   ---    
          www.jd.com/2017/    404
302临时: 用户浏览器  -请求信息->  www.360buy.com --> 302 --> www.jd.com
           客户端                              服务端
          用户浏览器  -请求信息->  www.360buy.com --> 302 --> www.jjd.com
           客户端                              服务端
总结:在浏览器上不记录跳转的缓存信息
          www.jd.com/oldboy.jpg   --- www.jd.com/2018/

补充:实现地址跳转的方法
第一种:利用rewrite  正则匹配
第二种:利用retrun

3.301,302跳转

练习1):用户访问/abc/1.html实际上真实访问是/ccc/bbb/2.html
#http://www.oldboy.com/abc/1.html ==> http://www.oldboy.com/ccc/bbb/2.html
第一里程:准备真实的访问路径
[root@web03 ~]# mkdir /code/ccc/bbb -p
[root@web03 ~]# echo "ccc_bbb_2" > /code/ccc/bbb/2.html

第二个里程:Nginx跳转配置
[root@web03 conf.d]# cat ccbb.conf 
server {
    listen 80;
    location / {
        root /html/www;
        index index.html;
    }
    location /abc/ {
        rewrite (.*) /ccc/bbb/2.html redirect;
        #return 302 /ccc/bbb/2.html;
    }
}

第三个里程:重启Nginx服务
[root@web03 ~]# systemctl restart nginx

练习2):用户访问/2018/ccc/bbb/2.html实际上真实访问是/2014/ccc/bbb/2.html
#http://www.oldboy.com/2018/ccc/bbb/2.html ==> http://www.oldboy.com/2019/ccc/bbb/2.html

第一次访问网站:
http://www.oldboy.com/2018/ccc/bbb/2.html  --> 跳转 
第二次访问网站:
http://www.oldboy.com/2019/ccc/bbb/2.html

第一个里程:准备真实的访问路径
[root@web03 ~]# mkdir /html/www/2019/ccc/bbb -p
[root@web03 ~]# echo "2019_ccc_bbb_2" > /html/www/2019/ccc/bbb/2.html

第二个里程:Nginx跳转配置
[root@web03 conf.d]# cat www.conf 
server {
    listen 80;
	server_name  www.oldboy.com;
    location / {
        root /html/www;
        index index.html;
    }
    location /2018 {
        rewrite ^/2018/(.*)$ /2019/$1 redirect;    
    }
}

第三个里程:重启Nginx服务
[root@web03 ~]# systemctl restart nginx

练习3):用户访问/test目录下任何内容, 实际上真实访问是http://www.oldboy.com
location /test {
    rewrite (.*) http://www.oldboy.com redirect;
}


练习4):用户访问course-11-22-33.html实际上真实访问是/course/11/22/33/course_33.html
#http://www.oldboy.com/course-11-22-33.html ==> http://www.oldboy.com/course/11/22/33/course_33.html  

第一个里程 准备真实的访问路径
[root@web03 ~]# mkdir /html/www/course/11/22/33/ -p
[root@web03 ~]# echo "docs.etiantian.org" > /html/www/course/11/22/33/course_33.html

第二个里程 Nginx跳转配置
[root@web03 conf.d]# cat www.conf 
server {
    listen 80;
	server_name  www.oldboy.com;
    root /html/www;
    index index.html;
    location / {
        #灵活rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;
        #固定rewrite ^/course-(.*)  /course/11/22/33/course_33.html redirect;
    }
    
第三个里程 重启Nginx服务
[root@web03 ~]# systemctl restart nginx

例5:将http请求,跳转至https   ???
server {
        listen 80;
        server_name oldboy.com;
        rewrite ^(.*) https://$server_name$1 redirect;
        #return 302 https://$server_name$request_uri;    ???
}

server {
    listen 443;
    server_name oldboy.com;
    ssl on;
}

终极测验:
a 访问oldboy.com/oldboy.jpg  --->  www.oldboy.com/oldboy.jpg   临时
第一个历程:准备环境
将oldboy.jpg --- 站点目录
第二个历程:编写配置文件:
server {
  listen      80;
  server_name www.oldboy.com;
     root  /html/www;
     index index.html;
     rewrite ^/(.*)  http://www.oldboy.com/$1  redirect;
}

打破循环问题:
第一种方式:多个server配置
server { 
listen   80;
server_name  oldboy.com;
rewrite ^/(.*)  http://www.oldboy.com/$1  redirect;
}
server {
	  listen      80;
	  server_name www.oldboy.com;
	  root  /html/www;
	  index index.html;
}
第二种方式:打破循环 if 内置变量 
server {
  listen      80;
  server_name www.oldboy.com;
     root  /html/www;
     index index.html;
     if ($host ~ ^oldboy.com) { 
        rewrite ^/(.*)  http://www.oldboy.com/$1  redirect;
     }	
}
 
nginx常用内置变量:
$host         		记录请求报文请求主体的host信息     
$server_name        当前用户配置server_name的域名信息
$request_filename   当前请求的文件路径名(带网站的主目录/html/www/images/test.jpg)
$request_uri        当前请求的文件路径名(不带网站的主目录/images/test.jpg)
$scheme             用的协议,比如http或者https

第三个历程:配置DNS解析
10.0.0.7   oldboy.com  www.oldboy.com	
   

b 访问www.etiantian.org      --->  www.oldboy.com   永久	
第一个历程:准备环境
将oldboy.jpg --- 站点目录
第二个历程:编写配置文件:
server {
  listen      80;
  server_name www.oldboy.com;
     root  /html/www;
     index index.html;
     if ($host ~ www.etiantian.org) { 
        rewrite ^/(.*)  http://www.oldboy.com/$1  redirect;
     }
}
第三个历程:主配置文件编写
include /etc/nginx/conf.d/www.conf
include /etc/nginx/conf.d/*.conf;
第四个历程:配置DNS解析
10.0.0.7   oldboy.com  www.oldboy.com www.etiantian.org	

ost ~ ^oldboy.com) {
rewrite ^/(.*) http://www.oldboy.com/$1 redirect;
}
}

nginx常用内置变量:
$host 记录请求报文请求主体的host信息
$server_name 当前用户配置server_name的域名信息
$request_filename 当前请求的文件路径名(带网站的主目录/html/www/images/test.jpg)
$request_uri 当前请求的文件路径名(不带网站的主目录/images/test.jpg)
$scheme 用的协议,比如http或者https

第三个历程:配置DNS解析
10.0.0.7 oldboy.com www.oldboy.com

b 访问www.etiantian.org —> www.oldboy.com 永久
第一个历程:准备环境
将oldboy.jpg — 站点目录
第二个历程:编写配置文件:
server {
listen 80;
server_name www.oldboy.com;
root /html/www;
index index.html;
if ($host ~ www.etiantian.org) {
rewrite ^/(.) http://www.oldboy.com/$1 redirect;
}
}
第三个历程:主配置文件编写
include /etc/nginx/conf.d/www.conf
include /etc/nginx/conf.d/
.conf;
第四个历程:配置DNS解析
10.0.0.7 oldboy.com www.oldboy.com www.etiantian.org


猜你喜欢

转载自blog.csdn.net/qq_31300229/article/details/94591997
今日推荐