nginx 资料整理(三)- web

在这里插入图片描述

导言:本系列整理资料中,在第一章我们概述了nginx以及搭建简单web服务器,由于nginx是基于http协议开发的web服务器软件,所以我们在第二章介绍了http协议相关内容,重点是请求和响应相关内容,在本章中,我们将继续介绍nginx作为web服务器的相关功能,从最简单的示例开始,逐渐追加功能

1. web服务器

在后面的示例中,我们将主要以编写子配置文件的形式进行搭建示例网站

cainiao网站为起点,开始我们本次的旅程吧

服务器信息

主机名IP系统软件
web-svr-01192.168.202.131CentOS 7.9nginx/1.20.1

目前我们只配置一台机器进行演示即可,当后面更多机器的时候,我们再进行扩充

编写站点文件

[root@web-svr-01 conf.d]# mkdir -p /app/code/www
[root@web-svr-01 conf.d]# echo "cainiao index" > /app/code/www/index.html
[root@web-svr-01 conf.d]# vim www.cainiao.cn.conf
[root@web-svr-01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-svr-01 conf.d]# systemctl reload nginx
[root@web-svr-01 conf.d]# cat www.cainiao.cn.conf
server {
  listen 80;
  server_name www.cainiao.cn;
  root /app/code/www;

  location / {
    index index.html;
  }
}

验证

[root@web-svr-01 conf.d]# curl -H Host:www.cainiao.cn 127.0.0.1
cainiao index

读者也可以在同网段的windows服务器,修改hosts域名解析文件,通过浏览器访问此网站。这个hosts文件一般在C:\Windows\System32\drivers\etc目录中,可以在该文件中追加192.168.31.48 www.cainiao.cn

1. 日志功能

Nginx的日志功能主要体现在两个方面:访问日志错误日志

  1. 访问日志(access.log):记录每次HTTP请求的信息,如请求的时间、客户端IP、请求的URL、响应状态码等。在Nginx配置文件中设置访问日志的格式和日志文件的位置。例如:
http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log /var/log/nginx/access.log main;
    ...
}
  1. 错误日志(error.log):记录Nginx运行中遇到的错误信息,如启动、运行中的错误、客户端请求的错误等。错误日志的配置通常在全局块中进行,例如:
error_log /var/log/nginx/error.log warn;

以上是Nginx日志配置的基本方法,可以根据实际需求进行定制化配置。

1. 访问日志

学习nginx,首先要学会查看官网 ngx_http_log_module
在这里插入图片描述

  1. log_format 定义访问日志的格式,只能出现在http全局块中,支持默认格式或者json格式,名字可以自定义
说明配置
Syntax(语法)log_format name [escape=default|json|none] string ...;
Default(默认)log_format combined “…”;
Context(语境,可以出现的位置)http

格式中包含的变量较多

变量名变量说明
$time_iso8601ISO 8601 时间格式
$time_local用户请求的时间和时区
$msec毫秒级别的日志记录时间
$remote_addr发起与 Nginx 建立连接的网络客户端的 IP,有时会是上层代理服务器的 IP
$http_x_forwarded_for可以记录客户端 IP,通过代理服务器来记录客户端的 IP
$remote_user用于记录远程客户端的用户名称
$http_user_agent用户客户端浏览器标识
$connection网络连接编号
$connection_requests当前连接的请求数
$request用户请求的 URI 及请求方法
$request_method用户请求方法
$request_uri用户请求的 URI 及请求方法
$server_protocol请求协议
$request_time请求时间
$request_length请求数据大小
$status用户请求响应状态码
$bytes_sent发送到客户端响应数据的大小
$body_bytes_sent用户请求返回的响应体字节数
$http_refererHTTP 请求头中属性字段 referer

示例:

#日志格式
http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log /var/log/nginx/access.log main;
    ...
}
#对应的日志内容
[root@web-svr-01 conf.d]# cat /var/log/nginx/access.log
127.0.0.1 - - [11/Oct/2024:10:41:40 +0800] "GET / HTTP/1.1" 200 14 "-" "curl/7.29.0" "-"
127.0.0.1 - - [11/Oct/2024:10:42:41 +0800] "GET / HTTP/1.1" 200 14 "-" "curl/7.29.0" "-"

log_format 中的单引号主要起到标明字符串和换行的作用,例如

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';

和示例中的效果是一样的,不过看起来不方便而已。当定义的字段在访问时未出现的时候会用空格代替

一般我们使用默认的变量就可以满足基本需求,当无法满足需求的时候,也可以自定义变量,或者通过三方插件进行扩充

  1. access_log定义访问日志的位置和使用的格式(log_format),可以出现的位置较多,不过多数情况放在http全局块或server全局块中
说明配置
Syntaxaccess_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Defaultaccess_log logs/access.log combined;
Contexthttp, server, location, if in location, limit_except

示例:

log_format compression '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $bytes_sent '
                       '"$http_referer" "$http_user_agent" "$gzip_ratio"';

access_log /spool/logs/nginx-access.log compression buffer=32k;
2. 错误日志

错误日志收集在ngx的核心模块[ngx_core_module]中,帮助用户及时判断 Nginx 配置及运行时出错的原因,错误日志也可以通过 Nginx 内置指令进行配置,但不支持格式定义

说明配置
Syntaxerror_log file [level];
Defaulterror_log logs/error.log error;
Contextmain, http, mail, stream, server, location

关于 error_log 指令需要说明:

  • 在同一级别的指令域中,也可指定多个日志;
  • 指令值中的第一个参数是输出日志的方式,默认是输出到本地的文件中。该指令也支持输出到 syslog 或内存缓冲区中;
error_log syslog:server=192.168.2.109 error;
error_log memory:32m debug;
error_log /dev/null;
 
# 访问文件不存在时,记入错误日志
log_not_found on;

指令值中第二个参数是输出日志的级别,指定的级别将包含自身及级别值比其小的所有级别日志,日志内容会保存到第一个参数设定的输出位置。

错误日志级别及相关说明如下表所示:

级别级别值级别说明
debug8代码中标记为 NGX_LOG_DEBUG 的输出,输出最为详细,配合调试使用
info7代码中标记为 NGX_LOG_INFO 的输出,因包括除 debug 级别的所有输出,故同样会消耗大量磁盘 IO 资源
notice6代码中标记为 NGX_LOG_NOTICE 的输出
warn5代码中标记为 NGX_LOG_WARN 的输出
error4代码中标记为 NGX_LOG_ERROR 的输出,实际生产环境中常用的输出级别
crit3代码中标记为 NGX_LOG_CRIT 的输出
alert2代码中标记为 NGX_LOG_ALERT 的输出
emerg1代码中标记为 NGX_LOG_EMERG 的输出

一般我们使用的日志级别是notice,视具体情况而定

3. 实例演示

现在为我们的cainiao网站,添加日志功能。

[root@web-svr-01 conf.d]# cat www.cainiao.cn.conf
server {
  listen 80;
  server_name www.cainiao.cn;
  root /app/code/www;

  access_log /var/log/nginx/www.cainiao.cn.access.log main;
  error_log /var/log/nginx/www.cainiao.cn.error.log notice;

  location / {
    index index.html;
  }

}

[root@web-svr-01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-svr-01 conf.d]# systemctl reload nginx
[root@web-svr-01 conf.d]# curl -H Host:www.cainiao.cn 192.168.202.131
cainiao index
[root@web-svr-01 conf.d]# cat /var/log/nginx/www.cainiao.cn.access.log
192.168.202.131 - - [11/Oct/2024:11:22:27 +0800] "GET / HTTP/1.1" 200 14 "-" "curl/7.29.0" "-"
[root@web-svr-01 conf.d]# cat /var/log/nginx/www.cainiao.cn.error.log
[root@web-svr-01 conf.d]#

在同一个服务器上,可能存在多个虚拟主机的情况下,建议把日志写在每个虚拟主机的server块中,并且直接用网站命名日志名称。

2. 网站功能列表

所属模块 Module ngx_http_autoindex_module

1. autoindex

Nginx的autoindex功能是用于在Nginx配置中启用或禁用自动索引的。当启用此功能时,如果Nginx在目录中找不到默认的首页(如index.html),它将生成一个包含该目录中所有文件和子目录的HTML页面,用于在请求目录而非文件时浏览文件、查看文件信息。它可通过配置选项定制,如显示确切文件大小、本地时间和自定义页面格式。优点包括方便浏览、易于配置和提供文件信息。

缺点包括安全风险、性能影响和无法自定义页面外观。

格式:

说明配置
Syntaxautoindex on
Defaultautoindex off;
Contexthttp, server, location

示例:

[root@web-svr-01 conf.d]# cat www.cainiao.cn.conf
server {
  listen 80;
  server_name www.cainiao.cn;
  root /app/code/www;

  access_log /var/log/nginx/www.cainiao.cn.access.log main;
  error_log /var/log/nginx/www.cainiao.cn.error.log notice;

  location / {
    index index.html;
    autoindex on;
  }

}

[root@web-svr-01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-svr-01 conf.d]# systemctl reload nginx

[root@web-svr-01 conf.d]# cd /app/code/www/
[root@web-svr-01 www]# touch {1..5}
[root@web-svr-01 www]# mv index.html index.html.bak

在这里插入图片描述

2. autoindex_localtime

nginx网站功能列表中的autoindex_localtime是一个指令,用于在nginx的自动索引功能中启用本地时间格式。

格式:

说明配置
Syntaxautoindex_localtime on
Defaultautoindex_localtime off;
Contexthttp, server, location

示例:

[root@web-svr-01 conf.d]# cat www.cainiao.cn.conf
server {
  listen 80;
  server_name www.cainiao.cn;
  root /app/code/www;

  access_log /var/log/nginx/www.cainiao.cn.access.log main;
  error_log /var/log/nginx/www.cainiao.cn.error.log notice;

  location / {
    index index.html;
    autoindex on;
    autoindex_localtime on;
  }

}

[root@web-svr-01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-svr-01 conf.d]# systemctl reload nginx

[root@web-svr-01 www]# touch {1..5}
[root@web-svr-01 www]# ll
total 4
-rw-r--r-- 1 root root  0 Oct 11 13:54 1
-rw-r--r-- 1 root root  0 Oct 11 13:54 2
-rw-r--r-- 1 root root  0 Oct 11 13:54 3
-rw-r--r-- 1 root root  0 Oct 11 13:54 4
-rw-r--r-- 1 root root  0 Oct 11 13:54 5
-rw-r--r-- 1 root root 14 Oct 11 09:53 index.html.bak

在这里插入图片描述

3. 认证功能

1. auth_basic

指令为auth_basic,所属模块 Module ngx_http_auth_basic_module

Nginx的auth_basic指令用于启用HTTP基本认证,它需要一个认证页面提示用户输入用户名和密码。

auth_basic格式:

说明配置
Syntaxauth_basic string
Defaultauth_basic off;
Contexthttp, server, location, limit_except

在这个配置中,auth_basic指令后跟一个提示信息,当用户访问受保护的区域时,会看到这个信息。auth_basic_user_file指令指定一个密码文件,Nginx将使用这个文件来验证用户名和密码

auth_basic_user_file 格式:

说明配置
Syntaxauth_basic_user_file file;
Default
Contexthttp, server, location, limit_except

账户密码文件

#Specifies a file that keeps user names and passwords, in the following format:

# comment
name1:password1
name2:password2:comment
name3:password3

创建密码文件需要使用htpasswd工具,这个工具通常与Apache的httpd-tools包一起安装。以下是创建密码文件的命令:

sudo htpasswd -c /etc/nginx/.htpasswd username

上面的命令会提示输入密码,并创建一个包含认证信息的文件。使用-c选项会创建一个新文件,如果文件已存在,则不应使用此选项以避免覆盖现有用户。

请确保Nginx用户有权限读取.htpasswd文件,通常这意味着设置合适的文件权限和所有权。

注意:auth_basic模块的优点包括实现简单、配置方便,适用于不需要高强度安全保护的场景。‌ 然而,它的缺点是用户名和密码以Base64编码的形式出现在Authorization头部,容易被截取和解析,因此不适合用于保护高安全性的数据‌

2. 实例演示

下面我们为cainiao网站增加用户基本认证功能

安装htpasswd工具,并增加账户cainiao

[root@web-svr-01 www]# yum install httpd-tools
[root@web-svr-01 www]# htpasswd -c /etc/nginx/.cainiao.pw cainiao
New password:
Re-type new password:
Adding password for user cainiao
[root@web-svr-01 www]# cat /etc/nginx/.cainiao.pw
cainiao:$apr1$QpVaBatU$qkPNkGeHGY1.MQqDgjTZe/

修改网站配置文件

[root@web-svr-01 conf.d]# cat www.cainiao.cn.conf
server {
  listen 80;
  server_name www.cainiao.cn;
  root /app/code/www;

  access_log /var/log/nginx/www.cainiao.cn.access.log main;
  error_log /var/log/nginx/www.cainiao.cn.error.log notice;

  location / {
    index index.html;
    autoindex on;
    autoindex_localtime on;

    auth_basic "cainiao website";
    auth_basic_user_file /etc/nginx/.cainiao.pw;
  }

}

[root@web-svr-01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-svr-01 conf.d]# systemctl reload nginx

强制刷新网页,可以看到要求输入账户及密码了
在这里插入图片描述
输入完账户及密码,可以正常访问

4. 访问控制功能

主要的指令包括:allowdeny,所属模块 Module ngx_http_access_module

1. allow & deny

Nginx 提供了基于 IP 地址的访问控制功能,可以通过 allowdeny 指令来实现。

说明配置
Syntaxallow address | CIDR | unix: | all;
Default
Contexthttp, server, location, limit_except
说明配置
Syntaxdeny address | CIDR | unix: | all;
Default
Contexthttp, server, location, limit_except

可以基于IP地址,网段或者所有。allow与deny是按照顺序执行的,即先执行的先生效

示例:

location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}

被拒绝访问的页面会提示403

2. 局限性

http_access_module 的局限性:

由于此实现的原理是基于客户端的ip来控制,但是nginx并不知道哪一个ip是真正的客户的,如果客户端与服务端的nginx之间还有一层代理(如其他的中间件),如对下图的中对IP1访问IP3做控制,此时IP1经过中间件后,放IP3发送的请求为IP2,即控制失败,所以只能通过$remote_addr控制允许访问,不能控制拒绝访问

在这里插入图片描述

解决办法:
1)HTTP头信息控制:http_x_forwarded_for
在这里插入图片描述

http_x_forwarded_for=Client IP,Proxy(1)IP,Proxy(2)IP,……

2)结合geo模块配置解决
3)HTTP头自定义变量传递

3. 实例演示
[root@web-svr-01 conf.d]# cat www.cainiao.cn.conf
server {
  listen 80;
  server_name www.cainiao.cn;
  root /app/code/www;

  access_log /var/log/nginx/www.cainiao.cn.access.log main;
  error_log /var/log/nginx/www.cainiao.cn.error.log notice;

  location / {
    index index.html;

    allow 10.0.84.22;
    deny all;

    autoindex on;
    autoindex_localtime on;

    auth_basic "cainiao website";
    auth_basic_user_file /etc/nginx/.cainiao.pw;

  }

}

[root@web-svr-01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-svr-01 conf.d]# systemctl reload nginx

在一直使用的机器进行访问:
在这里插入图片描述
10.0.84.22访问
在这里插入图片描述

5. 状态模块

Nginx 状态模块是用于收集 Nginx 的运行状态数据的模块。它可以提供关于 Nginx 的当前连接、处理的请求、状态码等信息。

1. stub_status

指令stub_status 所属模块 Module ngx_http_stub_status_module

说明配置
Syntaxstub_status;
Default
Contextserver, location

In versions prior to 1.7.5, the directive syntax required an arbitrary argument, for example, “stub_status on”.

示例:

location = /basic_status {
    stub_status;
}

可以通过访问 http://localhost/nginx_status 来查看 Nginx 的状态信息。

常见的状态信息:

状态说明
Active connections当前活跃的连接数。
server accepts handled requests服务器接受、处理和完成的请求数。
Reading读取客户端请求的连接数。
Writing响应客户端请求的连接数。
Waiting等待客户端请求的连接数。
2. 实例演示
[root@web-svr-01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-svr-01 conf.d]# systemctl reload nginx
[root@web-svr-01 conf.d]# cat www.cainiao.cn.conf
server {
  listen 80;
  server_name www.cainiao.cn;
  root /app/code/www;

  access_log /var/log/nginx/www.cainiao.cn.access.log main;
  error_log /var/log/nginx/www.cainiao.cn.error.log notice;

  location / {
    index index.html;

    stub_status;
    #allow 10.0.84.22;
    #deny all;

    autoindex on;
    autoindex_localtime on;

    auth_basic "cainiao website";
    auth_basic_user_file /etc/nginx/.cainiao.pw;

  }

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值