目录
一.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安装(编译安装)
[root@nginx ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y2.解压上传的nginx压缩包
[root@nginx ~]# tar zxf nginx-1.24.0.tar.gz3.创建了一个名为nginx的用户,该用户没有家目录,并且被配置为不能通过/sbin/nologin登录,用于运行Nginx的系统账户。
[root@nginx ~]# useradd -s /sbin/nologin -M nginx4.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  src5.检查系统环境,确保所有必要的依赖都已安装,并准备编译软件
[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的透传ip6.开始编译并将编译好的文件安装到指定目录
[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.02.关闭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/sbin2.重新加载文件生效
[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的平滑升级及版本回滚(未完待续)
三.Nginx核心配置
<1>.实现nginx的高并发配置
1.查看用户进程的文件限制,此时是1024
[root@nginx ~]# sudo -u nginx ulimit -n
10242.压力测试
(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
1000005.压力测试:2000个请求同时发送,总共发送50000条,0条失败,测试成功
[root@nginx ~]# ab -n 50000 -c 2000 http://172.25.254.100/index.html

<2>.root和alias(未完待续)
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.confevents {
    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>.检测文件是否存在
[root@nginx ~]# mkdir /data/web/html/error
[root@nginx ~]# echo nobody > /data/web/html/error/default.html
[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.confkeepalive_timeout #设定保持连接超时时长,0表示禁止长连接。开启长连接后,返回客户端的会话保持时间为60s,单次长连接累计请求达到指定次数请求或65秒就会被断开,第二个数字60为发送给客户端应答报文头部中显示的超时时间设置为60s,如不设置客户端将不显示超时时间。

[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/download2.生成一个大小为100MB的文件
[root@nginx ~]# dd if=/dev/zero of=/data/web/download/liufile bs=1M count=1003.子配置文件中写入规则并重启
[root@nginx ~]# vim /usr/local/nginx/conf.d/status.conf 
        location /download {         
             root /data/web;                  
             autoindex on;                          #自动索引功能
             autoindex_localtime on;                #on表示显示本机时间而非GMT
             autoindex_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>.压缩功能
1.主配置文件中写入规则并重启
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    gzip  on;                       #启用gzip压缩,默认关闭
    gzip_comp_level 5;              #压缩比由低到高从1到9,默认为1,值越高压缩后文件越小,但是消耗cpu比较高。基本设定未4或者5
    gzip_min_length 10k;            #gzip压缩的最小文件,小于设置值的文件将不会压缩
    gzip_http_version 1.1;          #启用压缩功能时,协议的最小版本,默认HTTP/1.1
    gzip_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



















