一、http协议
1)概述
HTTP:Hypertext Transfer Protocol,超文本传输协议
http协议默认端口:80
http超文本传输协议:数据请求与响应
传输:网站的数据如何传递给用户
超文本:文本、图片、视频...
用户打开网站后:网站如何传递数据给用户
请求request:打开网站、访问网站
响应response:网站显示出来,返回给你想要的内容
案例01:通过curl或wget访问网站并显示详细过程
curl -Lv 网站
wget –debug 网站
#request:请求
#response:响应
[root@web01 ~]#curl -Lv baidu.com
* About to connect() to baidu.com port 80 (#0)
* Trying 110.242.68.66...
* Connected to baidu.com (110.242.68.66) port 80 (#0)
> GET / HTTP/1.1 # >:请求。<:响应
> User-Agent: curl/7.29.0
> Host: baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sun, 18 Jun 2023 07:29:18 GMT
< Server: Apache
< Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
< ETag: "51-47cf7e6ee8400"
< Accept-Ranges: bytes
< Content-Length: 81
< Cache-Control: max-age=86400
< Expires: Mon, 19 Jun 2023 07:29:18 GMT
< Connection: Keep-Alive
< Content-Type: text/html
<
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>
* Connection #0 to host baidu.com left intact
[root@web01 ~]#
2)http协议版本
比较项目 | http 1.0 | http 1.1 | http 2.0 | http 3.0 |
---|---|---|---|---|
特点 | 短连接,每次请求都要重复建立、断开连接 | 加入长连接 | 增加并发,访问更快 | 基于udp,更快,应用于流媒体 |
占用服务端资源 | keepalive功能(网站响应后不会立刻断开,保留下这个连接 | |||
是否加密 | http不加密 https加密 | 默认基于https | ||
基于tcp/udp | tcp | tcp | tcp | udp |
目前,大部分还在使用htpp1.1,一部分使用http2.0
http3.0(QUIC)流媒体直播在用
1.1 vs 2.0 速度对比:https://https2.sinacloud.com/
3)http协议详解
HTTP报文:请求报文、响应报文
3.1 http请求报文
http请求起始行 | GET / HTTP/1.1 | 请求起始行:做什么、找什么 |
---|---|---|
http请求头(head) | User-Agent: Wget/1.14 (linux-gnu) Accept: / Host: baidu.com Connection: Keep-Alive | 请求头: 自报家门 指定网站 |
空行(分隔) | 分隔请求主体 | |
请求报文主体(body)(一般是上传才有) |
a)请求起始行解释:
GET / HTTP/1.1
第1部分:请求方法(上传、下载),GET:下载,POST:上传,HEAD:类似于GET,仅仅输出响应的头部信息
第2部分:资源位置,url,从网站的根目录(/)开始
第3部分:http协议版本,如:HTTP/1.1
请求方法:用于指定客户端如何访问服务端(下载、上传、查看服务端信息)
常见的请求方法 | 说明 |
---|---|
GET | 下载(大部分请求) |
POST | 上传(上传文件、登录) |
HEAD | 类似于GET,仅仅输出响应的头部信息(查看服务端的信息,一般用于检查) |
使用curl -I选项,就是HEAD请求,如下:
[root@web01 ~]#curl -Lv -I jd.com
* About to connect() to jd.com port 80 (#0)
* Trying 111.13.149.108...
* Connected to jd.com (111.13.149.108) port 80 (#0)
> HEAD / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: jd.com
> Accept: */*
资源的位置(URL):这个资源在网站站目录的哪个地方,叫什么名字。上面的根(/)是网站目录的根,而不是系统的根目录。
url:统一资源标识符
站点目录是用于存放网站代码的目录
案例02:测试www.baidu.com是否可以通过http访问
curl -I www.baidu.com
发出HEAD请求,查看服务端信息,服务端是否可以访问
b)请求头
User-Agent: 客户端访问工具,如:浏览器、curl命令、wget命令
Host:表示访问的目标网站:域名/ip
c)其他
空行:分隔请求头与请求报文主体
请求报文主体(body):一般上传的时候才有
d)浏览器调试查看:
浏览器的调试功能;DevTools
F12或Fn+F12查看“网络”部分
3.2 http响应报文
内容示例:
http响应起始行 | HTTP/1.1 200 OK |
---|---|
http响应头(head) | Date: Sun, 18 Jun 2023 08:40:56 GMT Server: Apache Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT ETag: “51-47cf7e6ee8400” Accept-Ranges: bytes Content-Length: 81 Cache-Control: max-age=86400 Expires: Mon, 19 Jun 2023 08:40:56 GMT Connection: Keep-Alive Content-Type: text/html |
空行(分隔) | |
响应报文主体(body)(要的文件、视频、文件…) |
1)响应起始行
HTTP/1.1 200 OK
第1部分:http协议版本
第2部分:状态码,用3个数字表示。用于描述服务端是否能找到或处理用户的请求
第3部分:状态码的描述,比如:OK、NOT FOUND、Moved Permanently等
2)响应头
响应头字段 | 说明 |
---|---|
Server | 显示服务端使用的web服务器及版本 |
Content-Type | 媒体类型(文件类型) |
Content-Length | 大小 |
Location | 跳转之后的新的位置(未来rewrite 301/302跳转的时候才有) |
3)其他
空行:分隔用
响应报文主体(body):服务端返回给客户端的数据
4)状态码
状态码:错误提示、反映出服务端是否能够正常的处理用户请求
状态码 | 含义 |
---|---|
2xx | 表示正常 |
3xx | 表示需要跳转,表示正常 |
4xx | 表示异常,客户端问题 |
5xx | 表示异常,服务端问题 |
详细说明:
详细的状态码 | 说明 |
---|---|
200 OK | 访问正常 |
301 Moved Permanently | 永久跳转 |
302 Found或Moved Temporarily | 临时跳转 |
304 Not Modified | 浏览器缓存 |
403 Forbidden | 权限拒绝(拒绝访问),1、权限问题,2、首页文件问题 |
404 Not Found | 文件找不到,一般辅助错误、日志排查 |
500 Internal Error | 内部错误,SELINUX开启,其他原因,一般辅助错误、日志排查 |
502 Bad Gateway | 网关错误,一般发生在负载中(类似场景),请求发送到后面,后面无人处理,提示502 |
503 service temporarily unavailable | 服务临时不可用,后端负载异常等情况,人为设置(升级)等 |
504 Gateway Time-out | 网关超时 |
2、衡量访问量指标
衡量系统访问量的指标
指标 | 说明 |
---|---|
IP | 访问网站的独立ip数量,公网ip |
PV | 页面访问量Page View |
UV | 独立访客数量,接近于用户数量,Unique Vistor |
DAU | 每天的活跃用户数量:日活(日活跃用户) |
MAU | 月活跃用户 |
统计:
IP、PV、UV:三剑客、第三方统计工具、网站页面加入代码、ELK
DAU、MAU:第三方工具、数据库统计、用户登录情况
3、web服务
3.1 web服务
web服务:网站服务
web中间件:等同于web服务
中间件:范围更广,指的是负载均衡之后的服务
数据库中间件:数据库缓存、消息队列…
3.3 常见网站服务
网站服务 | 说明 | 官网 |
---|---|---|
nginx | 大部分使用nginx,Engine X | http://nginx.org/en/docs/ |
tengine | 基于nginx二次开发,淘宝开源软件,更多的内置模块 | |
openresty | 基于nginx二次开发,加强Lua功能与模块 | |
Tomcat、Jboss、weblogic | 运行java环境的web服务 | |
php | 运行php环境,需要nginx | |
Apache | httpd | |
……. |
二、nginx
1、配置yum源
nginx官网:http://nginx.org/en/download.html
yum源配置方法:http://nginx.org/en/linux_packages.html#RHEL
[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
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
2、 安装nginx
[root@web01 ~]#yum install -y nginx
3、nginx配置文件
3.1 nginx包含的文件
yum安装之后的:
[root@web01 ~]#rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.24.0
/usr/share/doc/nginx-1.24.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx
[root@web01 ~]#
3.2 目录结构
nginx不同的安装方法,其目录、文件会有所不同。
目录结构 | 说明 |
---|---|
/etc/nginx/ | nginx各种配置的目录 |
/etc/nginx/nginx.conf | 主配置文件 |
/etc/nginx/conf.d/ | 子配置文件目录(针对不同网站的配置文件) |
/etc/nginx/conf.d/default.conf | 默认的子配置文件 |
/usr/sbin/nginx | nginx命令 |
/usr/share/nginx/html/ | nginx默认的站点目录,网站的根目录 |
/var/log/nginx/ | nginx日志:访问日志、错误日志、跳转日志 |
其他的目录和文件:
其他目录和文件 | 说明 |
---|---|
/etc/logrotate.d/nginx | 日志切割(防止文件过大) |
/etc/nginx/mime.types | 媒体类型 |
/etc/nginx/fastcgi_params | nginx+php |
/etc/nginx/uwsgi_params | nginx+python |
/usr/lib/systemd/system/nginx.service | nginx的systemctl配置文件 |
/var/cache/nginx/ | 缓存目录 |
3.3 启动与管理
# 启动
systemctl starat nginx
#开机启动
systemctl enable nginx
#检查服务状态
systemctl status nginx
#检查端口
ss -lntup | grep 80
#检查进程
ps -ef | grep nginx
ps aux | grep nginx
nginx进程分两类:master(1个)、worker(1个或多个)
4 、nginx核心功能详解⭐️
4.1 主配置文件
默认的主配置文件(/etc/nginx/nginx.conf)内容如下:
#第1部分:核心配置部分:设置进程用户、进程数量、日志文件、pid文件
user nginx; #指定nginx所属的用户(虚拟用户),即工具人进程用户,
#也就是nginx进程使用哪个用于运行,默认是nginx
worker_processes auto; #设置工具人进程(worker进程)数量,即处理用户请求的进程数量,默认是auto,自动
error_log /var/log/nginx/error.log notice; #错误日志文件及收集的类型,默认收集的类型是:notice
pid /var/run/nginx.pid; # pid文件
#第2部分:event区域:设置每个进程处理连接数量、iOS模型use epod设置
events {
worker_connections 1024; #工具人进程可以处理多少个连接(每个工具人进程最大的连接数)
}
#第3部分:http区域:7层功能网站配置等
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"'; # 用于提高nginx性能的参数
#以下部分:指定nginx访问日志,格式叫做main,访问里面记录什么内容
#使用之前指定的格式即上面log_format main定义的格式,记录访问日志
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65; #长连接时长,65秒
#gzip on;
#include:文件包含或引用功能,用于在nginx配置文件中调取其他文件
#引用nginx子配置文件
include /etc/nginx/conf.d/*.conf;
}
4.2 子配置文件
子配置文件存放目录:/etc/nginx/conf.d/
默认的子配置文件为:/etc/nginx/conf.d/default.conf,内容如下:
一个server{}表示一个网站。
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
去掉注释和空白行如下:
主要结构是server{},一个server{}区域表示一个网站站点。
多个站点,其端口可以相同,但是,域名(server_name)不能相同
server {
listen 80; #监听web服务的端口,默认是80
server_name localhost; #网站域名,可以写多个,域名之间以空格分割
location / { #用于匹配用于请求的url,location / 表示默认
root /usr/share/nginx/html; #用于指定站点目录,也就是网站代码的目录
index index.html index.htm; #指定首页文件,也就是只输入域名或ip的时候默认展示的页面
}
#下面的配置是,指定出现500、502、503、504错误的时候显示/50x.html文件
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
5 、站点部署演示
网站要求:
域名:cxk.test.com
端口:82
站点目录:/app/code/cxk/
首页文件:index.html
动态、静态资源:
nginx只能处理静态资源,比如:html、css、js
(java、Go、Python)动态资源指的是:用户上传、用户注册、用户刷礼物、用户评论...
5.1 配置文件
[root@web01 ~]#vim /etc/nginx/conf.d/cxk.conf
server {
listen 82;
server_name cxk.test.com;
root /app/code/cxk;
location / {
index index.html;
}
}
5.2 创建站点目录及文件
创建/app/code/cxk/目录及index.html文件
#/app/code/cxk/目录属主属组:nginx
[root@web01 ~]#mkdir -p /app/code/cxk/
[root@web01 ~]#chown -R nginx.nginx /app/code/cxk/
#index.html文件
[root@web01 ~]#echo 'The First web!' > /app/code/cxk/index.html
5.3 重启nginx
#1、检查配置文件是否有错
[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
[root@web01 ~]#
#结果显示,没有错误。
#2、重启nginx
[root@web01 ~]#nginx -s reload
#或者
[root@web01 ~]#systemctl restart nginx
#或者
[root@web01 ~]#systemctl reload nginx
浏览器打开:192.168.10.7:82
显示如下,则表示部署成功
6、处理请求流程
nginx处理用户请求流程:
第1步:假设用户浏览器打开www.test.com,解析到假设对应ip为:10.0.0.7
请求报文头为:
GET /index.html HTTP/1.1
Host:10.0.0.7
User-agent:Chrome/XXX
第2步:服务器10.0.0.7接到用户的请求,请求到达nginx后,
接到用户请求的报文:
GET /index.html HTTP/1.1
Host:www.test.com
User-agent:Chrome/XXX
1)先匹配http区域
nginx.conf:
核心区域
events区域
http 区域 {
http/https 请求
匹配子配置文件
直接找这里的server{}
}
2)根据用户请求的域名www.test.com与配置文件中的server_name 进行匹配
子配置文件1:default.conf(默认)
子配置文件2:cxk.conf
比如,匹配到cxk.conf,然后找url,比如是:/index.html
nginx进行转换,得到站点目录下的/index.html,比如/app/code/bird/index.html
3)找了/app/code/bird/index.html,然后响应给用户
比如,响应报文如下:
HTTP/1.1 200 ok
Server:nginx/1.24.0
文件大小:xxx
文件类型:xxx
文件内容:xxx
4)用户收到服务端的响应报文,浏览器进行解析与运行
注意:与域名不同的是
如果使用ip访问,前面的流程与上面的一样。不同的是子配置文件的匹配:
如果子配置文件的监听端口都是80,则按照子配置文件的文件名的首字母(数字)的先后顺序匹配。
比如,子配置文件有两个:a.conf、b.conf,则优先匹配处理a.conf,然后处理结果返回给用户。而不再匹配b.conf或其他子配置文件进行处理。
设置默认站点:
server{}区域中,在“linsten 端口“后面加上”default_server“就设置为默认站点
代码示例如下:
server {
listen 80 default_server; #设为默认站点
server_name cxk.test.com;
root /app/code/cxk;
location / {
index index.html;
}
}
用户请求nginx:
1、DNS解析域名--->ip地址
2、通过ip+端口,三次握手,建立连接
3、http请求:
GET / HTTP/1.1
Host:域名
User-agent:Chrome/XXX
4、请求通过建立的连接80端口,到达nginx,nginx开始处理,http区域处理
5、用户请求的域名与子配置文件中的server_nam部分进行匹配
如果匹配成功,则对应的server{}站点处理用户请求
如果匹配不成功,由默认的站点(default_server标记或者按照顺序的第1个)进行处理,
6、站点处理用户请求的时候,根据用户请求的url+站点目录进行处理(location规则)
7、处理完成把结果发回给用户
7、虚拟主机
虚拟主机:相当于是1个网站,在nginx中通过server{}区域实现
7.1 概述与分类
虚拟主机分类 | 说明 | 应用场景 |
---|---|---|
基于域名的虚拟主机 | 不同域名访问不同的站点 | 生产环境中最常用(域名不同,端口是80、443) |
基于端口的虚拟主机 | 不同端口访问不同的站点 | 保护,设置特殊端口,一般是1024以上 |
基于ip的虚拟主机 | 不同ip访问不同的站点 | 保护,用户只能通过某个ip连接进来,用来限制网站只能通过指定的ip进行访问内网ip,vpn ip |
7.2 基于域名的虚拟主机🌟
不同域名访问不同的主机
案例:创建test1.com、test2.com网站
网站目录:/app/code/test1、/app/code/test2
web01:ip:192.168.10.7
1)创建站点目录
[root@web01 ~]#mkdir /app/code/{test1,test2}
2)站点配置文件
[root@web01 ~]#vim /etc/nginx/conf.d/test1.conf
server {
listen 80;
server_name test1.com;
root /app/code/test1;
location / {
index index.html;
}
}
[root@web01 ~]#vim /etc/nginx/conf.d/test2.conf
server {
listen 80;
server_name test2.com;
root /app/code/test2;
location / {
index index.html;
}
两个站点的端口都是80
3)创建测试页
#1、test1的index.html
[root@web01 ~]#echo 'Test1.com' > /app/code/test1/index.html
#2、test2的index.html
[root@web01 ~]#echo 'Test2.com' > /app/code/test2/index.html
4)重启nginx
[root@web01 ~]#systemctl reload nginx
5)Windows端hosts解析
在Windows的hosts文件(wind7系统:C:\Windows\System32\drivers\etc)做域名解析。
192.168.10.7 test1.com
192.168.10.7 test2.com
6)测试
浏览器分别打开:test1.com、test2.com
效果:
test1.com:
test2.com:
OK,虽然端口都是80,但是域名不同,则访问不同的站点。
7.3 基于端口的虚拟主机
不同端口访问不同的主机
还是以上面的test1、test2为例,修改两者的配置文件:
test1的端口:81
test2的端口:82
域名均为:localhost
[root@web01 ~]#vim /etc/nginx/conf.d/test1.conf
server {
listen 81;
server_name localhost;
root /app/code/test1;
location / {
index index.html;
}
}
[root@web01 ~]#vim /etc/nginx/conf.d/test2.conf
server {
listen 82;
server_name localhost;
root /app/code/test2;
location / {
index index.html;
}
重启nginx后,通过ip:端口的形式访问网站
浏览器分别打开:192.168.10.7:81、192.168.10.7:82
192.168.10.7:81:
192.168.10.7:82:
相同的ip,相同的域名,不同的端口,通过不同的端口访问不同的站点。
7.4 基于ip的虚拟主机
web01主机ip:192.168.10.7(eth0)、172.16.1.7(eth1)
临时增加一个ip:192.168.10.17
[root@web01 ~]#ifconfig eth0:1 192.168.10.17 broadcast 192.168.10.255 netmask 255.255.255.0
还是以前面的test1、test2为例,修改两者的配置文件:
[root@web01 ~]#vim /etc/nginx/conf.d/test1.conf
server {
listen 192.168.10.7:80;
server_name localhost;
root /app/code/test1;
location / {
index index.html;
}
}
[root@web01 ~]#vim /etc/nginx/conf.d/test2.conf
server {
listen 192.168.10.17:80;
server_name localhost;
root /app/code/test2;
location / {
index index.html;
}
端口一样,域名一样,ip不一样。
重启nginx:systemctl restart nginx,不能使用systemctl reload nginx
进程如下:
[root@web01 ~]#ss -tnlp | grep nginx
LISTEN 0 128 192.168.10.17:80 *:* users:(("nginx",pid=3980,fd=7),("nginx",pid=3979,fd=7))
LISTEN 0 128 192.168.10.7:80 *:* users:(("nginx",pid=3980,fd=6),("nginx",pid=3979,fd=6))
浏览器分别打开:192.168.10.7、192.168.10.17
192.168.10.7:
192.168.10.17:
这就是基于ip的虚拟主机
另外,基于ip的虚拟主机,有利于安全,因为只能用指定的ip访问。
web01有两个ip:192.168.10.7、172.16.1.7
但是test1中配置指定的ip(192.168.10.7)访问,如果使用172.16.1.7访问则会被拒绝,如下:
[root@web01 ~]#curl 172.16.1.7
curl: (7) Failed connect to 172.16.1.7:80; Connection refused
[root@web01 ~]#curl 192.168.10.7
Test1.com
[root@web01 ~]#
因此,基于ip的虚拟主机有利于安全。
未来,如果为了更安全,可以使用基于ip和端口相结合。
8、nginx日志
8.1 概述
日志类型
日志类型 | 使用建议 | 定义 | 使用 |
---|---|---|---|
错误日志 | 发生故障的时候可以查看,比如:4xx、5xx | 通过错误级别指定 | error_log |
访问日志 | 记录着用户什么时候访问网站哪些页面、客户端信息 | 通过log_format定义访问日志的格式 | access_log |
跳转日志 | 301、302跳转 |
8.2 错误日志
指定错误日志的位置和错误级别(日志级别)
error_log指令
格式:error_log 文件名 错误日志级别;
指令放在哪:main、http、mail、stream、server、location
错误日志级别:从左到右,越来越粗糙(记录信息的详细程度)
debug、info、notice、warm、error、crit、alert、or emerg。
error:默认级别
notice:建议使用
debug:未来用于调试使用,短时间开启,网站访问量较大就不要开
生成建议:给每个虚拟主机指定单独的错误日志,建议把主配置文件里面的error_log注释
8.3 访问日志
访问日志在默认在主配置文件http{}区域中定义。也可以在子配置文件中定义
access_log指令
格式:access_log 文件名 日志格式名称
放在哪:http、server、location、if in location、limit_except
日志格式定义:
log_format 日志格式名称 '参数'
日志格式在在主配置文件http{}区域中定义
默认的访问日志格式:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
访问日志格式参数说明:
nginx访问日志格式(内置变量) | 说明 |
---|---|
$remote_addr | 客户端ip地址 |
$remote_user | 用户名(一般是空(-),nginx进行认证用户) |
$time_local | 时间,格式:30/Aug/2022:14:44:27+0800 |
$request | 请求报文的起始行(请求方法 URL HTPP/1.1) |
$status | http状态码 |
$body_bytes_sent | 响应给客户的文件的大小,响应报文的主体大小(文件大小),单位:字节 |
$http_referer | 从哪里跳转,访问到这个网站的,用于网站运营分析 |
$http_user_agent | 客户端代理(浏览器) |
$http_x_forwarded_for | XFF头,负载中使用,记录用户真实的ip地址 |
例如:
192.168.10.1 - - [19/Jun/2023:00:27:00 +0800] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" "-"
解析:
$remote_addr :192.168.10.1
$remote_user :-
[$time_local] : [19/Jun/2023:00:27:00 +0800]
$request: "GET / HTTP/1.1"
$status: 200
$body_bytes_sent:10
$http_referer:-
$http_user_agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
$http_x_forwarded_for : "-"
访问日志其他选项:
访问日志进行压缩:gzip,需要通过zcat、zless、zgrep查看
进行缓存:buffer=32k,先把日志写入内存中,定期写入磁盘
定义刷新时间:flush=10s,每10秒刷新一次
例如:
access_log /var/log/1.log main gzip buffer=32k flush=10s;
9、location规则
9.1 location概述
nginx的location规则:
在ngx用于匹配用户请求中的uri,ngx对用户请求中的uri进行判断
如果用户请求的uri是xxx,则做xxx
有的地方给location规则也叫路由规则
URI vs URL:
URL网址:https://nginx.org/en/docs/
URI:/en/docs/
URL:即域名之后的内容
案例01:搭建网站
域名:buy.test.com
站点目录:/app/code/buy 首页文件:index.html
后台管理页面:/app/code/buy/admin/index.html
要求后台只能内网访问:172.16.1.0/24网段
网站目录及页面文件:
#1、网站目录
[root@web01 ~]#mkdir -p /app/code/buy
#2、首页文件
[root@web01 ~]#echo buy.. > /app/code/buy/index.html
#3、后台管理目录、及页面
[root@web01 ~]#mkdir -p /app/code/buy/admin
[root@web01 ~]#echo admin buy.. > /app/code/buy/admin/index.html
网站配置文件:
[root@web01 ~]#vim /etc/nginx/conf.d/bug.conf
server {
listen 80;
server_name buy.test.com;
root /app/code/buy;
error_log /var/log/nginx/buy_error.log notice;
access_log /var/log/nginx/buy_access.log main;
location / {
index index.html;
}
}
不做域名解析的情况,用域名访问:
[root@web01 ~]#curl -v -H Host:buy.test.com 192.168.10.7
-H:指定域名
命令行中的访问测试:
[root@web01 ~]#curl -H Host:buy.test.com 192.168.10.7/admin/
admin buy..
[root@web01 ~]#curl -H Host:buy.test.com 192.168.10.7
buy..
[root@web01 ~]#
后台管理页面所有ip都能访问:
要求后台只能内网访问:172.16.1.0/24网段:
#增加location规则
#buy.test.com/admin/
#uri:/admin/
location /admin/ { #或者/admin
#只允许172.16.1.0/24网段访问
allow 172.16.1.0/24;
#拒绝所有
deny all;
}
设置了location规则后,只有172.16.1.0/24网段的ip才能访问后台管理页面,其他ip访问则拒绝:
9.2 正则匹配
location ~ 正则 {}
案例02:给案例01的网站设置缓存,网站中的html、js、css结尾的文件缓存1天,图片缓存1小时
浏览器缓存
#匹配css、js、html结尾的文件
#通过正则匹配
location ~ \.(css|js|html)$ {
expires 1d; # expores 时间,设置缓存,1d:1天
#expires max; #缓存最大值,即
}
#图片缓存
location ~ \.(gif|jpg|png|)$ {
expires 1h; #1h:1小时
}
9.3 location规则
location规则 | 说明 |
---|---|
⭐️location / {} | 默认规则,保底。当其他所有规则都匹配失败的时候,就匹配默认规则 |
⭐️location /image/ {} | 用于匹配请求的uri(路径),即:ip或域名/image/ |
⭐️location ~ \.(jpg|png) {} | 支持正匹配,区分大小写 |
⭐️location ~* \.(jpg|png) {} | 支持正匹配,不区分大小写 |
location ^~ /abc/ {} | 不支持正则。仅仅匹配普通字符,很少使用,优先 |
location = /50x.html {} | 不支持正则,精确匹配,使用较少 |
location @ 名字 {} | 命名的location,一般用于return/error_log跳转 |
前面4种较为常用
案例03:部署china代码
网站目录:/app/code/china/
如果访问.js文件,则访问/app/code/china/js
如果访问.css文件,则则访问/app/code/china/css
server {
listen 80;
server_name china.test.com;
error_log /var/log/nginx/china_error.log notice;
access_log /var/log/nginx/china_access.log main;
root /app/code/china;
location / {
index index.html;
}
location ~* .js$ {
expires 1d;
root /app/code/china/js;
}
location ~* .css$ {
expires 2d;
root /app/code/china/css;
}
}
9.4 location匹配优先级
优先级 | 符号 |
---|---|
1 | = |
2 | ^~ |
3 | ~ 、~* |
4 | /xxx/ |
5 | / |
优先级:1最高、5最低
例如:
server {
listen 80;
server_name test001.com;
default_type text/html;
location / {
return 200 "location /\n";
}
location = /index.html {
return 200 "location =/\n";
}
location ~ /index.html {
return 200 "location ~ /\n";
}
location ^~ /index.html {
return 200 "location ^~\n";
}
}
测试:
[root@web01 /etc/nginx/conf.d]#curl -H test001.com 192.168.10.7/index.html
location =/
[root@web01 /etc/nginx/conf.d]#curl -H test001.com 192.168.10.7
location /
[root@web01 /etc/nginx/conf.d]#curl -H test001.com 192.168.10.7/index/index.html
location ~ /
[root@web01 /etc/nginx/conf.d]#curl -H test001.com 192.168.10.7/haha
location /
[root@web01 /etc/nginx/conf.d]#
10、案例
搭建一个站点:
域名:v.test.com
站点目录:/app/code/v
创建几个文件
要求:
1、浏览器打开后,显示目录结构,而不是显示index.html
2、增加vip认证功能
3、增加统计功能,统计nginx服务状态、访问情况情况等。
1、创建站点目录及文件
[root@web01 ~]#mkdir /app/code/v
[root@web01 ~]#touch /app/code/v/1.txt
[root@web01 ~]#touch /app/code/v/2.txt
[root@web01 ~]#mkdir /app/code/v/svip/
[root@web01 ~]#touch /app/code/v/svip/sip{01..10}.avi
2、配置文件
[root@web01 ~]#vim /etc/nginx/conf.d/v.test.conf
server {
listen 83;
server_name v.test.com;
error_log /var/log/nginx/v-error.log notice;
access_log /var/log/nginx/v-access.log main;
root /app/code/v/;
autoindex on; #如果首页文件不存在,则显示站点目录列表
autoindex_exact_size off;
autoindex_localtime on;
charset utf8;
location / {
index index.html;
}
location /svip/ {
auth_basic "Enter username and password:"; #输出提示,根据不同的浏览器,可能不显示
auth_basic_user_file /etc/nginx/passwd; #指定用户名、密码文件
}
location /status {
stub_status;#stub_status模块/指令,显示nginx服务状态
}
}
3、创建用户认证密码文件
#1、安装httpd-tools
[root@web01 ~]#yum install -y httpd-tools
#2、创建密码文件
#htpasswd -bc 密码文件 用户名 密码
[root@web01 ~]#htpasswd -bc /etc/nginx/passwd test 12345
Adding password for user test
#3、再添加一个用户test01,密码12345,由于密码文件已经存在,所以不能使用-c选项,否则会被覆盖
[root@web01 ~]#htpasswd -b /etc/nginx/passwd test01 12345
Adding password for user test01
#4、修改权限
[root@web01 ~]#chmod 600 /etc/nginx/passwd
[root@web01 ~]#chown nginx.nginx /etc/nginx/passwd
参数解释:
autoindex on:开启目录索引功能(首页文件index.html不存在的时候显示站点目录下的文件的列表)
autoindex_localtime on:显示本地时间
autoindex_exact_size off:是否显示精确的文件的大小,off表示以人类可读形式显示大小
3、测试
重启nginx后,浏览器打开192.168.10.7:83
如果要访问svip的内容,则需要认证:
nginx服务状态:
4、nginx服务状态
Active connections: 当前已经建立的连接数(est)和等待数量
server accepts :已经接收到客户端的连接总是
handled :服务端已经处理的连接
requests :客户端发出请求的总数
Reading:正在读取的请求头连接数量
Writing:正在进行的响应的连接数量
Waiting:排队数量
压力测试:ab(apache bench)
[root@web01 ~]#ab -n 9999999 -c 3 -H v.test.com http://192.168.10.7/
-n:次数,-c:并发,同时处理多个个