当前位置: 首页 > news >正文

nginx实战演练

目录

一.Nginx架构和安装(未完待续)

<1>.Nginx概述

<2>.Nginx架构和进程

<3>.Nginx模块

<4>.Nginx安装(编译安装)

二.Nginx基础配置

<1>.关闭debug

<2>.将nginx软件添加到环境变量

<3>.开机自启动脚本

<4>.nginx的平滑升级及版本回滚(未完待续)

三.Nginx核心配置

<1>.实现nginx的高并发配置

<2>.root和alias(未完待续)

<3>.location详细使用(未完待续)

<4>.账户认证功能

<5>.自定义错误页面

<6>.自定义错误日志

<7>.检测文件是否存在

<8>.长连接配置

<9>.作为下载服务器

四.Nginx高级配置

<1>.状态页

<2>.压缩功能


一.Nginx架构和安装(未完待续)

<1>.Nginx概述

<2>.Nginx架构和进程

<3>.Nginx模块

<4>.Nginx安装(编译安装)

        源码安装需要提前准备标准的编译器,GCC的全称是(GNU Compiler collection),其有GNU开发,并以GPL即LGPL许可,是自由的类UNIX即苹果电脑Mac OS X操作系统的标准编译器,因为GCC原本只能处理C语言,所以原名为GNU C语言编译器,后来得到快速发展,可以处理C++,Fortran,pascal,objective C,java以及Ada等其他语言,此外还需要Automake工具,以完成自动创建Makefile的工作,Nginx的一些模块需要依赖第三方库,比如: pcre(支持rewrite),zlib(支持gzip模块)和openssl(支持ssl模块)等。
1.安装nginx模块需要依赖的模块
[root@nginx ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y

2.解压上传的nginx压缩包

[root@nginx ~]# tar zxf nginx-1.24.0.tar.gz

3.创建了一个名为nginx的用户,该用户没有家目录,并且被配置为不能通过/sbin/nologin登录,用于运行Nginx的系统账户。

[root@nginx ~]# useradd -s /sbin/nologin -M nginx

4.cd到解压后的目录下可以看到包含的文件

[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

5.检查系统环境,确保所有必要的依赖都已安装,并准备编译软件

[root@nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \                          # 指定nginx运行用户   
> --group=nginx \                         # 指定nginx运行组
> --with-http_ssl_module \                # 支持https://
> --with-http_v2_module \                 # 支持http版本2
> --with-http_realip_module \             # 支持ip透传
> --with-http_stub_status_module \        # 支持状态页面
> --with-http_gzip_static_module \        # 支持压缩
> --with-pcre \                           # 支持正则
> --with-stream \                         # 支持tcp反向代理
> --with-stream_ssl_module \              # 支持tcp的ssl加密
> --with-stream_realip_module             # 支持tcp的透传ip

6.开始编译并将编译好的文件安装到指定目录

[root@nginx nginx-1.24.0]# make && make install 

二.Nginx基础配置

<1>.关闭debug

1.估算nginx的磁盘空间使用量,此时所占空间较大

[root@nginx ~]# du -sh nginx-1.24.0
26M	nginx-1.24.0

2.关闭nginx服务的debug功能

[root@nginx nginx-1.24.0]# vim auto/cc/gcc

3.重新安装编译

[root@nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@nginx nginx-1.24.0]# make && make install 

<2>.将nginx软件添加到环境变量

1.把nginx软件的命令执行路径添加到环境变量中

[root@nginx nginx-1.24.0]# vim ~/.bash_profile 
export PATH=$PATH:/usr/local/nginx/sbin

2.重新加载文件生效

[root@nginx nginx-1.24.0]# source ~/.bash_profile 

3.此时可以使用nginx命令查看版本

[root@nginx nginx-1.24.0]# nginx -v
nginx version: nginx/1.24.0

<3>.开机自启动脚本

1.编写自启动脚本

[root@nginx ~]# vim /lib/systemd/system/nginx.service 
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target

2.重新加载文件并开启nginx服务

[root@nginx ~]# systemctl daemon-reload 
[root@nginx ~]# systemctl start nginx.service 

<4>.nginx的平滑升级及版本回滚(未完待续)

1.将旧 Nginx 二进制文件换成新 Nginx 程序文件(注意先备份 )
2.向 master 进程发送 USR2 信号
3.master 进程修改 pid 文件名加上后缀 .oldbin, 成为 nginx.pid.oldbin
4.master 进程用新 Nginx 文件启动新 master 进程成为旧 master 的子进程 , 系统中将有新旧两个 Nginx
5.进程共同提供 Web 服务 , 当前新的请求仍然由旧 Nginx worker 进程进行处理 , 将新生成的 master
程的 PID 存放至新生成的 pid 文件 nginx.pid
6.向旧的 Nginx 服务进程发送 WINCH 信号,使旧的 Nginx worker 进程平滑停止
7.向旧 master 进程发送 QUIT 信号,关闭老 master ,并删除 Nginx.pid.oldbin 文件
8.如果发现升级有问题 , 可以回滚∶向老 master 发送 HUP ,向新 master 发送 QUIT

三.Nginx核心配置

<1>.实现nginx的高并发配置

1.查看用户进程的文件限制,此时是1024

[root@nginx ~]# sudo -u nginx ulimit -n
1024

2.压力测试

(1).100个请求同时发送,总共发送500条,0条失败,测试成功

[root@nginx ~]# ab -n 500 -c 100 http://172.25.254.100/index.html

(2).2000个请求同时发送,总共发送50000条,576条失败,测试失败

[root@nginx ~]# ab -n 50000 -c 2000 http://172.25.254.100/index.html

3.修改pam限制

[root@nginx ~]# vim /etc/security/limits.conf 

4.进入配置文件设置最大并发连接数为100000并重载文件

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
events {worker_connections  100000;         #设置单个工作进程的最大并发连接数
}
[root@nginx ~]# nginx -s reload
[root@nginx ~]# sudo -u nginx ulimit -n
100000

5.压力测试:2000个请求同时发送,总共发送50000条,0条失败,测试成功

[root@nginx ~]# ab -n 50000 -c 2000 http://172.25.254.100/index.html

<2>.root和alias(未完待续)

root:指定web的家目录,在定义location的时候,文件的绝对路径等于 root+location
alias:定义路径别名,会把访问的路径重新定义到其指定的路径,文档映射的另一种机制;仅能用于
location上下文,此指令使用较少
注意: root, 给定的路径对应于 location 中的 /uri 左侧的 /
           alias , 给定的路径对应于 location 中的 /uri 的完整路径
1.使用epoll事件驱动并将子配置文件目录映射到主配置文件中,重载文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
events {worker_connections  100000;use epoll;                                #使用epoll事件驱动
}http  {######include "/usr/local/nginx/conf.d/*.conf"; #在响应报文中将指定的文件扩展名映射至MIME对应的类型######
}
[root@nginx ~]# nginx -s reload

2.创建子配置文件并写入规则,重载文件

[root@nginx ~]# mkdir -p /usr/local/nginx/conf.d
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {listen 80;server_name www.rhel9.org;root /data/web/html;index index.html;
}
[root@nginx ~]# nginx -s reload

3.写入测试文字

[root@nginx ~]# echo www.rhel9.org > /data/web/html/index.html 

4.测试:记得做本地解析

<3>.location详细使用(未完待续)

<4>.账户认证功能

1.创建用户,完成后可查看信息

[root@nginx ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password: 
Re-type new password: 
Adding password for user admim[root@nginx ~]# htpasswd -m /usr/local/nginx/.htpasswd liu
New password: 
Re-type new password: 
Adding password for user liu[root@nginx ~]# cat /usr/local/nginx/.htpasswd 
admin:$apr1$okMzYJLu$Sk/0N1AUZoDjgpldJUi2a0
liu:$apr1$DMbv5BB8$HbeB4TMW9UZfAScnzjkq.1

2.创建文件并写入测试文字

[root@nginx ~]# mkdir /data/web/liu
[root@nginx ~]# echo liu > /data/web/liu/index.html 

3.子配置文件中写入规则并重启

[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
        location /liu {root /data/web;auth_basic "login password !!!";                   #用户在浏览器看到的认证信息提示auth_basic_user_file "/usr/local/nginx/.htpasswd"; #http认证文件路径}
[root@nginx ~]# nginx -s reload

4.浏览器测试

<5>.自定义错误页面

1.创建文件并写入测试文字

[root@nginx ~]# mkdir -p /data/web/errorpage
[root@nginx ~]# echo sorry! > /data/web/errorpage/40x.html 

2.子配置文件中写入规则并重启

[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {listen 80;server_name www.rhel9.org;root /data/web/html;index index.html;error_page 404 /40x.html;              #第一步location = /40x.html {root /data/web/errorpage;      #第二步}
}
[root@nginx ~]# nginx -s reload

3.测试

<6>.自定义错误日志

1.创建自定义日志存放目录

[root@nginx ~]# mkdir /var/log/rhel9.org

2.子配置文件中写入规则并重启

[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {listen 80;server_name www.rhel9.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/rhel9.org/error.log;           #错误日志access_log /var/log/rhel9.org/access.log;         #成功日志location = /40x.html {root /data/web/errorpage;}
}
[root@nginx ~]# nginx -s reload

3.测试

<7>.检测文件是否存在

       try_files 会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一 个内部重定向,之前的参数只设置内部 URI 的指向。最后一个参数是回退 URI 且必须存在,否则会出现内 500 错误。
1.创建文件并写入测试文字
[root@nginx ~]# mkdir /data/web/html/error
[root@nginx ~]# echo nobody > /data/web/html/error/default.html
2.子配置文件中写入规则并重启
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {listen 80;server_name www.rhel9.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/rhel9.org/error.log;          access_log /var/log/rhel9.org/access.log;        try_files $uri $uri.html $uri/index.html /error/default.html;   #配置location = /40x.html {root /data/web/errorpage;}
}
[root@nginx ~]# nginx -s reload

3.测试

<8>.长连接配置

1.主配置文件中写入规则并重启

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf

keepalive_timeout    #设定保持连接超时时长,0表示禁止长连接开启长连接后,返回客户端的会话保持时间为60s,单次长连接累计请求达到指定次数请求或65秒就会被断开,第二个数字60为发送给客户端应答报文头部中显示的超时时间设置为60s,如不设置客户端将不显示超时时间。

keepalive_requests   #在一次长连接上所允许请求的资源的最大数量

[root@nginx ~]# nginx -s reload

2.测试:需要用到telnet测试工具

[root@nginx ~]# yum telnet install -y
[root@nginx ~]# telnet www.rhel9.org 80
Trying 172.25.254.100...
Connected to www.rhel9.org.
Escape character is '^]'.
GET / HTTP/1.1                            #输入动作
HOST:www.rhel9.org                        #输入访问HOST#回车
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Sat, 18 Aug 2024 0:16:16 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Sat, 17 Aug 2024 11:49:12 GMT
Connection: keep-alive
ETag: "669b7a08-f"
Accept-Ranges: bytes

<9>.作为下载服务器

1.创建文件

[root@nginx ~]# mkdir /data/web/download

2.生成一个大小为100MB的文件 

[root@nginx ~]# dd if=/dev/zero of=/data/web/download/liufile bs=1M count=100

3.子配置文件中写入规则并重启

[root@nginx ~]# vim /usr/local/nginx/conf.d/status.conf 
        location /download {         root /data/web;                  autoindex on;                          #自动索引功能autoindex_localtime on;                #on表示显示本机时间而非GMTautoindex_exact_size off;              #计算文件确切大小limit_rate 1024k;                      #限速,默认不限速}
[root@nginx ~]# nginx -s reload

4.测试

四.Nginx高级配置

<1>.状态页

1.子配置文件中写入规则并重启

[root@nginx ~]# vim /usr/local/nginx/conf.d/status.conf 
server {listen 80;server_name status.rhel9.org;root /data/web/html;index index.html;location /status {stub_status;allow 172.25.254.1;deny all;}
}
[root@nginx ~]# nginx -s reload

2.测试

Active connections
当前处于活动状态的客户端连接数
accepts
统计总值, Nginx 自启动后已经接受的客户端请求连接的总数
handled
统计总值, Nginx 自启动后已经处理完成的客户端请求连接总数
requests
统计总值, Nginx自启动后客户端发来的总的请求数,数值越大 , 说明排队现象严重 , 性能不足
Reading
当前状态,正在读取客户端请求报文首部的连接的连接数
Writing
当前状态,正在向客户端发送响应报文过程中的连接数 , 数值越大说明访问量很大
Waiting
当前状态,正在等待客户端发出请求的空闲连接数

<2>.压缩功能

        Nginx 支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,这样有助于降低出口带宽的利用率,降低企业的 IT 支出,不过会占用相 应的 CPU 资源。

1.主配置文件中写入规则并重启

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    gzip  on;                       #启用gzip压缩,默认关闭gzip_comp_level 5;              #压缩比由低到高从1到9,默认为1,值越高压缩后文件越小,但是消耗cpu比较高。基本设定未4或者5gzip_min_length 10k;            #gzip压缩的最小文件,小于设置值的文件将不会压缩gzip_http_version 1.1;          #启用压缩功能时,协议的最小版本,默认HTTP/1.1gzip_vary on;                   #如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”,一般建议打开gzip_types text/plain application/javascript application/x-javascript text/css
application/xml text/javascript application/x-httpd-php image/gif image/png;#指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错
[root@nginx ~]# nginx -s reload

2.创建两个测试文件

[root@nginx ~]# echo small > /data/web/html/small.html 
[root@nginx ~]# cat /usr/local/nginx/logs/access.log > /data/web/html/big.html 

3.测试:大于10k的文件被压缩了

[root@nginx ~]# du -sh /data/web/html/big.html 
62M	    /data/web/html/big.html
[root@nginx ~]# du -sh /data/web/html/small.html 
4.0K	/data/web/html/small.html

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Vue3核心探索:深入解析组合式API中的mount方法
  • 保研考研机试攻略:第三章——数学(1)
  • 代码随想录算法训练营 | 动态规划 part05
  • 设计模式解析:组合模式与装饰模式
  • php7.4二进制安装-contos7
  • HoloLens 和 Unity 空间坐标系统 Coordinate systems
  • 信号signal与信号量semaphore的区别
  • 基于STM32开发的智能植物浇水系统
  • 音视频相关知识
  • 算法的学习笔记—链表中倒数第 K 个结点(牛客JZ22)
  • 激光雷达点云投影到图像平面
  • CSS方向选择的艺术:深入探索:horizontal和:vertical伪类
  • Ansible可视化管理之web界面集成使用探究(未完待续)
  • 2024年杭州市网络与信息安全管理员(网络安全管理员)职业技能竞赛的通知
  • 【STM32嵌入式系统设计与开发拓展】——14_定时器之输入捕获
  • 2017-08-04 前端日报
  • 2018一半小结一波
  • 3.7、@ResponseBody 和 @RestController
  • CODING 缺陷管理功能正式开始公测
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • es6--symbol
  • export和import的用法总结
  • extract-text-webpack-plugin用法
  • iOS | NSProxy
  • Linux下的乱码问题
  • Mithril.js 入门介绍
  • React组件设计模式(一)
  • Redis的resp协议
  • swift基础之_对象 实例方法 对象方法。
  • Vue2.0 实现互斥
  • VuePress 静态网站生成
  • vuex 学习笔记 01
  • win10下安装mysql5.7
  • WordPress 获取当前文章下的所有附件/获取指定ID文章的附件(图片、文件、视频)...
  • 译米田引理
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • ​2020 年大前端技术趋势解读
  • ​ubuntu下安装kvm虚拟机
  • ​软考-高级-信息系统项目管理师教程 第四版【第14章-项目沟通管理-思维导图】​
  • # 移动硬盘误操作制作为启动盘数据恢复问题
  • #if等命令的学习
  • #pragam once 和 #ifndef 预编译头
  • $(function(){})与(function($){....})(jQuery)的区别
  • (3)Dubbo启动时qos-server can not bind localhost22222错误解决
  • (k8s)Kubernetes 从0到1容器编排之旅
  • (pojstep1.1.2)2654(直叙式模拟)
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (第61天)多租户架构(CDB/PDB)
  • (面试必看!)锁策略
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • (一)80c52学习之旅-起始篇
  • ****Linux下Mysql的安装和配置
  • .naturalWidth 和naturalHeight属性,
  • .net core 6 集成和使用 mongodb
  • .Net Core 微服务之Consul(二)-集群搭建