Nginx系列之6:重要模块

nginx模块分为两种,官方和第三方,我们通过命令 nginx -V 查看 nginx已经安装的模块!

[root@localhost ~]# nginx -V

nginx version: nginx/1.15.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

Nginx模块名称模块作用
ngx_http_access_module 四层基于IP的访问控制,可以通过匹配客户端源IP地址进行限制
ngx_http_auth_basic_module 状态页,使用basic机制进行用户认证,在编译安装nginx的时候需要添加编译参数--withhttp_stub_status_module,否则配置完成之后监测会是提示语法错误
ngx_http_stub_status_module 状态统计模块
ngx_http_gzip_module 文件的压缩功能
ngx_http_gzip_static_module 静态压缩模块
ngx_http_ssl_module nginx 的https 功能
ngx_http_rewrite_module 重定向模块,解析和处理rewrite请求
ngx_http_referer_module 防盗链功能,基于访问安全考虑
ngx_http_proxy_module 将客户端的请求以http协议转发至指定服务器进行处理
ngx_stream_proxy_module tcp负载,将客户端的请求以tcp协议转发至指定服务器处理
ngx_http_fastcgi_module 将客户端对php的请求以fastcgi协议转发至指定服务器助理
ngx_http_uwsgi_module 将客户端对Python的请求以uwsgi协议转发至指定服务器处理
ngx_http_headers_module 可以实现对头部报文添加指定的key与值
ngx_http_upstream_module 负载均衡模块,提供服务器分组转发、权重分配、状态监测、调度算法等高级功能
ngx_stream_upstream_module 后端服务器分组转发、权重分配、状态监测、调度算法等高级功能
ngx_http_fastcgi_module 实现通过fastcgi协议将指定的客户端请求转发至php-fpm处理
ngx_http_flv_module 为flv伪流媒体服务端提供支持
 

nginx_substitutions_filter 响应过滤替换

这个是比较冷门但异常强大的模块,它不是重定向,也不是请求过滤,而是对http响应进行魔术般的过滤替换。在响应体上执行正则表达式和固定字符串替换的过滤模块。这个模块与NGINX的本机替换模块非常不同。它扫描输出链缓冲区并逐行匹配字符串。
一个真实的例子:
    location / {
            proxy_pass  https://198.198.20.188:8443;
            proxy_redirect default;
        subs_filter_types text/css text/plain text/xml text/javascript application/javascript application/json application/xml;
        subs_filter 'https://198.198.20.188:8443' 'https://nytalktest.gdnybank.com:6017' gir;
        subs_filter '198.198.20.188:8443' 'nytalktest.gdnybank.com:6017' gir;
        subs_filter 'https%3a%2f%2f198.198.20.188%3a8443' 'https%3a%2f%2fnytalktest.gdnybank.com%3a6017' gir;
        proxy_set_header Accept-Encoding "";
        }

安装:

git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
./configure --add-module=/path/to/module

nginx-push-stream-module

Push Stream Module使用http技术来实现连接管道,在项目里主要用于即时消息的推送,比如聊天功能。

Push Stream Module主要采用pub/sub模式来管理长连接,用户可以申请连接通道,通道建立订阅该通道,消息推送者可以向连接通道发送消息,这样订阅该通道的所有用户都可以接收到该消息。
配置:

# add the push_stream_shared_memory_size to your http context
    http {
       push_stream_shared_memory_size 32M;

        # define publisher and subscriber endpoints in your server context
        server {
           location /channels-stats {
                # activate channels statistics mode for this location
                push_stream_channels_statistics;

                # query string based channel id
                push_stream_channels_path               $arg_id;
            }

            location /pub {
               # activate publisher (admin) mode for this location
               push_stream_publisher admin;

                # query string based channel id
                push_stream_channels_path               $arg_id;
            }

            location ~ /sub/(.*) {
                # activate subscriber (streaming) mode for this location
                push_stream_subscriber;

                # positional channel path
                push_stream_channels_path                   $1;
            }
        }
    }

发布订阅例子

# Subs
    curl -s -v --no-buffer 'http://localhost/sub/my_channel_1'
    curl -s -v --no-buffer 'http://localhost/sub/your_channel_1'
    curl -s -v --no-buffer 'http://localhost/sub/your_channel_2'

    # Pubs
    curl -s -v -X POST 'http://localhost/pub?id=my_channel_1' -d 'Hello World!'
    curl -s -v -X POST 'http://localhost/pub?id=your_channel_1' -d 'Hi everybody!'
    curl -s -v -X POST 'http://localhost/pub?id=your_channel_2' -d 'Goodbye!'

    # Channels Stats for publisher (json format)
    curl -s -v 'http://localhost/pub?id=my_channel_1'

    # All Channels Stats summarized (json format)
    curl -s -v 'http://localhost/channels-stats'

    # All Channels Stats detailed (json format)
    curl -s -v 'http://localhost/channels-stats?id=ALL'

    # Prefixed Channels Stats detailed (json format)
    curl -s -v 'http://localhost/channels-stats?id=your_channel_*'

    # Channels Stats (json format)
    curl -s -v 'http://localhost/channels-stats?id=my_channel_1'

    # Delete Channels
    curl -s -v -X DELETE 'http://localhost/pub?id=my_channel_1'

详细介绍:

https://github.com/wandenberg/nginx-push-stream-module

ngx_http_gzip_module模块

压缩模块,有利于传输数据的大小减少,但是cpu使用会变高。因为要对传输的数据进行压缩。
1. gzip on | off;
开启或关闭压缩功能
2.gzip_comp_level level;
设置压缩比,一般使用6
3. gzip_disable regex …;
对请求报文的“User-Agent”匹配成功的请求,不进行压缩。
4. gzip_min_length length;
启用压缩功能的响应报文大小阈值;大于某个值才开启压缩功能
5. gzip_buffers number size;
支持实现压缩功能时为其配置的缓冲区数量及每个缓存区的大小;
6. gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any …;
nginx作为代理服务器接收到从被代理服务器发送的响应报文后,在何种条件下启用压缩功能的;

off:对代理的请求不启用
no-cache, no-store,private:表示从被代理服务器收到的响应报文首部的Cache-Control的值为此三者中任何一个,则启用压缩功能;
gzip_types mime-type …;
压缩过滤器,仅对此处设定的MIME类型的内容启用压缩功能;
示例:

gzip on;
gzip_comp_level 6;
gzip_min_length 64;
gzip_proxied any;
gzip_types text/xml text/css application/javascript;

ngx_http_referer_module模块

可以基于这个模块做防盗链。Referer 是 HTTP 请求header 的一部分,当浏览器(或者模拟浏览器行为)向web 服务器发送请求的时候,头信息里有包含 Referer 。比如我在www.google.com 里有一个www.baidu.com 链接,那么点击这个www.baidu.com ,它的header 信息里就有:

Referer=http://www.google.com
valid_referers none | blocked | server_names | string …;
定义referer首部的合法可用值;

none:请求报文首部没有referer首部;
blocked:请求报文的referer首部没有值;
server_names:参数,其可以有值作为主机名或主机名模式;
arbitrary_string:直接字符串,但可使用*作通配符;
regular expression:被指定的正则表达式模式匹配到的字符串;要使用~打头,例如 ~.*.ice.com;
配置示例:

valid_referers none block server_names *.ice.com ; #3设置有效的referers

if($invalid_referer) {
return 403; ##如果referer是不再我们定义的valid_referers列表中,则返回一个403拒绝访问
}

更多第三方模块

https://www.nginx.com/resources/wiki/modules/

posted @ 2020-05-28 01:04  昕友软件开发  阅读(396)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯