Nginx高级

Nginx高级模块

ngx_http_rewrite_module

什么是rewrite

rewrite的主要功能是实现URL地址重写,需要PCRE软件的支持,通过PCRE兼容正则表达式语法进行匹配。

作用场景:

  • URL访问跳转,支持开发设计,如页面跳转,兼容性支持,展示效果等
  • SEO优化
  • 维护:后台维护、流量转发等
  • 安全

rewrite的语法

关于rewrite的语法可以查看官方文档,http://nginx.org/en/docs/http/ngx_http_rewrite_module.html,同时官网也提供了关于rewrite规则的一些用法,http://nginx.org/en/docs/http/converting_rewrite_rules.html。

实例

  1. 首先书写相关的配置文件
server {
    listen 80 default_server;
    server_name localhost;

    root /usr/share/nginx/html;
    location ~ ^/break {
        rewrite ^/break /rewrite_test/ break;
    }

    location ~ ^/last {
         rewrite ^/last /rewrite_test/ last;
    }

    location /rewrite_test/ {
       default_type application/json;
       return 200 '{"status":"success"}';
    }
}

接下里我们在/usr/share/nginx/html/目录下创建目录rewrite_test,并添加一个index.html文件,当我们访问http://193.112.69.164/break的时候,我们将会访问/usr/share/nginx/html/rewrite_test/index.html,此时页面将返回json内容。

{
    "status": "success"
}

当然访问last也是可以的。

官网有一些小例子,这里贴出来。

if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
}

if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
}

if ($request_method = POST) {
    return 405;
}

if ($slow) {
    limit_rate 10k;
}

if ($invalid_referer) {
    return 403;
}
#例如我们请求/category-水果-苹果-红富士.html时,我们将访问/category/水果/苹果/category_红富士.html
rewrite ^/category-(\d+)-(\d+)-(\d+)\.html$ /category/$1/$2/category_$3.html break;
# 如果当前的agent是google浏览器,将重定向/nginx到百度
if ($http_user_agent ~* Chrome) {
	rewrite ^/nginx http://www.baidu.com redirect;
} 
#如果请求的文件不存在。将重定向到百度去请求这个文件
if (!-f $request_filename) {
rewrite ^/(.*)$ http://www.baidu.com/$1 redirect;
}

4种flag的介绍

  • last 停止rewrite检测【如果没有匹配到,会继续向下匹配】

  • break 停止rewrite检测【如果没有匹配到,则不再向下匹配,直接返回结果404】

  • redirect 返回302临时重定向,地址栏会显示跳转后的地址

  • permanent 返回301永久重定向,地址栏会显示跳转后的地址

rewrite规则优先级

  • 执行server块中的rewrite指令
  • 执行location匹配
  • 执行选定的location中的rewrite指令

ngx_http_secure_link_module

官方文档地址:http://nginx.org/en/docs/http/ngx_http_secure_link_module.html

  • 指定并允许检查请求的链接的真实性以及保护资源免遭未经授权的访问
  • 限制链接生效周期

测试


    location / {
        secure_link $arg_md5,$arg_expires;
        secure_link_md5 "$secure_link_expires$uri admin";

        if ($secure_link = "") {
            return 403;
        }

        if ($secure_link = "0") {
            return 410;
        }
    }
#该sh用于生成相应的请求文件
servername="193.112.69.164" #请求的服务器
download_file="/download/User.java" #下载的文件路径
time_num=$(date -d "2020-1-1 00:00:00" +%s) #过期时间
secret_num="admin" #密钥,与配置文件相对应

res=$(echo -n "${time_num}${download_file} ${secret_num}"|openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)

echo "http://${servername}${download_file}?md5=${res}&expires=${time_num}"

当我们用浏览器访问上面脚本生成的链接时,我们就可以下载这个文件了,但是当文件过期,或者是参数路径不正确都时无法下载该文件的。

ngx_http_geoip_module

官方文档:http://nginx.org/en/docs/http/ngx_http_geoip_module.html

基于IP地址匹配MaxMind GeoIP二进制文件,读取IP所在地域信息

案例:公司多台服务器,国内访问国内的服务器,国外用户访问国外的服务器。

  • 区别国内外做HTTP访问规则
  • 区别国内城市地域做HTTP访问规则

需要注意的是,nginx默认是不安装这个模块的,需要手动安装这个模块。

测试使用的配置文件,如下。

geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
server {
    listen       80;
    server_name  localhost;
    #如果访问的ip不是中国的ip,那将返回403
    location / {
        if ($geoip_country_code != CN) {
            return 403;
        }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    #获取自己的ip
   location /myip {
        default_type text/plain;
        return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
   }
    error_page   500 502 503 504 404  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Nginx的安全控制

关于安全控制的部分可以查看nginx的官方文档:https://docs.nginx.com/nginx/admin-guide/security-controls/

关于HTTPS服务优化

  • 激活keepalive长连接
  • 设置SSL session缓存
 ##示例配置
 keepalive_timeout 100;
   ssl on;
   ssl_session_cache   shared:SSL:10m;
   ssl_session_timeout 10m;

Nginx结合Lua

Integrate Lua co‑routines into the NGINX event‑processing model.

Installation Instructions

  1. Install the Lua module.

    For Amazon Linux, CentOS, Oracle Linux, and RHEL:

    $ yum install nginx-plus-module-lua
    

    For Debian and Ubuntu:

    $ apt-get install nginx-plus-module-lua
    

    For SLES:

    $ zypper install nginx-plus-module-lua
    
  2. Put both of the following directives in the top-level (“main”) context of the main NGINX Plus configuration file, /etc/nginx/nginx.conf:

    load_module modules/ndk_http_module.so;
    load_module modules/ngx_http_lua_module.so;
    

    Note: The directives must be in this order.

  3. Perform additional configuration as required by the module.

  4. Reload NGINX Plus to enable the module:

    $ nginx -t && nginx -s reload
    

总结

  • rewrite
  • secure_link
  • geoip
  • 安全控制
  • Lua的使用

关于文档的来源,当前文档的大部分模块都是参考的nginx admin doc ,文档地址:https://docs.nginx.com/nginx/admin-guide/,

发布了55 篇原创文章 · 获赞 6 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/liver_life/article/details/99223797
今日推荐