海运的博客

Nginx安装配置防盗链模块nginx-accesskey

发布时间:April 13, 2012 // 分类:Nginx // No Comments

之前有介绍通过Nginx通过referer防盗链,此方法设置简单,不过referer信息可以伪造,不能达到完全防盗链的目的。
为追求完美可以使用nginx-accesskey模块防盗链,nginx-accesskey会根据不同的请求IP生成不同的key,拒绝无效的请求,经测试可防迅雷盗链。
安装需重新编译Nginx增加nginx-accesskey模块,Lnmp请参考Lnmp平滑升级及修改Nginx版本号
下载Nginx和nginx-accesskey

wget http://nginx.org/download/nginx-1.0.15.tar.gz
tar zxvf nginx-1.0.15.tar.gz 
cd nginx-1.0.15
wget http://wiki.nginx.org/images/5/51/Nginx-accesskey-2.0.3.tar.gz
tar zxvf Nginx-accesskey-2.0.3.tar.gz 

修改Nginx-accesskey配置文件:

vim nginx-accesskey-2.0.3/config 
#修改$HTTP_ACCESSKEY_MODULE为ngx_http_accesskey_module
USE_MD5=YES
USE_SHA1=YES
ngx_addon_name=ngx_http_accesskey_module
HTTP_MODULES="$HTTP_MODULES ngx_http_accesskey_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_accesskey_module.c"

编译安装Nginx:

 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module \
--with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --add-module=./nginx-accesskey-2.0.3/
make 
make install

编辑Nginx配置文件,添加:

#location / {       #限制相应目录
location ~ .*\.(7z|iso|msi|tar|zip|rar|gz|exe)$ {        #或限制相应文件
accesskey             on;
accesskey_hashmethod  md5;
accesskey_arg         "key";
accesskey_signature   "pass$remote_addr";      #pass为干扰码,可自定义修改
}

Nginx防盗链应用测试:

<?
$ipkey= md5("pass".$_SERVER['REMOTE_ADDR']);
echo "<a href=file.rar>file</a><br />";
echo "<a href=file.rar?key=".$ipkey.">file.rar?key=".$ipkey.">file</a><br />";
?>

Lnmp平滑升级及修改Nginx版本号

发布时间:April 13, 2012 // 分类:Nginx // No Comments

首先下载官方stable稳定版,当前最新stable版是1.0.15。

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.0.15.tar.gz
tar zxvf nginx-1.0.15.tar.gz
cd nginx-1.0.15

修改源码,更改nginx版本信息。

vim src/core/nginx.h
#define NGINX_VERSION      "1.0.15" #版本号可自定义修改
#define NGINX_VER          "nginx/" NGINX_VERSION #nginx可自定义修改

例如修改为:

vim src/core/nginx.h
#define NGINX_VERSION      "1.0.0" 
#define NGINX_VER          "ows/" NGINX_VERSION 

修改:

vim src/http/ngx_http_header_filter_module.c
static char ngx_http_server_string[] = "Server: nginx" CRLF;

修改:

vim src/http/ngx_http_special_response.c
"<hr><center>nginx</center>" CRLF  

优化编译,去除DEBUG。

vim auto/cc/gcc
# debug
CFLAGS="$CFLAGS -g" #注释此行或删除

升级编译最好以原来编译的参数编译,查看原先编译的参数:

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

编译安装:

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6
make #make后先不要make install
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old #备份旧程序
cp objs/nginx /usr/local/nginx/sbin/nginx #复制新程序至程序目录
/usr/local/nginx/sbin/nginx -t #测试是否有问题
make upgrade  #完成升级
/usr/local/nginx/sbin/nginx -v #查看升级后的版本号

Nginx设置目录密码认证/限制特定IP访问

发布时间:April 8, 2012 // 分类:Nginx,网络安全 // No Comments

网络安全无处不在,使用Nginx为您的网站后台加把锁吧。
验证界面如:
2012-04-08_071422.png
1.Nginx兼容于Apache下htpasswd生成的密钥,不过Nginx下无此工具,可以使用crypt生成密码。

perl -e 'print crypt($ARGV[0], "pwdsalt")' password ;echo ""#password请更改为您的密码

用php生成sha密码,不建议:

$user = 'test';
$pass = 'test';
$sha1 = base64_encode(sha1($pass, true));
echo $user.':{SHA}'.$sha1;

使用openssl生成sha512crypt密码,建议使用。

echo 123456|openssl passwd -6 -in - 

结果即加密后的密码,新建密钥配置谁的将用户密码以以下方式添加。

user:passwd

编辑Nginx配置文件

location ^~ /admin/ {        #特定目录
location ~ .*\.(php|php5)?$ {
allow 192.168.1.16; #允许IP
deny all; #拒绝所有
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
auth_basic "Login";
auth_basic_user_file /passwdfile;
}

添加ip白名单跳过验证:

satisfy any;
allow 127.0.0.1;
deny all;
auth_basic "Login";
auth_basic_user_file /passwdfile;

Nginx图片/文件防盗链配置

发布时间:April 7, 2012 // 分类:Nginx // No Comments

Nginx可根据浏览器请求的Referer防盗链,限制非本站的页面链接请求,更严格的限制可使用防盗链模块nginx-accesskey,可防止迅雷盗链。
编辑Nginx配置文件,在Server段加入以下内容:

location ~* \.(jpg|gif|png)$ {      #限制的文件类型                                                                                                
valid_referers none blocked *.www.haiyun.me www.haiyun.me; #定义允许的域名
if ($invalid_referer) {   #如果请求非允许
#rewrite ^/ https://www.haiyun.me/error.png; #定向到指定图片
       return 403; #返回403
    }
  }

Nginx限制IP请求数防CC攻击

发布时间:April 6, 2012 // 分类:Nginx,网络安全 // No Comments

适用于Nginx服务器,如需通过自带防火墙限制请参考iptables防CC
编辑Nginx配置文件在http段添加:

limit_req_zone $binary_remote_addr zone=req:10m rate=2r/s;
#定义会话存储区,名字为reg,每秒2个请求

server添加:

location /path/ #限制的目录
{
  limit_req_zone zone=reg burst=2; #突发最高每秒2个请求数
}
分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...