nginx 的常用模块

时间:2020-07-14
本文章向大家介绍nginx 的常用模块,主要包括nginx 的常用模块使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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 别人叫我晓东

原文地址:https://www.cnblogs.com/gshelldon/p/13301190.html