三大功能
1.网页服务:自身是静态web服务,还支持动态web服务
{PHP(fastcgi_pass)}
{JAVA(proxy_pass)}
{Python(uwsgi_pass)}
===================================
apache lighttpd iis
2.负载均衡\反向代理
haproxy lvs F5 netscaler
3.缓存服务器
squid varnish
特点
静态小文件高并发,占用资源少。软件本身小
Nginx HTTP服务器的特点及优点
1.支持高并发:能支持几万并发连接(特别是静态小文件业务环境)
2.资源消耗少:在3万并发连接下,开启10个Nginx线程消耗不到200Mb内存
3.可以做HTTP反向代理及加速缓存,即负载均衡功能,内置对RS节点服务器健康检查功能,相当于专业的haproxy软件或LVS的功能
##1.Nginx作为web服务器的主要应用场景
1.静态web服务器:
使用Nginx运行HTMl、JS、css、小图片等静态数据
2.配合运行动态web服务器

3.反向代理/负载均衡
http负载均衡
4.做web缓存服务器(把文件放入内存里)
5.反向代理与负载均衡
代理:海外代购,微商:代理:代替别人做事
正向代理:由内向外,请求外部应用服务
代替局域网内PC,请求外部应用服务
反向代理:由外向内 代题 效率低
代替外部的应用 请求内部的应用服务器。
负载均衡:转发、效率高
甩手掌柜
6.为什么Nginx总体能比Apache高?


1.安装nginx的方法
配置本地的nginx源码
vim /etc/yum.repo.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
yum install nginx
检查是否可用 百度查看装有nginx服务的ip
1.1nginx主要配置文件(yum 1.20.0)
[root@web01 ~]# cat /etc/nginx/nginx.conf
user nginx; #nginx运行的用户
worker_processes auto; #worker 进程数量(处理用户请求)
error_log /var/log/nginx/error.log notice; #error_log错误日志 notice错误级别
pid /var/run/nginx.pid; #nginx pid文件位置
events { #events {模块/区域
worker_connections 1024; #每个worker进程的连接数(并发)
}
http { #http区域/模块 web服务
include /etc/nginx/mime.types; #媒体类型(文件类型) Include引用或包含其他地方配置
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访问日志的格式 log_format 格式名称 详细定义
access_log /var/log/nginx/access.log main; # access_log 指定日志的位置及格式
sendfile on; #开启一个高效的传输模式
#tcp_nopush on; #
keepalive_timeout 65; #超时时间
#gzip on; #是否开启gzip压缩 静态文本内容 节省带宽
include /etc/nginx/conf.d/*.conf; #引用conf.d/*.conf结尾的文件 网站站点配置文件
}

1.2nginx编译安装
1. mkdir /app
2. cd /app
3. wget http://nginx.org/download/nginx-1.20.0.tar.gz
4. 安装依赖 yum -y install gcc make wget pcre-devel zlib-devel
5. 解压 tar xf nginx-1.20.0.tar.gz
6. cd nginx-1.20.0
7.执行指定路径 ./configure --prefix=/app/code
8.创建软链接 ln -s /app/code/sbin/nginx /sbin/ngin
9.开启测试 nginx 上百度输入ip地址显示 Welcome to nginx
10创建用户 useradd -s /sbin/nologin -M -u 888 www
11.添加到root组 usermod -g root www
12.修改配置文件 vim /app/code/conf/nginx.conf
user www; 名称
server_name www.dwg.com; 访问的网站
root /app/game; 访问的目录
13.修改linux中的hosts文件
vim /etc/hosts
172.16.1.8 www.dwg.com
14.修改计算机中的hosts文件解析
15.重启 nginx 输入nginx
1.3yum直接安装nginx
yum install -y nginx
2.nginx目录结构

3.简单使用nginx
systemctl start nginx 临时启动nginx
systemctl enable nginx 开机自启nginx
#检查端口
ss -lntup |grep nginx
tcp LISTEN 0 128 *:80
*:* users:
(("nginx",pid=11746,fd=6),("nginx",pid=11745,fd=6))
#检查进程
ps -ef |grep nginx
root 11745 1 0 11:45 ? 00:00:00
nginx: master process /usr/sbin/nginx -c
/etc/nginx/nginx.conf
nginx 11746 11745 0 11:45 ? 00:00:00
nginx: worker process
root 11790 11262 0 11:45 pts/0 00:00:00 grep
--color=auto nginx
#检查nginx是否可用
linux中 curl 10.0.0.7
windows中 网页搜索 10.0.0.7
#上线代码
1.创建game目录
2.上传到 /usr/share/nginx/html/game
3.解压代码并删除压缩包
4.浏览器访问http://10.0.0.7/game/
4.nginx配置文件
/etc/nginx/nginx.conf

5.nginx虚拟配置主机

5.1.基于域名的虚拟主机
1.改配置 把原有的配置文件全部打包
#/code/game index.html 内容 game.liu.com
#/code/www index.html 内容 www.liu.com
$cd /etc/nginx/conf.d
$vim liu.conf
==> www.liu.com <==
server {
listen 80;
server_name www.liu.com;
location / {
root /code/www;
index index.html;
}
}
==> game.liu.com <==
server {
listen 80;
server_name game.liu.com;
location / {
root /code/game;
index index.html;
}
}
2.测试配置与重启nginx 一定要做windows中的hosts解析
nginx -t 显示ok配置正确
systemctl start nginx
3.创建目录
mkdir -p /code/{game,www}
给/code/www/index.html 和/code/game/index.html中增加东西
echo ‘我收下了’ >>/code/www/index.html
4.做解析,做检查
[root@web01 /etc/nginx/conf.d]# curl www.liu.com
www.oldboy.com
[root@web01 /etc/nginx/conf.d]# curl game.liu.com
game.oldboy.com
5.2基于ip虚拟机
1.添加ip地址
ip addr add 10.0.0.100/24 dev eth0 label eth0:0
2.修改配置文件
==> www.liu.com <==
server {
listen 10.0.0.100:80;
server_name www.liu.com;
location / {
root /code/www;
index index.html;
}
}
==> game.liu.com <==
server {
listen 10.0.0.7:80;
server_name game.liu.com;
location / {
root /code/game;
index index.html;
}
}
3.检查语法并重启
nginx -t
systemctl restart nginx
4.测试
curl
5.3基于端口虚拟机
1.修改配置文件基于端口
==> www.liu.com <==
server {
listen 80;
server_name www.liu.com;
location / {
root /code/www;
index index.html;
}
}
==> game.liu.com <==
server {
listen 81;
server_name game.liu.com;
location / {
root /code/game;
index index.html;
}
}
2.检查语法并重启
nginx -t
systemctl reload nginx
3.检查端口是否开启
ss -lntup |grep nginx
4.测试
crul http://10.0.0.100:81
6.nginx日志详解
**访问日志 : /var/log/nginx/access.log
**错误日志 : /var/log/nginx/error.log
**访问日志的格式: log_format 设置 nginx访问日志的格式
**设置(开启)访问日志 access_log
6.1log_format日志格式

log_format main '$remote_addr - $remote_user
[$time_local] "$request" '
'$status $body_bytes_sent
"$http_referer" '
'"$http_user_agent"
"$http_x_forwarded_for"';
$remote_addr #客户端ip地址
$remote_user #远程用户(空)
$time_local #时间 11/May/2021:10:26:41
+0800
$request #用户请求报文的起始行 "GET
/index.html HTTP/1.1"
$request_uri #用户请求的uri
$status #状态码
$body_bytes_sent #http响应报文的主体大小(文件大小) 字
节 (服务器给你发送了1个多大的文件)
$http_referer #从哪里跳转到你的网站 (从哪里跳转)
分析用户的来源,精确投放广告(sem)
$http_user_agent #用户的代理(浏览器)
$http_x_forwarded_for #记录用户真实ip地址(讲解负载均衡再
说)
6.2access_log访问日志设置

path是路径
format 日志格式
gzip是否压缩 注压缩后日志最好命名为access.log.gz
fush 定时更新
#应用案例: 指定位置及格式
access_log /var/log/nginx/access.log main;
#应用案例: 压缩 日志先写入到缓存 每隔3s写入到磁盘
[root@web01 /etc/nginx/conf.d]# cat www.oldboy.com.conf
server {
listen 80;
server_name www.oldboy.com;
access_log /var/log/nginx/www_access.log.gz main
gzip buffer=128 flush=3;
location / {
root /code/www;
index index.html;
}
}
#
6.3错误日志格式

level #日志格式
debug #最详细
notice #
error #等等
6.4日志切割
#日志切割: 定期把切割成一个新的文件(加上日期). 避免日志过大
#如何实现:
##logrotate 命令 + /etc/logroate.d/配置文件
logroate -f /etc/logroate.d/nginx
[root@web01 /etc/nginx/conf.d]# cat
/etc/logrotate.d/nginx
/var/log/nginx/*.log { #指定你要切割的文件
daily #每天
missingok #如果对应日志不存在,跳过,不
显示错误信息
rotate 52 #最多保留多少个切割后的日
志.
compress #日志是否压缩 gzip
delaycompress #延迟一个周期,然后在进行压
缩
notifempty #not if empty 如果日志是
空的跳过.
create 640 nginx adm #日志权限,所有者
sharedscripts #
postrotate #在日志轮询(切割)之后,执行
里面的命令
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat
/var/run/nginx.pid` #systemctl reload nginx
fi
endscript
}
7.nginx处理请求处理过程
1. 用户输入url域名
2. DNS解析:查找域名对应的ip地址(获得域名对应的ip地址)
3. tcp3次握手:用户通过ip地址与对应的服务器建立连接(与服务器连接)
4. http请求报文(豹纹):建立tcp连接后,用户向服务器索要指定的内容(页面,
图片,视频....) (向服务索要内容)
5. 服务器通过查找 (nginx如何处理请求)
6. http响应豹纹: 找到后把用户要的内容,返还给用户(服务型响应用户)
7. tcp4次挥手: 用户与服务器断开连接
#1 . nginx处理用户请求流程
https://www.processon.com/view/link/609a2aeee401fd45927
54f39
#2. 使用ip或未注册的域名访问流程 和 如何限制 www.oldboy.com
bbs.oldboy.com blog.oldboy.com game.oldboy.com
aaa.oldboy.com ---> 网站
#3. url--->找出文件在服务器的位置?
https://img10.360buyimg.com/imgzone/jfs/t1/64309/10/139
28/244846/5db65b36E92ff53eb/38c9d4cea3e94b98.jpg
img10.360buyimg.com---> 域名对应的server
根据uri
/imgzone/jfs/t1/64309/10/13928/244846/5db65b36E92ff53eb
/38c9d4cea3e94b98.jpg 配合站点目录(root) 进行查找 .
8.nginx常用模块
8.1autoindex 目录索引模块
autoindex_module
autoindex 是否开启目录索引功能
autoindex_exact_size 是否以人类可读形式显示大小(off), on(精确显示)
autoindex_format html /json
autoindex_localtime
8.1.1配置一个下载目录
编辑配置文件
server {
listen 80;
server_name www.liu.com;
autoindex on;
location / {
root /code/www;
}
}
###注意在/code/www;目录中一定不要存在index.html
8.1.2配置一个查看nginx状态的目录
server {
listen 80;
server_name www.liu.com;
autoindex on;
location / {
root /code/www;
}
location /status {
stub_status;
access_log off;
}
}

8.2访问限制-allow-deny
ngx_http_access_module 访问限制模块
allow 准许 某个ip或网段访问
deny 拒绝
使用allow和deny 完成白名单和黑名单功能
白名单: allow,deny常用,用来限制核心目录,文件,禁止外界访问
黑名单:deny, 屏蔽ip地址
server {
listen 80;
server_name www.liu.com;
autoindex on;
location / {
root /code/www;
}
location /status {
stub_status;
access_log off;
allow 10.0.0.7;
}
}
只允许10.0.0.7访问,也就是只能在linux中访问,可以去百度验证。
8.3auth_basic_user用户授权模块
ngx_http_auth_basic_module
限制用户访问,访问的时候输入用户名和密码


#让用户访问www.liu.com/status 需要输入用户密码
1.创建nginx auth_basic_user_file 需要的密码文件
先安装插件 yum install -y httpd-tools
创建密码文件 htpasswd -b -c /etc/nginx/conf.d/status.pass liu 123456
修改密码文件权限为600 chmod 600 /etc/nginx/conf.d/status.pass
修改密码文件的属主为nginx chown nginx /etc/nginx/conf.d/status.pass
#修改配置文件
server {
listen 80;
server_name www.liu.com;
autoindex on;
location / {
root /code/www;
}
location /status {
stub_status;
auth_basic "status";
auth_basic_user_file /etc/nginx/conf.d/htpasswd.pass;
}
}
重启服务并测试
systemctl reload nginx
curl -u liu:123456 www.liu.com/status
8.5各种模块限制
limit_req 模块 限制请求(http)
limit_conn 模块 限制连接(tcp)
limit_rate core模块 限速速度
8.5.1limit_req 请求限制模块
limit_req用于限制每个已定义密钥的请求处理速率,特别是来自单个IP地址的请求的处理速率。限制是使用“漏桶”方法完成的。

配置文件
http {
limit_req_zone $binary_remote_addr zone=one:10m
rate=1r/s;
}
#$binary_remote_addr 用户ip地址,占用空间更少
#zone=One:10m 指定空间名字:大小
#rate=1r/s 指定木桶处理速度

limit_req zone=one burst=5;
zone=one #指定limit_req_zone 创建的木桶空间
burst=5 #并发5
配置文件
limit_req_zone $binary_remote_addr zone=one:10m
rate=1r/s;
server {
location /search/ {
limit_req zone=one burst=5;
}
#平均每秒允许不超过1个请求,突发请求不超过5个。
limit_req_zone $binary_remote_addr zone=one:10m
rate=1r/s;
server {
location /search/ {
limit_req zone=one burst=5 nodelay;
}
#nodelay #默认不加上nodelay,超过并发数后,排队(delay)nodelay超过并发数后,报错
8.5.2limit_conn 连接数限制模块
基于ip限制每个ip地址的连接数量.
1.ngx_http_limit_conn模块用于限制每个已定义密钥的连接数,特别是来
自单个IP地址的连接数。
2.并非所有连接都被计算在内。只有当一个连接有一个正在由服务器处理的请求
并且整个请求头已经被读取时,它才会被计数。


配置文件
server {
listen 80;
server_name www.liu.com;
autoindex on;
location / {
root /code/www;
}
location /status {
stub_status;
auth_basic "status";
auth_basic_user_file /etc/nginx/conf.d/htpasswd.pass;
limit_conn addr 1;
}
}
8.5.3 limit_rate 速率限制
limit_rate 限速
limit_rate_after 下载多少文件后再进行限速

1.环境准备
dd if=/dev/zero of=/code/share/vsecret.avi bs=1M
count=500
2.配置文件
server {
listen 80;
server_name www.liu.com;
autoindex on;
location / {
root /code/www;
}
location /status {
stub_status;
auth_basic "status";
auth_basic_user_file /etc/nginx/conf.d/htpasswd.pass;
limit_conn addr 1;
limit_rate_after 50m;
limit_rate 200k;
}
}
9.location 功能
主要匹配用户请求中的uri



#图片
location ~* \.(jpg|png|bmp|jpeg|gif|img)$ {
缓存
}
配置
server {
listen 80;
server_name location.oldboy.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 ~/";
}
测试
[root@web01 /etc/nginx/conf.d]# curl -H
Host:location.oldboy.com 10.0.0.7
location ~*/[root@web01 /etc/nginx/conf.d]#
location 应用场景
# 通用匹配,任何请求都会匹配到
location / {
...
}
# 严格区分大小写,匹配以.php结尾的都走这个location
location ~ \.php$ {
...
}
# 严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ \.jsp$ {
...
}
# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条
location
location ~* .*\.(jpg|gif|png|js|css)$ {
#缓存
}
# 不区分大小写匹配
# 通用匹配,任何请求都会匹配到
location / {
...
}
# 严格区分大小写,匹配以.php结尾的都走这个location
location ~ \.php$ {
...
}
# 严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ \.jsp$ {
...
}
# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条
location
location ~* .*\.(jpg|gif|png|js|css)$ {
#缓存
}
# 不区分大小写匹配
# 通用匹配,任何请求都会匹配到
location / {
...
}
# 严格区分大小写,匹配以.php结尾的都走这个location
location ~ \.php$ {
...
}
# 严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ \.jsp$ {
...
}
# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条
location
location ~* .*\.(jpg|gif|png|js|css)$ {
#缓存
}
# 不区分大小写匹配
# 通用匹配,任何请求都会匹配到
location / {
...
}
# 严格区分大小写,匹配以.php结尾的都走这个location
location ~ \.php$ {
...
}
# 严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ \.jsp$ {
...
}
# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条
location
location ~* .*\.(jpg|gif|png|js|css)$ {
#缓存
}
# 不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
return 403;
}
10.goaccess
10.1日志分析工具
日志分析工具:
matomo (piwiki) lnmp
awstat (perl)
goaccess
Elastic (ELK) Stack
#1.环境搭建
wget http://tar.goaccess.io/goaccess-1.2.tar.gz
tar xf goaccess-1.2.tar.gz
安装依赖
yum install -y GeoIP-devel
yum install -y ncurses-devel
cd goaccess-1.2/
./configure --enable-utf8 --enable-geoip=legacy
make
make install
2.测试
goaccess -f /var/log/nginx/access.log
html方式显示
goaccess -f /var/log/nginx/access.log-20210512 -o /code/log/report.html
10.2goaccess
-f 指定日志文件
-p 指定配置文件(html页面显示使用)
10.3选择日志格式


网友评论