Nginx的模块http_secure_link_module

此模块是nginx的第三方模块,在编译的时候直接添加就可以了,此模块可以做防盗链

/usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.13.9

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --add-module=/root/nginx-1.13.9/nginx-http-concat --add-module=/root/nginx-1.13.9/naxsi-0.56rc1/naxsi_src --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_image_filter_module --with-http_slice_module --with-mail --with-threads --with-file-aio --with-stream --with-mail_ssl_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-pcre --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module

编译进去以后把旧的nginx备份一个,把新编译的nginx放到/usr/local/nginx/sbin/下面

在conf的nginx.conf配置文件里编辑

在server里添加下面配置文件

#防盗链
location /index.html/s/ {
secure_link_secret MySecret;
if ($secure_link = "") {
return 403;
}
rewrite ^ /人生苦短,我用Python/$secure_link;
}
location /index.html/a/ {
secure_link_secret MySecret;
if ($secure_link = "") {
return 403;
}
rewrite ^ /人生苦短,我用Python/$secure_link;
}

保存退出

检查nginx配置有无问题

/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

基本没有问题 重启

/usr/local/nginx/sbin/nginx -s reload

此时检查防盗链是否生效

访问的时候后面加上/index.html/a/,页面返回403。

防盗链成功,我这里讲的不是很详细,网上很多资料,有和php结合的 有和java结合的,总觉得有错误,等有时间再总结,今天先写到这里。

点一杯星巴克

原文地址:https://www.cnblogs.com/qingyuanyuanxi/p/8558971.html