Nginx常用模块介绍

系统管理员 2024-03-17 21:03 192阅读 0赞

Nginx常用模块

Nginx官方文档:

https://nginx.org/en/docs/

1、目录索引模块

ngx_http_autoindex_module (适合建yum仓库用)

  1. # Example Configuration
  2. location / {
  3. autoindex on;
  4. }
  5. # Directives
  6. Syntax(语法):autoindex on | off;
  7. Default(默认):autoindex off;
  8. Context(环境):http, server, location

如何配置:例如

  1. 0 10:16:14 root@web01,172.16.1.7:~ # cd /etc/nginx/conf.d
  2. 0 10:18:20 root@web01,172.16.1.7:/etc/nginx/conf.d # ll
  3. total 4
  4. -rw-r--r-- 1 root root 1072 May 24 23:33 default.conf
  5. 0 10:18:21 root@web01,172.16.1.7:/etc/nginx/conf.d # vim xxx.conf 创个配置文件
  6. server {
  7. listen 80;
  8. server_name blog.xxx.com;
  9. location / {
  10. autoindex on; # <<----放进去
  11. root /xxx1;
  12. index index.html;
  13. }
  14. }
  15. # ps:nginx默认会去xxx1目录下去找index.html,如果找到了会交给下面index处理。没找到的话走autoindex模块
  16. mkdir /xxx1 && cd /xxx1
  17. cd xxx1
  18. mkdir 1
  19. mkdir 2
  20. mkdir abc
  21. systemctl start nginx ,打开网页:

c5f145a592f4446785727965a6e2a932_noop.image_iz_58558_from_article.pc_detail_x-expires_1690177184_x-signature_Smdhvg_2BGN4bh9wnEenTyAKxz3_2BQ_3D

优化1:文件大小展示,下点东西进xxx1

  1. Syntax:autoindex_exact_size on | off;
  2. Default:autoindex_exact_size on; (把这行放进去)
  3. Context:http, server, location
  4. location / {
  5. autoindex on;
  6. autoindex_exact_size off <<---放进去
  7. root /xxx1;
  8. index index.html;
  9. }

cb8158369b204e44a46b6c37ea6630b6_noop.image_iz_58558_from_article.pc_detail_x-expires_1690177184_x-signature_oj3FStGlzPz_2F6dd_2BBUnUXdO8We0_3D

优化2 显示格式

  1. Syntax: autoindex_format html | xml | json | jsonp; html有时显示中文是乱码,通过修改json形式可以看到中文
  2. Default:autoindex_format html;
  3. Context:http, server, location
  4. 例如:
  5. server {
  6. listen 80;
  7. server_name blog.xxx.com;
  8. location / {
  9. autoindex on;
  10. autoindex_exact_size off;
  11. autoindex_format json; <<----换成json
  12. root /xxx1;
  13. }
  14. }

db364d4c6b994d2c9f0b2ba860c3bf67_noop.image_iz_58558_from_article.pc_detail_x-expires_1690177184_x-signature_JlIXS_2BN_2FYZPrHk9Ri8rSNXQWDyQ_3D

优化3 显示本地时间

  1. Syntax:autoindex_localtime on | off;
  2. Default:autoindex_localtime off;
  3. Context:http, server, location

2、状态监控模块

ngx_http_stub_status_module

https://nginx.org/en/docs/http/ngx\_http\_stub\_status\_module.html

  1. # Example Configuration
  2. location = /basic_status {
  3. stub_status;
  4. }
  5. # 例如:
  6. server {
  7. listen 80;
  8. server_name blog.xxx.com;
  9. location / {
  10. autoindex on;
  11. autoindex_exact_size off;
  12. root /xxx1;
  13. }
  14. location = /basic_status { #<<------ 另起一层location
  15. stub_status;
  16. }
  17. }
  18. ##### 各参数的解释
  19. Active connections # 当前活动的连接数
  20. accepts # 当前的总连接数 of TCP
  21. handled # 成功的连接数TCP
  22. requests # 通过各终端访问的的http总请求数(每刷新一次就+1)
  23. Reading # 请求
  24. Writing # 响应
  25. Waiting # 等待的请求数,开启了keepalive
  26. # ps: 长格式:一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
  27. keepalive_timeout 65; # 65s没有活动则断开连接
  28. keepalive_timeout 0; # 类似于关闭长连接

d810443142d64c5e8341396d402d462d_noop.image_iz_58558_from_article.pc_detail_x-expires_1690177184_x-signature_mzzB_2B6rbr8iQOXjqR9lVfQ3SND8_3D

实验:用linux服务器访问nginx页面(web02用curl访问页面)

  1. # 先把配置改一下
  2. server {
  3. listen 80;
  4. server_name 10.0.0.7; # <<-----改成10.0.0.7,因为web02里没有做域名解析
  5. location / {
  6. autoindex on;
  7. autoindex_exact_size off;
  8. root /xxx1;
  9. }
  10. location = /basic_status {
  11. stub_status;
  12. }
  13. }
  14. # 连接
  15. 0 11:11:15 root@web02,172.16.1.8:~ # curl -s 10.0.0.7/basic_status (-s 显示静态)
  16. Active connections: 3
  17. server accepts handled requests
  18. 23 23 38
  19. Reading: 0 Writing: 1 Waiting: 2
  20. 0 11:12:18 root@web02,172.16.1.8:~ # curl -s 10.0.0.7/basic_status | awk 'NR==3 {print $1}'
  21. 24
  22. 0 11:14:29 root@web02,172.16.1.8:~ # curl -s 10.0.0.7/basic_status | awk 'NR==3 {print $1}'
  23. 25

3、访问控制模块

ngx_http_access_module

  1. # Example Configuration
  2. location / {
  3. deny 192.168.1.1;
  4. allow 192.168.1.0/24;
  5. allow 10.1.1.0/16;
  6. allow 2001:0db8::/32;
  7. deny all;
  8. }
  9. # 允许配置语法
  10. Syntax: allow address | CIDR | unix: | all;
  11. Default:
  12. Context: http, server, location, limit_except
  13. # 拒绝配置语法
  14. Syntax:deny address | CIDR | unix: | all;
  15. Default:—
  16. Context:http, server, location, limit_except
  17. 默认是allow all

4、访问认证模块

ngx_http_auth_basic_module

https://nginx.org/en/docs/http/ngx\_http\_auth\_basic\_module.html

  1. # 配置示例格式
  2. location / {
  3. auth_basic "closed site"; (密码输错会显示的信息,比如密码提示)
  4. auth_basic_user_file conf/htpasswd;
  5. }
  6. # 说明
  7. 1、配到哪一层,进哪一层就要输密码
  8. 2、通常和auth_basic配合使用的工具是htpasswd,该工具来源于httpd-tools包,主要用于生成用户及其密码加密文件

实操举例:

  1. # 安装htpasswd
  2. yum install -y httpd
  3. # 在/etc/nginx下创建auth目录,把密码都放这
  4. root@web01,172.16.1.7:/etc/nginx # mkdir auth
  5. # 执行命令生成秘钥文件
  6. htpasswd -bc /etc/nginx/auth/xxx_auth xxx 123
  7. -b 允许命令输入密码
  8. -c 创建一个新文件,将用户名和密码保存到文件中
  9. 0 21:00:11 root@web01,172.16.1.7:/etc/nginx/auth # cat xxx_auth
  10. xxx:$apr1$0PjJZR1K$ZYD9kukcrNzMIfTAa9y3x.
  11. # 修改xxx.conf配置文件
  12. 0 21:08:08 root@web01,172.16.1.7:/etc/nginx/conf.d # vim xxx.conf
  13. server {
  14. listen 80;
  15. server_name blog.xxx.com;
  16. location / {
  17. autoindex on;
  18. autoindex_exact_size off;
  19. root /xxx1;
  20. }
  21. location = /basic_status {
  22. stub_status;
  23. auth_basic "closed site"; # <<--------配在这一层,进监控模块时便需密码
  24. auth_basic_user_file /etc/nginx/auth/xxx_auth;
  25. }
  26. }
  27. # 保存退出 重载Nginx,再打开网页便需输入密码,输入xxx和123可进入
  28. http://blog.xxx.com/basic_status

发表评论

表情:
评论列表 (有 0 条评论,192人围观)

还没有评论,来说两句吧...

相关阅读

    相关 9.Nginx模块

    1.nginx开启目录浏览 提供下载功能 默认情况下,网站返回index指定的主页,若该网站不存在主页,则将请求交给autoindex模块 如果开启autoindex

    相关 模块介绍

      光模块是起到光电转换作用的一种连接模块,其中发送端把电信号转换成光信号,通过光纤传送后,接收端再把光信号转换成电信号。由光电子器件、功能电路和光接口等组成,光电子器件包括发