惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
博客园_首页
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
博客园 - 司徒正美
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog

吹比💨

NFS 服务引发的一次“诡异”应急响应 银狐使用WDAC来对抗杀软 浏览器第三方Cookie保护导致无法使用Github.dev 使用pushState伪造浏览器地址栏 一次排查网站劫持过程流水账 手动导出 Microsoft Authenticator 中的2FA密钥 使用CloudFlare的IP Lists功能来做访问控制 FrontJS网站错误监控插件存在弹窗广告 宝塔面板可能存在未知漏洞的分析与处置方法 CDN厂商解决域前置问题 中文域名那些事 TLS握手指纹(JA3/JA3S)检测恶意软件流量 [A-Z]{2}\d{2}.cc 团伙的一些公开信息 Azure旗下奇怪的静态托管服务 Csrf与浏览器第三方Cookie策略的问题 Pppoe密码嗅探 Publicsuffix那些事 一次有意思的域名追踪 CobaltStrike通信中host字段的获取 XFF那些事 CORB那些事 Referrer Policy那些事 使用CloudFlare来为网站开启客户端证书验证 DNS那些事 用flask来在线管理你的iptables 屏蔽掉向日葵的有效方法 帝国cms在防止内容采集方面做的工作 身份验证器是如何验证我们的身份? 利用GoogleAppsScript自动回复短信实现保号 有关跨域请求的一些记录
Nginx实用模块
榆木 · 2020-04-08 · via 吹比💨

Nginx 是一个很强大的高性能Web和反向代理应用。原先一直停留在apt install nginx 的阶段,直到开始深入了解其模块等,才发现:nginx牛逼!!nginx除了基础功能以外还有很多有趣且实用的模块。因为nginx内置了许多变量(http://nginx.org/en/docs/varindex.html 部分是模块实现)、逻辑运算、指令,组合起来,就可以实现强大的功能。

本文所使用的环境

OS: Ubuntu 18.04.1 LTS x86_64 
Host: HVM domU 4.2.amazon 
Kernel: 4.15.0-1021-aws 
Shell: bash 4.4.19 
Terminal: /dev/pts/0 
CPU: Intel Xeon E5-2676 v3 (1) @ 2.400GHz 
GPU: Cirrus Logic GD 5446 

由于俺比较菜,就不再编译安装了。我这直接使用apt install nginx进行安装后,查看nginx的模块

root@ip-172-26-6-200:/# nginx -V
nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-GkiujU/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module

ngx_http_geoip_module

配置:

  1. 下载数据库

    这里有一个巨坑。那就是原先提供IP地址数据库的公司不再提供此格式的了(黑人问号??),然后我找到了解决方案 。所以可以直接看这篇文章https://stackoverflow.com/questions/54097838/geoip-dat-gz-and-geolitecity-dat-gz-not-longer-available-getting-404-trying-to

  2. 编辑配置文件,指定IP数据库

    注意:此处编辑的一定是总的nginx.conf,而不是某个站点的配置。比如我的是在(/etc/nginx/nginx.conf),然后在文件中的 http 参数配置里加上我们的数据库。例如:

    http {
    	    geoip_country /etc/nginx/GeoIP.dat; # 第一部中下载的IP数据库存放的路径
            geoip_city /etc/nginx/GeoLiteCity.dat; # 第一部中下载的IP数据库存放的路径
    

    此时nginx服务还能正常重启的话,说明没有问题。如果nginx打不开请检查文件路径和是否配置在http参数下!!

使用:

​ 如果不出意外的话,我们已经可以使用该模块了。根据上面配置的geoip_+IP数据库,nginx多出了几个可供我们使用的参数。例如我们只配置了geoip_country的话,就配置成功了以下参数。

$geoip_country_code

two-letter country code, for example, “RU”, “US”.

$geoip_country_code3

three-letter country code, for example, “RUS”, “USA”.

$geoip_country_name

country name, for example, “Russian Federation”, “United States

这点在 模块使用文档中 http://nginx.org/en/docs/http/ngx_http_geoip_module.html有清晰说明。

然后就可以在站点的配置文件中使用该字段了。例如以下情况:

location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                add_header 'Country' "$geoip_country_code";
        }

保存并重启nginx配置以后访问就可以看到头部添加的参数

HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Country: CN # 就是这里啦。
Content-Encoding: gzip

实用场景:

​ 当我们知道了访客来自哪里后,就可以针对地区做一些操作。比如只允许来自中国和美国的访客访问:

set $deny 1;
if ($geoip_country_code = "CN"){
    set $deny 0;
}
if ($geoip_country_code = "US"){
    set $deny 0;
}
if ($deny = 1){
    return 403;
}

当然也不仅限于此。这里有更多想法:https://github.com/search?q=geoip_country_code&type=Code

ngx_http_image_filter_module

配置:

​ 好像没啥好配置的??启用了该模块就可以直接使用了。

使用:

​ 有以下参数可供我们选择

image_filter off; 默认是on状态,使用这个参数来关闭。
image_filter test;
image_filter size; 
image_filter rotate 90 | 180 | 270;
image_filter resize width height;
image_filter crop width height;

​ 所以我实际使用是这样:

	location /img/ {
	# 对访问url 以 /img/ 开头的URL做下面操作
		set $imgsize 5000;
		# 设置一个默认的大小,为了保持不给定大小参数时候可用。
		if ($arg_w != ''){
			set $imgsize $arg_w;
			# 如果url中有w参数,则取该参数的值
		}
		image_filter resize $imgsize -;
		# 设置返回的大小,width 来自我们的url中的 w 参数。
	}
	# 此时我们访问 /img/avatar.jpg?w=512 就可以看到图片的宽度变成了512。由于我们给了一个5000的默认值,所以没有w参数的情况下也不会影响访问。

ngx_http_limit_conn_module

配置:

​ 好像没啥好配置的??启用了该模块就可以直接使用了。

使用:

​ 该模块可以通过对链接数限制从而实现对访客的访问进行限制。

​ 比如这样:(可以放到单虚拟主机的配置文件里,也可以放到具体location下)

limit_conn perserver 300; # 每秒允许有300个连接
limit_conn perip 25;# 每秒允许单IP有25个连接
limit_rate 512k;# 限速每秒512K

当然,可以根据自己需要调整上面的数值。如果达到了限制的话,会返回503页面。可以通过查询日志的方式来看自己站点的错误数量,如果正常访问时候错误很多的话,那证明需要提高阀值了。


暂时先写这些,有好用的以后再更新。

我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=18ilrdb9a4olt