NGINX 基础参数与功能

 章节

1 NGINX 的源码安装

2 NGINX 核心配置详解

3 NGINX 之 location 匹配优先级

4 NGINX 基础参数与功能 

目录

1 实现 Nginx 账户认证功能

1.1 创建htpasswd 认证文件

1.2 创建数据目录

1.3 指定认证文件路径

1.4 测试效果

2 定义重定向错误日志

2.1 指定错误日志访问路径

2.2 创建错误文件目录并增加自定义内容

2.3 测试效果

3 定义访问日志与错误日志的路径

3.1 指定日志路径

3.2 创建日志目录 并重新启动服务

3.3 访问网站查看日志是否正确加载

4 检测文件是否存在 try_files

4.1 参数介绍

4.2 语法格式

4.3 示例: 如果不存在页面, 就转到default.html页面

4.3.1 配置文件增加参数try_files

4.3.2  创建数据目录增加内容

4.3.3 实现效果 

4.4 示例二:使用 return 指令返回特定状态码

5 NGINX的长连接配置

5.1 请求连接数 keepalive_requests

5.1.1 参数配置与介绍

5.1.2 重启加载参数

5.1.3 测试效果

5.2 最大长连接时间 keepalive_timeout

5.2.1 参数配置

5.2.2 动图演示效果

6 作为下载服务器配置

6.1 相关指令介绍

6.2 自动索引参数的介绍与开启

6.2.1 配置autoindex参数

6.2.2 文件制作命令 dd

6.2.3 实现效果

6.2.4 网页时间同步

7 NGINX状态页面设置

7.1 定义状态页路径

7.2 创建状态目录 

7.3 实现效果

8 实现NGINX文件压缩功能

8.1 NGINX压缩功能介绍

8.2 参数介绍及解释

8.3 开启压缩功能(默认不开启的)

8.4 gzip_types 支持的压缩格式

8.5 实现效果


1 实现 Nginx 账户认证功能

ngx_http_auth_basic_module 模块提供此功能

示例

1.1 创建htpasswd 认证文件

[root@RHEL-9 nginx]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password: 
Re-type new password: 
Adding password for user admin
[root@RHEL-9 nginx]# htpasswd -m /usr/local/nginx/.htpasswd shuyan
New password: 
Re-type new password: 
Adding password for user shuyan

[root@RHEL-9 conf.d]# ls -a /usr/local/nginx/
.   client_body_temp  conf.d        html       logs        sbin       uwsgi_temp
..  conf              fastcgi_temp  .htpasswd  proxy_temp  scgi_temp


# -c: 创建新的 .htpasswd 文件。
# -m: 使用 MD5 加密算法。

1.2 创建数据目录

[root@RHEL-9 nginx]# mkdir  /data/web/shuyan
[root@RHEL-9 nginx]# echo shuyan > /data/web/shuyan/index.html

1.3 指定认证文件路径

[root@RHEL-9 nginx]# vim /usr/local/nginx/conf.d/location.conf
server {
    listen 80;
    server_name 192.168.239.20;
    index index.html;

    location /shuyan {
        root /data/web;
        auth_basic "login password";
        auth_basic_user_file  "/usr/local/nginx/.htpasswd";
    }
}

[root@RHEL-9 nginx]# systemctl restart nginx

1.4 测试效果

[root@RHEL-9 conf.d]# curl www.shuyan.com/shuyan/ -u shuyan
Enter host password for user 'shuyan':
shuyan

2 定义重定向错误日志

2.1 指定错误日志访问路径

[root@RHEL-9 nginx]# vim /usr/local/nginx/conf.d/location.conf
server {
    listen 80;
    server_name 192.168.239.20;
    index index.html;
    error_page 404 /40x.html;
    location /shuyan {
        root /data/web;
    }

    location = /40x.html {
        root /data/web/errorpage;

    }

}

状态码写404当然也可以写500 503 

2.2 创建错误文件目录并增加自定义内容

[root@RHEL-9 nginx]# mkdir -p /data/web/errorpage

[root@RHEL-9 nginx]# echo "<h1>error page</h1>" > /data/web/errorpage/40x.html
[root@RHEL-9 nginx]# vim /data/web/errorpage/40x.html
[root@RHEL-9 nginx]# systemctl restart nginx

2.3 测试效果

3 定义访问日志与错误日志的路径

3.1 指定日志路径

server {
    listen 80;
    server_name 192.168.239.20;
    root /data/web/html;
    index index.html;
    error_page 404 /40x.html;
    error_log /var/log/nginx_log/error.log;
    access_log /var/log/nginx_log/access.log;
    location /shuyan {
        root /data/web;
        auth_basic "login password";
        auth_basic_user_file  "/usr/local/nginx/.htpasswd";

    }

    location = /40x.html {
        root /data/web/errorpage;

    }

3.2 创建日志目录 并重新启动服务

[root@RHEL-9 conf.d]# mkdir  -p /var/log/nginx_log/
[root@RHEL-9 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@RHEL-9 conf.d]# systemctl restart nginx.service 
[root@RHEL-9 conf.d]# ls /var/log/nginx_log/
access.log  error.log

3.3 访问网站查看日志是否正确加载

4 检测文件是否存在 try_files

4.1 参数介绍

try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如 果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一 个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内 部500错误。

4.2 语法格式

try_files path1 path2 ... pathN [default|return status|internal|break];

4.3 示例: 如果不存在页面, 就转到default.html页面

4.3.1 配置文件增加参数try_files

server {
    listen 80;
    server_name 192.168.239.20;
    root /data/web/html;
    index index.html;
    error_page 404 /40x.html;
    error_log /var/log/nginx_log/error.log;
    access_log /var/log/nginx_log/access.log;
    try_files $uri $uri.html $uri/index.html /error/default.html;
    location /shuyan {
        root /data/web;
        auth_basic "login password";
        auth_basic_user_file  "/usr/local/nginx/.htpasswd";

    }

    location = /40x.html {
        root /data/web/errorpage;

    }

}

4.3.2  创建数据目录增加内容


[root@RHEL-9 conf.d]# mkdir  /data/web/html/error
[root@RHEL-9 conf.d]# echo this is error default > /data/web/html/error/default.html

[root@RHEL-9 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@RHEL-9 conf.d]# systemctl restart nginx


[root@RHEL-9 conf.d]# echo this is default > /data/web/html/index.html

4.3.3 实现效果 

[root@RHEL-9 conf.d]# curl www.shuyan.com
this is default

# 删除目录看效果
[root@RHEL-9 conf.d]# rm -f /data/web/html/index.html 
[root@RHEL-9 conf.d]# curl www.shuyan.com
this is error default

4.4 示例二:使用 return 指令返回特定状态码

try_files $uri $uri/ @fallback;
location @fallback {
    return 404;
}

5 NGINX的长连接配置

5.1 请求连接数 keepalive_requests

5.1.1 参数配置与介绍

[root@RHEL-9 conf.d]# vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65 60;
    keepalive_requests 2;

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



Keep-Alive:timeout=60   #浏览器收到的服务器返回的报文
                        #如果设置为0表示关闭会话保持功能,将如下显示:
                        #Connection:close 浏览器收到的服务器返回的报文

5.1.2 重启加载参数

[root@RHEL-9 conf.d]# systemctl restart nginx.service

5.1.3 测试效果

5.2 最大长连接时间 keepalive_timeout

5.2.1 参数配置

5.2.2 动图演示效果

如动图所示,在3秒之后就断开链接了

6 作为下载服务器配置

ngx_http_autoindex_module 模块处理以斜杠字符 "/" 结尾的请求,并生成目录列表,可以做为下载服务 配置使用

6.1 相关指令介绍

autoindex on | off;             # 自动文件索引功能,默认off
autoindex_exact_size on | off;   # 计算文件确切大小(单位bytes),
                                 # off 显示大概大小(单位K、M),默认on
autoindex_localtime on | off ;  # 显示本地时间而非GMT(格林威治)时间,默认off
autoindex_format html | xml | json | jsonp;     # 显示索引的页面文件风格,默认html

limit_rate rate;              # 限制响应客户端传输速率(除GET和HEAD以外的所有方法),
                              # 单位B/s, bytes/second,                
                              #  默认值0,表示无限制,此指令由ngx_http_core_module提供

set $limit_rate 4k;          # 也可以通过变量限速,单位B/s,同时设置,此项优级高。

6.2 自动索引参数的介绍与开启

在我们平常寻找镜像源的时候会发现有这样一个界面,在NGINX内也拥有这样一个功能,此时需要再NGINX里增加一个参数autoindex

6.2.1 配置autoindex参数

指定目录 /data/web/download

[root@RHEL-9 conf.d]# vim /usr/local/nginx/conf.d/location.conf 
server {
    listen 80;
    server_name 192.168.239.20;
    root /data/web/html;
    index index.html;
    error_page 404 /40x.html;
    error_log /var/log/nginx_log/error.log;
    access_log /var/log/nginx_log/access.log;
    try_files $uri $uri.html $uri/index.html /error/default.html;
    location /shuyan {
        root /data/web;
        auth_basic "login password";
        auth_basic_user_file  "/usr/local/nginx/.htpasswd";

    }

    location = /40x.html {
        root /data/web/errorpage;

    }
    location /download {
        root /data/web;
        autoindex on;
    }

}

6.2.2 文件制作命令 dd

[root@RHEL-9 conf.d]# mkdir /data/web/download
[root@RHEL-9 conf.d]# dd if=/dev/zero of=/data/web/download/shuyan bs=1M count=100

# if 代表输入文件。如果不指定if,默认就会从stdin中读取输入。
# of 代表输出文件。如果不指定of,默认就会将stdout作为默认输出。
# bs 代表字节为单位的块大小。
# count 代表被复制的块数。
# /dev/zero 是一个字符设备,会不断返回0值字节(\0)。
单元大小代码
字节(1B)c
字节(2B)w
块(512B)b
千字节(1024B)k
兆字节(1024KB)M
吉字节(1024MB)G

[root@RHEL-9 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@RHEL-9 conf.d]# systemctl restart nginx.service 

6.2.3 实现效果

浏览器访问指定页面查看

6.2.4 网页时间同步

autoindex_localtime 参数

本机时间而非GMT(格林威治)时间,默认off

在时间的时候会发现一个问题,发现显示时间与时间时间对不上。

autoindex_localtime 改为 on

增加参数之后时间对的上了

autoindex_exact_size (精确计算)
默认是开启按照字节的单位来换算的 (精确计算)

    location /download {
        root /data/web;
        autoindex on;
        autoindex_localtime on; #显示本机时间而非GMT(格林威治)时间,默认off
        autoindex_exact_size off;
    }

实现效果

7 NGINX状态页面设置

7.1 定义状态页路径

路径为 /data/web/status

[root@RHEL-9 conf.d]# vim /usr/local/nginx/conf.d/status.conf 
server {
    listen 80;
    server_name www.status.com;
    root /data/web;
    index index.html;
    location /status {
        stub_status;
        allow 192.168.239.1/32;
        allow 127.0.0.1;
        deny all;
    }
}

允许本机并拒绝所有

7.2 创建状态目录 

[root@RHEL-9 conf.d]# mkdir /data/web/status
[root@RHEL-9 conf.d]# systemctl restart nginx

7.3 实现效果

状态页参数说明

#状态页用于输出nginx的基本状态信息:

#输出信息示例:

Active connections: 2 

server accepts handled requests
 8 8 13 

               # 上面三个数字分别对应accepts,handled,requests三个值



Reading: 6 Writing: 179 Waiting: 106

Active connections: #当前处于活动状态的客户端连接数

                    #包括连接等待空闲连接数=reading+writing+waiting

accepts: #统计总值,Nginx自启动后已经接受的客户端请求连接的总数。

handled: #统计总值,Nginx自启动后已经处理完成的客户端请求连接总数

         #通常等于accepts,除非有因worker_connections限制等被拒绝的连接

requests: #统计总值,Nginx自启动后客户端发来的总的请求数


Reading: #当前状态,正在读取客户端请求报文首部的连接的连接数
            #数值越大,说明排队现象严重,性能不足
Writing: #当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大

Waiting: #当前状态,正在等待客户端发出请求的空闲连接数开启 keep-alive的情况下,
         #这个值等于active –(reading+writing)

8 实现NGINX文件压缩功能

8.1 NGINX压缩功能介绍

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

Nginx对文件的压缩功能是依赖于模块 ngx_http_gzip_module,默认是内置模块

8.2 参数介绍及解释

gzip on;

        作用: 启用 gzip 压缩功能。

        说明: 当客户端支持 gzip 压缩时,Nginx 会对响应进行压缩后再发送。

gzip_comp_level 5;

        作用: 设置 gzip 压缩级别。

        说明: 这个值介于 1 到 9 之间,数值越大压缩比越高,但压缩所需的时间也会增加,所需要的性能也就越多。这里设置为 5,是一个比较平衡的选择。

gzip_min_length 1k;

        作用: 设置最小响应长度以启用 gzip 压缩。

        说明: 只有响应体大小超过指定的字节数时才会进行 gzip 压缩。这里设置为 1KB,意味着只有当响应体大于等于 1KB 时才会进行压缩。

gzip_http_version 1.1;

        作用: 设置支持 gzip 压缩的 HTTP 版本。

        说明: 这里设置为 1.1,意味着只有 HTTP 1.1 的请求才会启用 gzip 压缩。

gzip_vary on;

        作用: 启用 Vary HTTP 头,告诉浏览器是否支持 gzip 压缩。

        说明: 当启用 gzip_vary 时,Nginx 会在响应头中添加 Vary: Accept-Encoding,这可以让缓存代理服务器根据客户端是否支持 gzip 压缩来决定是否缓存压缩后的响应。

gzip_types

        作用: 指定哪些 MIME 类型的内容会被 gzip 压缩。

8.3 开启压缩功能(默认不开启的)

[root@RHEL-9 conf.d]# vim /usr/local/nginx/conf/nginx.conf

    gzip_comp_level 5;
    gzip_min_length 1k;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/gif image/png;
    include /usr/local/nginx/conf.d/*.conf;

8.4 gzip_types 支持的压缩格式

注意gzip_types 必须是mime.type里支持的格式 

[root@RHEL-9 conf.d]# vim ../conf/mime.types

[root@RHEL-9 conf.d]# cat /var/log/messages > /data/web/html/big.html


[root@RHEL-9 conf.d]# ll -h /data/web/html/
总用量 744K
-rw-r--r--. 1 root root 738K  8月 16 16:19 big.html
drwxr-xr-x. 2 root root   26  8月 16 14:41 error
-rw-r--r--. 1 root root   16  8月 16 15:03 index.html

8.5 实现效果

[root@RHEL-9 conf.d]#  curl --head --compressed 192.168.239.20/index.html
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Fri, 16 Aug 2024 08:36:28 GMT
Content-Type: text/html
Content-Length: 16
Last-Modified: Fri, 16 Aug 2024 07:03:34 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "66bef9c6-10"
Accept-Ranges: bytes

[root@RHEL-9 conf.d]#  curl --head --compressed 192.168.239.20/big.html
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Fri, 16 Aug 2024 08:36:39 GMT
Content-Type: text/html
Last-Modified: Fri, 16 Aug 2024 08:19:38 GMT
Connection: keep-alive
Keep-Alive: timeout=60
Vary: Accept-Encoding
ETag: W/"66bf0b9a-b85da"
Content-Encoding: gzip

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

妍妍的宝贝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值