nginx的常用模块

ngx_http_index_module


Syntax:	index file ...;
Default:
index index.html;
Context: http, server, location
location / {
index index.html;
}

ngx_http_autoindex_module


在没有主页的时候,会自动以目录的方式生成主页,如果在指定的默认站点目录下有index.html的文件会自动的被打开,默认关闭。

Syntax:	autoindex on | off;
Default: autoindex off;
Context: http, server, location # 配置方法:
location / {
# 开启autoindex模块
autoindex on;
} # ==========================================================================
# 按单位显示文件大小(off),默认按字节精确输出(on)
Syntax: autoindex_exact_size on | off;
Default: autoindex_exact_size on;
Context: http, server, location # 配置方法:
location / {
autoindex on;
# 会按照单位输出,M、k。
autoindex_exact_size off
} # 显示本地时间,默认关闭。
Syntax: autoindex_localtime on | off;
Default: autoindex_localtime off;
Context: http, server, location # 配置方法:
location / {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}

ngx_http_log_module


记录日志的生成格式

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;

ngx_http_charset_module


设置nginx显示的字符编码,默认关闭的状态utf-8是最常用的字符编码方式。

Syntax:	charset charset | off;
Default: charset off;
Context: http, server, location, if in location # 配置方法
include conf/koi-win;
charset windows-1251; # 这是要设置的字符集
source_charset koi8-r;

ngx_http_stub_status_module


监控nginx的模块

#
Syntax: stub_status;
Default: —
Context: server, location # 配置方法
location = /basic_status {
stub_status;
}
# 或者 自己命名
location /jk {
stub_status;
} # 使用域名或者ip加上监控模块的名称访问。

Active connections  # 当前活动的连接数
accepts # 当前的总连接数TCP
handled # 成功的连接数TCP
requests # 总的http请求数 Reading # 请求
Writing # 响应
Waiting # 等待的请求数,开启了keepalive # 注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s没有活动则断开连接

短链接:每请求一次服务器上的资源建立一次连接。

长连接:一直与服务器连接着,直接向服务器请求资源,有超时时间。

ngx_http_auth_basic_module


网页认证模块,用于网页的用户名和密码的身份认证。

Syntax:	auth_basic string | off;
Default:
auth_basic off;
Context: http, server, location, limit_except # 使用方法,要让哪个模块开启认证就把{}里面的内容添加到模块里面。
location / {
# 注释,认证时候的提示
auth_basic "closed site";
# 密码文件认证时的
auth_basic_user_file conf/htpasswd;
} # 比如要把监控模块加密
location /jk {
stub_status;
# 注释,认证时候的提示
auth_basic "closed site";
# 密码文件认证时的
auth_basic_user_file /etc/nginx/pass/jk.pass;
} # 创建目录
[root@web01 /etc/nginx]# mkdir /etc/nginx/pass # 安装认证的密码生成命令htpasswd
[root@web01 /etc/nginx]# yum -y install httpd-tools # 生成用户为gong密码是123的密码配置文件。
[root@web01 /etc/nginx]# htpasswd -b -c /etc/nginx/pass/jk.pass gong 123 htpasswd
-b # 后面指定一个密码,免交互。
-c # 创建一个新文件

index


# 语法
location / {
index index.html;
}
# 或者
location / {
index index.html,index.php;
}

autoindex


# 语法
location / {
autoindex on;
} # 注:前提是该目录下不存在index.html文档,如果存在则直接解析index内容,而不是浏览目录。 url:http://nginx.org/en/download.html
# 资源路径
uri:/en/download.html/en/download.html # autoindex的格式
Syntax: autoindex_format html | xml | json | jsonp;
Default:
autoindex_format html;
Context: http, server, location # 开启autoindex,默认关闭。
autoindex off; # 显示本地服务器的时间,默认关闭
autoindex_localtime off; # 显示文件大小,off不显示详细信息
autoindex_exact_size on;

三种需求

1.访问www.gong.com打开主页,www.gong.comg/gong打来另外一个页面。

[root@web01 /etc/nginx/conf.d]# vi gong.conf
server {
listen 80;
server_name www.gong.com; location / {
root /code/gong;
index index.html;
}
location /gong {
root /code/gong;
index index.html;
}
} [root@web01 /etc/nginx/conf.d]# mkdir /code/gong
[root@web01 /etc/nginx/conf.d]# echo 'home webpage' > /code/gong/index.html
[root@web01 /etc/nginx/conf.d]# mkdir /code/gong/gong
[root@web01 /etc/nginx/conf.d]# echo 'gong webpage' > /code/gong/gong/index.html

结论:

  • 1、如果在配置文件中只有location / {}模块,如果我在浏览器中访问域名+不知名的资源路径的时候,就会在location / {}下指定的目录找文件或者目录。

  • 2、如果配置了location /以外的location,那么在域名后面加目录或者文件,会去location指定目录下去找。

2、把/opt/xxx/gong变成一个下载站点目录(浏览目录,开启autoindex)

[root@web01 /etc/nginx/conf.d]# vi gong.conf
server {
listen 80;
server_name www.gong.com; location / {
root /code/gong;
index index.html;
}
location /gong {
root /opt/xxx;
autoindex on;
}
} [root@web01 /etc/nginx/conf.d]# mkdir -p /opt/xxx/gong/
[root@web01 /etc/nginx/conf.d]# mkdir /opt/xxx/gong/
[root@web01 /etc/nginx/conf.d]# touch /opt/xxx/gong/aa.txt

结论: location /里面的“/”指的就是root所执行的路径。

3、把/opt/xxx/download变成一个下载站点的目录。

[root@web01 /etc/nginx/conf.d]# vi gong.conf
server {
listen 80;
server_name www.gong.com;
charset utf-8; location / {
root /code/gong;
index index.html;
}
location /gong {
root /opt/xxx;
autoindex on;
}
location /down {
alias /opt/xxx/download;
autoindex on;
autoindex_format html;
autoindex_localtime on;
autoindex_exact_size on;
}
} [root@web01 /etc/nginx/conf.d]# mkdir /opt/xxx/download
[root@web01 /etc/nginx/conf.d]# touch /opt/xxx/download/'说明.txt'

结论:

3、只要location里面写的root,那么root指定的目录就是/;

4、root会受到uri的影响,alias不会受到uri的影响。

认证模块

# 使用方法
location / {
# 默认关闭,有字符串就开启了。
auth_basic "closed site";
# 指定的密码文件
auth_basic_user_file conf/htpasswd;
} Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http, server, location, limit_except # 需要安装httpd-tools
yum -y install httpd-tools
htpasswd生成密码文件。

状态模块

# 使用方法
location = /basic_status {
stub_status;
} Syntax: stub_status;
Default: —
Context: server, location

字符集模块

# 默认是关闭的状态
# 使用方法 Syntax: charset charset | off;
Default:
charset off;
Context: http, server, location, if in location charset utf-8;
charset gbk,utf-8;

ngx_http_access_module


基于ip的访问控制

# 访问控制,使用方法,需要先允许再拒绝。
location / {
# 拒绝指定iP
deny 192.168.1.1;
# 允许某个网段
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
} curl构造用户名和密码访问。
curl -u username:password http://example.com # 做一个实验

ngx_http_limit_conn_module


连接频率限制,只能测公网的。

# 一般写在http层,或者写在conf.d里面的servce层外面。server层调用。
http {
# 开启一个内存空间,设置了一个变量addr设置内存空间大小10m(连接频率)
limit_conn_zone $remote_addr zone=addr:10m; ... server { ... location /download/ {
# 调用,限制同时最高1个连接
limit_conn addr 1;
}
#--------------------------------------------
Syntax: limit_conn zone number;
Default: —
Context: http, server, location [root@web01 ~]# yum install -y httpd-tools
# 表示两秒种内发送20次请求
[root@web01 ~]# ab -n 20 -c 2 http://127.0.0.1/index.html

ngx_http_limit_req_module


请求频率限制,一般写在http模块中。

http {
# http标签段定义请求限制, rate限制速率,限制一秒钟最多一个IP请求
# zooe开启一个内存空间名字叫做one,大小10m
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; ... server { ... location /search/ {
limit_req zone=one burst=5;
} # ==========================================
server {
...
# 调用limit_req变量(http层),zone指定共享内存空间的名字(perip),burst超过该配置的请求数,则返回503
# 请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量, 多余的请求返回503
limit_req zone=one burst=5 nodelay;
# 请求超过1r/s,请求数超过burst定义的数量, 多余的请求返回503
limit_req zone=one burst=10;
} nodelay # 延迟处理
默认delay # 返回错误页面的设置
Syntax: limit_req_status code;
Default: limit_req_status 503;
Context: http, server, location # 返回错误页路径,和返回错误页面配和使用。
limit_req_status 503;
# 错误页实在默认站点目录的下去找。
error_page 503 /503_err.html
[root@web01 ~]# vi /etc/nginx/conf.d/gong.conf
server {
listen 80;
server_name www.gong.com;
charset utf-8; location / {
root /code/gong;
index index.html;
}
location /gong {
root /opt/xxx;
autoindex on;
# 只允许10.0.0.1的ip访问,其余的全部拒绝。
allow 10.0.0.1;
deny all;
}
location /down {
alias /opt/xxx/download;
autoindex on;
autoindex_format html;
autoindex_localtime on;
autoindex_exact_size on;
}
} #===========================分隔符======================== [root@web01 /code/gong]# vi /etc/nginx/conf.d/gong.conf
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server {
listen 80;
server_name www.gong.com;
charset utf-8; location / {
root /code/gong;
index index.html;
# 当请求数量每分钟超过1次的时候会弹出503设置的页面
limit_req zone=one burst=5 nodelay;
limit_req_status 503;
error_page 503 /503_err.html;
}
location /gong {
root /opt/xxx;
autoindex on;
allow 10.0.0.1;
deny all;
}
location /down {
alias /opt/xxx/download;
autoindex on;
autoindex_format html;
autoindex_localtime on;
autoindex_exact_size on;
}
location = /jk {
stub_status;
}
}
FBI WARNING

QQ:1402122292 认准原创sheldon 别人叫我晓东

nginx 的常用模块的更多相关文章

  1. Nginx基础 - 常用模块配置

    1.Nginx状态监控http_stub_status_module记录Nginx客户端基本访问状态信息 location /mystatus { stub_status on; access_log ...

  2. nginx应用场景,特性,目录结构,常用模块,内置变量,URL和URI,http状态码,配置文件详解

    1.nginx介绍 1丶俄罗斯人开发的,开源www服务软件 2丶软件一共780K 3丶nginx本身是一款静态(html,js,css,jpg等)www软件 4丶静态小文件高并发,同时占用的资源很少, ...

  3. nginx常用模块(三)

    Nginx常用模块(三) ngx_http_proxy_module模块配置(http或https协议代理) proxy_pass URL; 应用上下文:location, if in locatio ...

  4. Nginx 常用模块

    Nginx 常用模块 1. ngx_http_autoindex_module # ngx_http_autoindex_module模块处理以斜杠字符(' / ')结尾的请求,并生成一个目录列表. ...

  5. nginx常用模块

    Nginx模块介绍 核心模块:core module 标准模块:stand modules HTTP modules: Standard HTTP modules Optional HTTP modu ...

  6. 《Ansible权威指南》笔记(3)——Ad-Hoc命令集,常用模块

    五.Ad-Hoc命令集1.Ad-Hoc命令集通过/usr/bin/ansible命令实现:ansible <host-pattern> [options]    -v,--verbose  ...

  7. day--6_python常用模块

    常用模块: time和datetime shutil模块 radom string shelve模块 xml处理 configparser处理 hashlib subprocess logging模块 ...

  8. Tengine 常用模块使用介绍

    Tengine 和 Nginx Tengine简介 从2011年12月开始:Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能 和特性. ...

  9. Saltstack常用模块及API

    Saltstack提供了非常丰富的功能模块,涉及操作系统的基础功能.常用工具支持等,更多模块信息可以查看官网模块介绍.也可以通过sys模块列出当前版本支持的模块. salt '*' sys.list_ ...

随机推荐

  1. Class类文件结构--访问标志

    访问标志的位置:在常量池结束之后的两个字节(16位)表示访问标志access_flags. 访问标志的作用:用于标识类或者接口层次的访问信息:比如该Class是类还是接口,是否为public类型.是否 ...

  2. jinja2的简单使用

    后端代码 from jinja2 import Template def index(): with open('./index.html', 'r', encoding='utf-8') as fp ...

  3. JPA之排序条件查询

    List<Monitoring> monitoringList = repository.findAll((root, query, cb) -> { List<Predica ...

  4. 案例分析——Who is the king of handwriting notes?

    案例分析--Who is the king of handwriting notes? 项目 内容 这个作业属于那个课程 2021春季学期软件工程(罗杰.任健) 这个作业的要求在哪里 案例分析 我在这 ...

  5. 991. Broken Calculator

    On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...

  6. php异常及错误信息捕获并记录日志实现方法全解析

    php异常处理 什么是异常? PHP 5 提供了一种新的面向对象的错误处理方法.异常处理用于在指定的错误(异常)情况发生时改变脚本的正常流程.这种情况称为异常. 当异常被触发时,通常会发生: 当前代码 ...

  7. 使用 mvn install 命令将本地jar包注册到本地maven仓库

    前提: 要安装maven并配置环境变量. windows 系统环境变量配置 新建环境变量:MAVEN_HOME    值为:maven的解压包路径或安装路径. 在path 环境变量中添加:%MAVEN ...

  8. SpringCloud之配置中心(config)的使用Git+数据库实现

    SpringCloud微服务实战系列教程 -------------------------目录------------------------------ 一.配置中心应用(Git) 二.配置中心的 ...

  9. POJ 1386 欧拉路的判定

    题意:       给你n个单词,问你有没有一种排列方式可以所有单词的首部是相邻单词的尾部. 思路:       这个题目还挺基础的,就是个欧拉的判定,首先对于每一个单词,我们把他抽象成边,每个单词两 ...

  10. hdu4923 f(A,B)分段处理

    题意:        给你序列A,让你构造序列B然后求出最小的f(A <B),其中A 是0,或者1组成的,而B是[0,1]的实数,f(A,B) = 求和(i从1到n) (Ai - Bi)^ 2. ...