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的模块
[email protected]:/# 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
配置:
使用:
如果不出意外的话,我们已经可以使用该模块了。根据上面配置的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;
}
当然也不仅限于此。
配置:
好像没啥好配置的??启用了该模块就可以直接使用了。
使用:
有以下参数可供我们选择
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参数的情况下也不会影响访问。
配置:
好像没啥好配置的??启用了该模块就可以直接使用了。
使用:
该模块可以通过对链接数限制从而实现对访客的访问进行限制。
比如这样:(可以放到单虚拟主机的配置文件里,也可以放到具体location下)
limit_conn perserver 300; # 每秒允许有300个连接
limit_conn perip 25;# 每秒允许单IP有25个连接
limit_rate 512k;# 限速每秒512K
当然,可以根据自己需要调整上面的数值。如果达到了限制的话,会返回503页面。可以通过查询日志的方式来看自己站点的错误数量,如果正常访问时候错误很多的话,那证明需要提高阀值了。