了解网络层及各种协议:https://www.cnblogs.com/linhaifeng/articles/5937962.html
http协议
一,概述
1,http协议:Hyper Text Transfer Protocol
超文本传输协议,将客户的请求发送给服务器,服务器传输给浏览器,浏览器将请求的通过解析,变成我们可读的页面
2,超文本
包含超链接和各种多媒体元素标记的文本,这些超文本彼此形成链接,形成网状,因此称为网页;
这些超文本用URL表示
最常见的超文本格式是,超文本标记语言html
html 文件 -> 包含各种各样的元素(url链接) - -> 形成webpage,简称网页
3, URL
URL即统一资源定位符(Uniform Resource Locator)
用来表示唯一的万维网中的某一个文件
URL由协议 主机,端口(默认80)及文件名构成
当输入:baidu.com
实际上访问的是:
http://www.baidu.com:80/index.html
URL:http://www.baidu.com:80/index.html
协议 域名 端口 文件名
协议:http/https/file/tcp/ssh
域名:方便记忆,DNS会把域名转换成对应的IP地址
端口:网站当入口
文件:真实存在于服务器伤的文件
---------
一个完整当html页面是由很多个URL组成的,http协议用来传输和解析html页面
---------
二, http协议原理
1,浏览器中输入网址https://www.baidu.com/index.html,浏览器会分析这个URL
2,浏览器向DNS服务器请求解析,该URL中的域名www.baidu.com,解析出百度所在的IP地址
3,DNS服务器将解析出的IP地址110.111.112.113返回给浏览器
4,浏览器收到IP地址,立即与该IP所在的服务器建立tcp连接(默认端口80)
5,浏览器请求文档,即html页面,GET /index.html,并发出HTTP请求报文
6.服务器给出响应,将请求的index.html文档返回给浏览器,也就是响应HTTP请求的报文。
7.TCP连接响应完之后,释放TCP连接。
8.最后就能显示出,你请求的这个页面了
三, 请求回应格式
客户端请求消息:
客户端发送一个http请求到服务器包括以下格式:
请求行
请求头部
空行
请求数据
服务端响应消息:
四, 请求方式
五, 常见状态码及分类
状态码(status-code)是响应报文状态行中包含的一个3位数字,指明特定的请求是否被满足,如果没有满足,原因是什么。状态码分为以下五类:
六, http相关术语
1.PV、UV、IP
假设公司有一座大厦,大厦有100人,每个人有一台电脑和一部手机,上网都是通过nat转换出口,每个人点击网站2次, 请问对应的pv,uv,ip分别是多少?
PV : 页面独立浏览量
UV : 独立设备
IP : 独立IP
那么上面的题:
PV: 100*2*2 = 400
UV: 100*2 = 200
IP: 1
日PV千万量级并不大
SOA松耦合架构
Service-Oriented Architecture,面向服务的架构。
通俗说,当下图中的会员中心URL不能登陆,并不影响其他URL的登陆
七, 用户访问网站集群架构流程
1. 客户端发起http请求,请求会先抵达前端的防火墙
2. 防火墙识别用户身份,正常的请求通过内部交换机通过tcp连接后端的负载均衡,传递用户的http请求
3. 负载均衡接收到请求,会根据请求的内容进行下发任务,通过tcp连接后端的web,转发发用户的http请求
4. web接收到用户的http请求后,会根据用户请求的内容进行解析,解析分为如下:
静态请求:web直接返回给负载均衡->防火墙->用户
动态请求:web向后端的动态程序建立TCP连接,将用户的动态http请求传递至动态程序->由动态程序进行解析
5. 动态程序在解析的过程中,如果碰到查询数据库请求,则优先与缓存建立tcp连接,并发起数据查询操作。
6. 如果缓存没有对应的数据,动态程序再次向数据库建立tcp连接,并发起查询操作。
7. 最后数据由, 数据库->动态程序->缓存->web服务->负载均衡->防火墙->用户
Nginx
一,概述
开源且高性能、可靠的http web服务、代理服务
开源:直接获取源代码
高性能:支持海量并发
可靠:服务器稳定
原理:内部的进程主要有两类,master 进程 和 worker 进程。其中 master 进程只有一个,worker 进程可以有多个
二,特征
1,高性能并发:
在海量并发的情况下,nginx比web其他服务响应速度快
2,轻量且高扩展性:
nginx模块很多,不需要全部安装,只需要保留必要模块
需要哪个模块再装哪个模块
3,高可靠性:
其他web服务运行一段时间可能需要重启。nginx不需要
支持的宕机级别是9999/99999
3个9:(1-99.9%)*365*24=8.76小时,表示该系统在连续运行1年时间里最多可能的业务中断时间是8.76小时。
4个9:(1-99.99%)*365*24=0.876小时=52.6分钟,表示该系统在连续运行1年时间里最多可能的业务中断时间是52.6分钟。
5个9:(1-99.999%)*365*24*60=5.26分钟,表示该系统在连续运行1年时间里最多可能的业务中断时间是5.26分钟。
4,支持热部署:
可以在开机时进行升级和重启
5, 应用广泛:
大多互联网公司在用nginx、
6, nginx的网络模型是epool
epool:用户发起请求,epool模型会直接进行处理,效率高,无连接限制
select:用户发起一次请求,select模型会进行一次遍历扫描,导致性能差,同时只支持1024个,超过这个限制,很可能导致溢出
(举例:班级里有一位学生提问,老师从第一排开始筛选是谁提出的问题,一直筛选到最后一排才是那个提问的学生
epool模型是知道最后一排的学生提问,直接回答)
三, web的其他服务
1,apache
2,nginx
1)tengine:由淘宝发起的web服务器项目,在nginx的基础上,针对 网站的需求,添加了很多高级功能和特征,目的打造一个高效安全的web平台
2)openresty:基于nginx和Lua脚本语言的 高性能web平台(360,UPYUN,阿里云,新浪,腾讯网,去哪儿网,酷狗音乐等都是 OpenResty 的深度用户)
四, 应用场景
五,nginx的安装
1, epol源安装 (不常用)
[root@momobanji ~]# yum install -y nginx
2,官方源安装
http://nginx.org/en/linux_packages.html#RHEL-CentOS
下载前先修改配置文件,打开官网链接有介绍
1) 修改配置;
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
Active Internet connections (only servers)
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
~
2)安装依赖
[root@web01 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
3)下载
[root@web01 ~]# yum install -y nginx
4) 启动nginx
[root@web01 ~]# systemctl start nginx
5)验证
第一种方法:
[root@web01 ~]# netstat -lntp
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13207/nginx: master
第二种方法:
[root@web01 ~]# ps -ef|grep nginx
root 13207 1 0 03:53 ? 00:00:00 nginx: master process
第三种方法:
访问10.0.0.7
这一步出现报错,可查看端口80是否被httpd占用,查看进行杀死httpd即可
6) 常用命令
systemctl start nginx 开启
systemctl stop nginx 关闭
systemctl reload nginx 重新加载配置文件
nginx 开启
nginx -s stop 关闭
nginx -s reload 重新加载配置文件
systemctl restart nginx 重启,不常用(9999/99999)
检查配置
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl enable nginx 加入开机自启
Centos6 操作
#启动
nginx
/etc/init.d/nginx start
service nginx start
#开机自启
chkconfig nginx on
查找下载的路径----站点目录
[root@web01 ~]# rpm -qc nginx
/etc/nginx/conf.d/default.conf
3, 源码包安装 (可指定安装路径),指定装在server下面
1)安装插件 -必须要装
[root@web02 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
2) 下载
[root@web02 ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
3)解压
[root@web02 ~]# tar xf nginx-1.18.0.tar.gz
4) 创建用户
[root@web02 ~]# groupadd www -g 666
[root@web02 ~]# useradd www -u 666 -g 666
5)编译安装Nginx
[root@web02 nginx-1.18.0]# cd nginx-1.18.0/
[root@web02 nginx-1.18.0]# mkdir /server
---------------下面参数安装路径需要用到
安装好的前提下:
##查看当前版本
[root@web02 ~]# nginx -v
##查看模块
[root@web02 ~]# nginx -V
[root@web02 nginx-1.18.0]# ll
-rwxr-xr-x 1 1001 1001 2502 2020-04-21 22:09 configure
[root@web02 ~]# ./configure --prefix=/server/nginx-1.18.0 --user=www --group=www --with-http_addition_module --with-http_auth_request_module --without-http_gzip_module
参数:
./configure --help
--help 显示本提示信息
--prefix=PATH 设定安装目录
--sbin-path=PATH 设定程序文件目录
--conf-path=PATH 设定配置文件(nginx.conf)目录
--error-log-path=PATH 设定错误日志目录
--user=USER 设定程序运行的用户环境(www)
--group=GROUP 设定程序运行的组环境(www)
--with-http_addition_module 允许ngx_http_addition_module模块(mod_layout)
6)生成
[root@web02 nginx-1.18.0]# make && make install
##配置文件
[root@web02 nginx-1.18.0]# ll /server/nginx-1.18.0/conf/nginx.conf
-rw-r--r-- 1 root root 2656 2020-08-31 15:56 /server/nginx-1.18.0/conf/nginx.conf
7)做软链接
ln -s /server/nginx-1.18.0 /server/nginx
--1.配置system管理可以不加版本号
--2.升级的时候直接修改软连接即可
8)修改配置 (百度)
[root@web02 nginx-1.18.0]# vim /usr/lib/systemd/system/nginx.server
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/server/nginx/logs/nginx.pid
ExecStartPre=/server/nginx/sbin/nginx -t -c /server/nginx/conf/nginx.conf
ExecStart=/server/nginx/sbin/nginx -c /server/nginx/conf/nginx.conf
ExecReload=/server/nginx/sbin/nginx -s reload
ExecStop=/server/nginx/sbin/nginx -s stop
ExecQuit=/server/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
~
9)启动
[root@web02 nginx-1.18.0]# systemctl daemon-reload
[root@web02 nginx-1.18.0]# systemctl start nginx
[root@web02 nginx-1.18.0]# systemctl enable nginx
启动不了的话看一下进程端口是否被占用
4, 升级 (官方源去装)
1.下载新版本的包
[root@web02 ~]# wget http://nginx.org/download/nginx-1.19.2.tar.gz
2.解压
[root@web02 ~]# tar xf nginx-1.19.2.tar.gz
3.生成
[root@web02 ~/nginx-1.19.2]# ./configure --prefix=/server/nginx-1.19.2 --user=www --group=www --with-http_addition_module --with-http_auth_request_module --without-http_gzip_module
4.编译安装
[root@web02 ~/nginx-1.19.2]# make && make install
5.复制配置文件给新版本
[root@web02 ~/nginx-1.19.2]# cp /server/nginx-1.18.0/conf/nginx.conf /server/nginx-1.19.2/conf/
6.重做软连接
[root@web02 ~/nginx-1.19.2]# rm -rf /server/nginx
[root@web02 ~/nginx-1.19.2]# ln -s /server/nginx-1.19.2 /server/nginx
##查看配置文件
[root@web02 server]# /server/nginx/sbin/nginx -t
nginx: the configuration file /server/nginx-1.19.2/conf/nginx.conf syntax is ok
nginx: configuration file /server/nginx-1.19.2/conf/nginx.conf test is successful
7.重启nginx
[root@web02 /server]# systemctl stop nginx
[root@web02 /server]# systemctl start nginx
(重启不了看一下pid的文件)
5, nginx 添加模块
1.重新生成
[root@web02 ~/nginx-1.19.2]# rm -rf Makefile
[root@web02 ~/nginx-1.19.2]# ./configure --prefix=/server/nginx-1.19.2-new --user=www --group=www --with-http_addition_module --with-http_auth_request_module --without-http_gzip_module --with-http_stub_status_module
注意:不要覆盖原来的配置文件⚠️
2.编译安装
[root@web02 ~/nginx-1.19.2]# make && make install
3.复制配置文件给新版本
[root@web02 ~/nginx-1.19.2]# cp /server/nginx-1.19.2/conf/nginx.conf /server/nginx-1.19.2-new/conf/
4.重做软连接
[root@web02 ~/nginx-1.19.2]# rm -rf /server/nginx
[root@web02 ~/nginx-1.19.2]# ln -s /server/nginx-1.19.2-new /server/nginx
5.重启nginx
[root@web02 /server]# systemctl stop nginx
[root@web02 /server]# systemctl start nginx
三, nginx 相关配置文件
[root@web01 ~]# rpm -qc nginx
/etc/logrotate.d/nginx Nginx默认的日志切割
/etc/nginx/conf.d/default.conf 配置文件 - 默认网站配置文件
/etc/nginx/fastcgi_params Fastcgi代理配置文件
/etc/nginx/koi-utf Nginx编码转换映射文件
/etc/nginx/koi-win Nginx编码转换映射文件
/etc/nginx/mime.types Content-Type与扩展名
/etc/nginx/nginx.conf 配置文件 - nginx主配置文件
/etc/nginx/scgi_params scgi代理配置文件
/etc/nginx/uwsgi_params uwsgi代理配置文件
/etc/nginx/win-utf Nginx编码转换映射文件
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
四、nginx主配置文件
Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。
Nginx主配置文件整体分为三块进行学习,分别是CoreModule(核心模块),EventModule(事件驱动模块),HttpCoreModule(http内核模块)
五, 游戏搭建- -官网包
1,编写配置文件
[root@web01 code]# vim /etc/nginx/conf.d/supermali.conf
server {
listen 80;
server_name localhost;
location / {
root /code/supermali;
index index.html;
}
}
2, 根据配置文件创建站点目录
[root@web01 code]# mkdir /code
[root@web01 code]# cd /code
3,上传游戏代码
**MacBook-Pro:~ $ scp /Users/zy/Desktop/web小游戏/html5-mario.zip [email protected]:/code/
4, 解压文件并改名
[root@web01 code]# unzip html5-mario.zip
[root@web01 code]# mv html5-mario supermali
[root@web01 code]# ll
-rwxr-xr-x 1 root root 148142 2020-08-25 15:54 html5-mario.zip
drwxr-xr-x 3 root root 118 2014-11-07 21:42 supermali
这时候重启服务器依然是nginx的页面
[root@web01 code]# ll /etc/nginx/conf.d/
-rw-r--r-- 1 root root 119 2020-08-25 15:45 default.conf
-rw-r--r-- 1 root root 119 2020-08-25 15:45 supermali.conf
按照顺序读取的
5, 删除第一个文件并重启服务
[root@web01 code]# rm -rf /etc/nginx/conf.d/default.conf
[root@web01 code]# systemctl restart nginx
六, 小游戏搭建- 源码安装nginx下
1, 创建配置文件
----删除了注释,打开了日志,及增加站点目录
[root@web02 ~]# vim /server/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /code/xiangqi;
index index.html index.htm;
}
}
}
2, 根据配置文件创建站点目录
[root@web02 ~]# mkdir /code
3, 上传代码
**-Pro:~ zy$ scp /Users/zy/Desktop/web小游戏/Chinesechess.zip [email protected]:/code
4, 压缩并改名
[root@web02 ~]# cd /code/
[root@web02 code]# unzip Chinesechess.zip
[root@web02 code]# mv Chinesechess xiangqi
[root@web02 code]# ll
-rwxr-xr-x 1 root root 1599614 2020-09-02 21:14 Chinesechess.zip
drwxr-xr-x 5 root root 56 2014-11-11 15:46 xiangqi
七, 虚拟主机
虚拟主机的配置方式
- 基于IP的多虚拟主机
- 基于端口的多虚拟主机
- 基于域名的多虚拟主机
- - 基于IP
网卡添加IP地址
[root@web01 ~]# ifconfig eth1:1 10.0.0.222/24
网卡去掉IP地址
[root@web01 ~]# ifconfig eth1:1 down
[root@web01 ~]# ip a
eth1:mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:07:c4:cb brd ff:ff:ff:ff:ff:ff
inet 172.16.1.7/24 brd 172.16.1.255 scope global eth1
valid_lft forever preferred_lft forever
inet 10.0.0.222/24 brd 10.0.0.255 scope global eth1:1
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe07:c4cb/64 scope link
valid_lft forever preferred_lft forever
1,上传新的游戏代码包到code目录下, 解压
[root@web01 conf.d]# cd /code/
[root@web01 code]# ll
drwxr-xr-x 5 root root 56 2014-11-11 15:46 Chinesechess
-rwxr-xr-x 1 root root 1599614 2020-08-25 20:22 Chinesechess.zip
-rwxr-xr-x 1 root root 148142 2020-08-25 19:07 html5-mario.zip
drwxr-xr-x 3 root root 118 2020-08-25 19:01 supermali
2, 到站点目录新增文件
[root@web01 conf.d]# cd /etc/nginx/conf.d/
##增加并改IP
[root@web01 conf.d]# vim Chinesechess.conf
systemctl restart nginx
server {
listen 172.16.1.7:80;
server_name localhost;
location / {
root /code/Chinesechess;
index index.html index.htm;
}
##改IP
[root@web01 conf.d]# vim supermali.conf
server {
listen 10.0.0.7:80;
server_name localhost;
location / {
root /code/supermali;
index index.html;
}
}
~
3, 重启
[root@web01 code]# systemctl restart nginx
访问另一个IP地址(由于内网,curl)
[root@web01 code]# curl 172.16.1.7 也可以访问到
基于端口- -
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# ll
-rw-r--r-- 1 root root 198 2020-08-25 20:43 Chinesechess.conf
-rw-r--r-- 1 root root 128 2020-08-25 20:43 supermali.conf
[root@web01 conf.d]# vim Chinesechess.conf
server {
listen 80;
server_name localhost;
location / {
root /code/Chinesechess;
index index.html index.htm;
}
}
[root@web01 conf.d]# vim supermali.conf
server {
listen 81;
server_name localhost;
location / {
root /code/supermali;
index index.html;
}
}
[root@web01 conf.d]# systemctl restart nginx
基于域名- -
1, 配置本地hosts文件
mac本:打开Finder,快捷键shift+command+G输入/etc/hosts即可
windows:\windows\system32\drivers\etc\hosts,属性-> 安全,全勾上,用技术本打开
最后一行增加
10.0.0.7 www.mali.com www.Chinesechess.com
2, 配置nginx
[root@web01 conf.d]# vim supermali.conf
server {
listen 80;
server_name www.mali.com;
location / {
root /code/supermali;
index index.html;
}
}
~
[root@web01 conf.d]# vim Chinesechess.conf
server {
listen 80;
server_name www.Chinesechess.com;
location / {
root /code/Chinesechess;
index index.html index.htm;
}
}
[root@web01 conf.d]# systemctl restart nginx
源码安装下多配置:
1,修改配置文件:
---删除了server模块 增加粗线部分
[root@web02 conf.d]# vim /server/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
keepalive_timeout 65;
include conf.d/*.conf;
}
~
2, 到配置目录下新建站点目录
[root@web02 conf.d]# cd /server/nginx/conf/
[root@web02 conf.d]# mkdir conf.d
3,把站点目录的配置移到这里
[root@web02 conf.d]# vim conf.d/mali.conf
server {
listen 80;
server_name localhost;
location / {
root /code/xiangqi;
index index.html index.htm;
}
}
~
4, 查看配置文件
[root@web02 conf.d]# /server/nginx/sbin/nginx -t
nginx: the configuration file /server/nginx-1.19.2/conf/nginx.conf syntax is ok
nginx: configuration file /server/nginx-1.19.2/conf/nginx.conf test is successful
5, 重启
[root@web02 conf.d]# systemctl restart nginx
nginx日志
日志模块如下:
syntax: access_log 路径 中括号里面可不选
default: 默认路径 格式
context: 可配置在http sever location等层
日志常用变量:
$remote_addr # 记录客户端IP地址 (上一层IP地址)
$remote_user # 记录客户端用户名
$time_local # 记录通用的本地时间
$time_iso8601 # 记录ISO8601标准格式下的本地时间
(正常国人可读模式,例2020.9.14.23:11)
$request # 记录请求的方法以及请求的http协议(post get等)
$status # 记录请求状态码(用于定位错误信息) (200 304 等)
$body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent # 发送给客户端的总字节数
$msec # 日志写入时间,单位为秒,精度是毫秒。
$http_referer # 记录从哪个页面链接访问过来的
$http_user_agent # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录客户端IP地址 (客户端,源头的IP地址)
$request_length # 请求的长度(包括请求行, 请求头和请求正文)。
$request_time # 请求花费的时间,单位为秒,精度毫秒
[root@web01 ~]# vim /etc/nginx/nginx.conf
可修改以上日志变量
日志切割:
(系统自带)
[root@web01 ~]# vim /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily 每天切割一次
missingok 忽略日志丢失
rotate 52 保留52天
compress 日志压缩
delaycompress 延时日志压缩
not if empty 忽略空日志
create 640 nginx adm 日志授权
shared scripts 启动脚本
postrotate 脚本内容
# 判断nginx是否启动
if [ -f /var/run/nginx.pid ]; then
#重新生成access_log
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript #结束脚本
}
接上~
[root@web01 ~]# cd /var/log/nginx/
[root@web01 nginx]# ll
-rw-r----- 1 nginx adm 36774 2020-09-10 11:36 access.log
-rw-r----- 1 nginx adm 178935 2020-08-25 23:22 access.log-20200826
-rw-r----- 1 nginx adm 42644 2020-09-10 11:56 error.log
-rw-r----- 1 nginx adm 148762 2020-08-25 23:22 error.log-20200826
-rw-r--r-- 1 root root 24320 2020-09-10 11:56 www.mali.com.log
## 根据上一个脚本,每天会切割新的日志
[root@web01 logrotate.d]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# ll
-rw-r--r-- 1 root root 198 2020-08-25 23:05 Chinesechess.conf
-rw-r--r-- 1 root root 175 2020-09-10 11:54 supermali.conf
[root@web01 conf.d]# vim supermali.conf
server {
listen 80;
server_name www.mali.com;
access_log /var/log/nginx/www.mali.com.log main;
location / {
root /code/supermali;
index index.html;
}
}
。。。
如果不配置,默认在/var/log/nginx access.log中,多个sever的话不好区分
。。。
# 重新加载配置
[root@web01 ]# systemctl reload nginx
#查看日志,验证
[root@web01 ]# ll /var/log/nginx/www.mali.com.log
[root@web01 logrotate.d]# tailf -1 /var/log/nginx/www.mali.com.log
10.0.0.1 - - [10/Sep/2020:11:56:01 +0800] "GET /sounds/stomp.mp3 HTTP/1.1" 404 555 "http://www.mali.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36" "-"
nginx常用模块
sever下面的内容是由模块决定
[root@web01 ~]# vim /etc/nginx/conf.d/supermali.conf
server {
listen 80;
server_name www.mali.com;
access_log /var/log/nginx/www.mali.com.log main;
location / {
root /code/supermali;
index index.html;
}
}
目录索引模块:
该ngx_http_autoindex_module模块处理以斜杠字符(' /')结尾的请求,并生成目录列表。通常,ngx_http_autoindex_module 当ngx_http_index_module模块找不到索引文件时,会将请求传递给模块 。
[root@web01 code]# mkdir /code/auto
[root@web01 code]# echo "111 autoindex model" > /code/auto/index.html
[root@web01 ~]# vim /etc/nginx/conf.d/autoindex.conf
server {
listen 80;
server_name www.autoindex.com;
location / {
root /code/auto;
index index.html;
}
location /download {
root /code/auto;
autoindex on;
}
}
#创建目录
[root@web01 ~]# mkdir /code/auto/download
[root@web01 code]# echo "1234" > /code/auto/download/index
[root@web01 code]# cd /code/auto/
[root@web01 auto]# ll
drwxr-xr-x 2 root root 19 2020-09-10 22:32 download
-rw-r--r-- 1 root root 35 2020-09-10 22:24 index.html
[root@web01 auto]# ll download/
-rw-r--r-- 1 root root 5 2020-09-10 22:32 index
root@web01 ~]# systemctl reload nginx
域名加入到本地hosts文件中
10.0.0.7 www.mali.com www.Chinesechess.com www.autoindex.com
访问测试
# 增加模块的目录
[root@web01]# cd /code/auto/download/
[root@web01 download]# ll
-rw-r--r-- 1 root root 5 2020-09-10 22:32 index
[root@web01 download]# mkdir aaaa
[root@web01 download]# mkdir bbbb
[root@web01 download]# touch ccc
访问
[root@web01 download]# mkdir 开心
配置里增加字符集(不然会乱码)
[root@web01 download]# vim /etc/nginx/conf.d/autoindex.conf
server {
listen 80;
server_name www.autoindex.com;
charset utf-8;
........
访问
模块的时间与文件大小单位配置:
[root@web01 aaaa]# vim /etc/nginx/conf.d/autoindex.conf
server {
listen 80;
server_name www.autoindex.com;
charset utf-8;
location / {
root /code/auto;
index index.html;
}
location /download {
root /code/auto;
autoindex on;
autoindex_exact_size off;
# 关掉的文件大小是G 或M;默认打开是byte
autoindex_localtime on;
# 如果off 是GMT时间,与真实时间相差8h,打开即本地时间
}
--- 访问控制模块
[root@web01]# vim /etc/nginx/conf.d/autoindex.conf
server {
listen 80;
....
location /download {
root /code/auto;
autoindex on;
autoindex_exact_size off;
autoindex_localtime off;
allow 10.0.0.0/24;
deny all;
}
}
allow 10.0.0.0/24; 允许10.0.0.0网段访问,
deny all; 拒绝其他网段访问
allow 10.0.0.1; 允许IP地址为10.0.0.1访问,
deny all; 拒绝其他IP访问
deny 10.0.0.1; 拒绝IP地址为10.0.0.1访问
allow all; 允许其他IP访问
--以上配置在location /download层,代表访问该网址的download模块的限制;也可以配置在http层,代表访问该网址限制
-- 访问认证模块
# 创建密码文件,密码不是明文
[root@web01 ~]# htpasswd -c /etc/nginx/auth_basic zx
New password:
Re-type new password:
Adding password for user zx
-c 指定文件
也可以
[root@web01 ~]# htpasswd -cb /etc/nginx/auth_basic zx 123456
第二种查历史记录可以看见密码,建议第一种
# 查看密码文件为加密文件
[root@web01 ~]# cat /etc/nginx/auth_basic
zx:$apr1$kIyBbEwK$MErdsnnxmk4zUBBgx0iCu.
配置
[root@web01 ~]# vim /etc/nginx/conf.d/autoindex.conf
server {
listen 80;
server_name www.autoindex.com;
charset utf-8;
auth_basic "输入登陆用户密码"
# 后面的字只要不是off都可以,页面看不见
auth_basic_user_file /etc/nginx/auth_basic;
............
重启nginx
以上配置在sever层,代表访问该网址首页即需要用户和密码才能进入
也可以配置在location层
访问
多人使用不同密码:
去掉 -c 参数
不然会替代新的文件
-- nginx状态模块
配置:
[root@web01 ~]# vim /etc/nginx/conf.d/autoindex.conf
server {
listen 80;
...
location /download {
root /code/auto;
autoindex on;
autoindex_exact_size off;
autoindex_localtime off;
allow 10.0.0.0/24;
deny all;
auth_basic "输入登陆用户密码";
auth_basic_user_file /etc/nginx/auth_basic;
}
location /nginx_status {
stub_status;
重启nginx
访问
nginx状态页面:
活跃的连接数 sever是状态
accepts 120 tcp连接数
handled 120 成功的连接数
requests 877 总共处理的请求数
不断的刷新,requests值不断变化,accepts和handled 一定值才会变化
因为开启了长连接
[root@web01 ~]# vim /etc/nginx/nginx.conf
keepalive_timeout 65;
一次连接多次访问
关闭长连接
[root@web01 ~]# vim /etc/nginx/nginx.conf
keepalive_timeout 0;
## 如果打开长连接是浏览器到负载均衡是一次长连接;
## 负载均衡到后段nginx,nginx到php是短连接;需要再配置
-- 连接限制模块
限制一部分连接,如果没有限制的连接服务器,会崩溃
语法 - 设置一个空间
调用空间定义模块 空间中存储的内容 空间名及大小
Syntax:limit_conn_zone key zone=name:size;
Default:—
Context:http 只能写在http层
调用上面的空间
Syntax:limit_conn zone number;
Default:—
Context:http, server, location
从下面文件里找空间的变量值给下面的配置用
[root@web01 ~]# vim /etc/nginx/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"';
配置设置空间:只能放在http层(sever的上面或者/etc/nginx/nginx.conf最下面)
[root@web01 ~]# vim /etc/nginx/conf.d/autoindex.conf
limit_conn_zone $remote_addr zone=kongjian:1kb;
server {
listen 80;
server_name www.autoindex.com;
charset utf-8;
......
记录客户端IP地址的空间大小为1kb
调用
[root@web01 ~]# vim /etc/nginx/conf.d/autoindex.conf
limit_conn_zone $remote_addr zone=kongjian:1kb;
server {
listen 80;
server_name www.autoindex.com;
charset utf-8;
limit_conn kongjian 1;
... ....
比如10.0.0.1访问,限制不同网段的10.0.0.1只允许一个访问
-- 请求限制模块
设置限制请求的空间 - 语法:
Syntax:limit_req_zone key zone=name:size rate=rate [sync]; 速率1r/s每秒一次请求
Default:—
Context:http
调用
Syntax:limit_req zone=name [burst=number] [nodelay | delay=number];
Default:—
Context:http, server, location
配置:
[root@web01 ~]# vim /etc/nginx/conf.d/autoindex.conf
limit_conn_zone $remote_addr zone=limit_conn:1kb;
limit_req_zone $remote_addr zone=limit_req:1m rate=1r/s; 设置空间
server {
listen 80;
server_name www.autoindex.com;
charset utf-8;
limit_conn limit_conn 1;
limit_req zone=limit_req; 调用
location / {
... ....
访问页面快速刷新,会提示503报错
代码文件不用配置,一个代码里会出现多个请求
burst=26 可以加个配置,一次请求不够增加的请求
测试请求
[root@web01 ~]# ab -n 200 -c 2 http://www.autoindex.com/
nginx的location
-- 官网的ngx_http_core_module模块里的
1, 语法:
Syntax:location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default:—Context:server, location
URL:http://www.tank.com/index,html
uri : /index.hetml 域名后的就是uri
2, location的匹配符
3,验证location优先级
[root@web01 ~]# vim /etc/nginx/conf.d/location.conf
server {
listen 80;
server_name linux.location.com;
location / {
default_type text/html;
return 200 "location /";
}
location =/ {
default_type text/html;
return 200 "location =/";
}
location ~ / {
default_type text/html;
return 200 "location ~/";
}
location ~* / {
default_type text/html;
return 200 "location ~*";
}
#location ^~ / { 注释的内容等于 /
# default_type text/html;
# return 200 "location ^~";
#}
}
[root@web01 ~]# nginx -t 检查配置
[root@web01 ~]# systemctl reload nginx
域名加入本地hosts里
访问 http://linux.location.com/
可以此类推,注释再访问测试优先级