缓存nginx针对静态页面防CC攻击

防cc  时间:2021-05-01  阅读:()

nginx 针对静态页 面防CC攻 击

一、架构

Nginx介绍

Nginx是一款由俄罗斯程序设计师Igor Sysoe v所开发轻量级的网页服务器、反向代理服务器以及电子邮件IMAP/POP3代理服务器。

Nginx的特点

Nginx相较于Ap ache、l ighttpd具有占有内存少稳定性高等优势并且依靠并发能力强丰富的模块库以及友好灵活的配置而闻名。在Linu x操作系统下 nginx使用epo ll事件模型得益于此 nginx在Linux操作系统下效率相当高。

Nginx的反向代理功能nginx proxy是ngin x的重要功能利用proxy基本可以实现一个完整的7层负载均衡它有这些特色

1、功能强大性能超强工作稳定。

2、后端转向与业务配置分离非常灵活。

3、可以指定任意IP和端口进行配置与网络环境是不相干的。

4、可以针对后端返回的情况判断不正常则重新请求另一台主机并自动剔除不正常的主机。

5、可以分配权重并且分配均匀。

6、可以实现多种分配策略。

7、上传文件使用异步处理方式 nginx会先将文件接收下来然后再转发到后端这样可以减少后端服务器很多连接。

根据Ngi nx的特点与反向代理的功能使之成为静态页面防CC攻击绝佳选择。根据我们以往的经验一台Q9300、 4GB内存的主机可以轻松防住7万链接的CC攻击此时服务器资源占用仍然相当低。

Nginx防CC攻击的架构

二、服务器配置

服务器环境CentOS 6.3 X64

注 以下文档蓝色为配置文件的内容红色为运行的命令

1、安装系统、编译环境等

最小化安装CentOS。因为安装后我们要升级系统补丁再安装必要组件所以没必要安装其他组件。

安装完后配置iptables打开80端口或者直接关闭ipta bles。

用yum update-y升级系统的组件

安装编译Nginx必要的组件yum install -y gcc make sendmail pcre pcre-devel openssl openssl-devel nano screen lrzszwget curl curl-devel sendmail mloca te openssh-clients man patch

添加www用户组和用户并且指定www用户的shell为/sbin/nolog ingroupadd www&&useradd -g www-s/sbin/nologin www

创建相关的目录mkdir -p/var/log/nginx/var/proxy_temp_dir/var/proxy_cache_dir

#to u c h/var/log/ng inx/test.co m. log

2、编译n g in x

下载nginxcd~wget http://nginx.org/download/nginx-1.2.7.tar.gztar zxf nginx-1.2.7.tar.gz

下载ngx_cache_purgew"get --no-c hec k-certificat e

"https://github.com/FRiCKLE/n"gx_cache_purge/archive/master.zip" -Ongx_cache_purge-master.zipunzip-x ngx_cache_purge-master.zip

下载ng inx-u pstream-jv m-routew"gethttp://ng inx-u pstream-jv m-route.goog lecode.com/files/ng inx-u pstream-jv m-route-0.1.tar.gz" -O"nginx-upstream-jvm-route-0.1.tar.gz"tar zxf nginx-upstream-jvm-route-0.1.tar.gz

进入nginx源代码目录cd nginx-1.2.7

打补丁patch-p0< . ./nginx_upstream_jvm_route/jvm_route.patch

编译

./configure--user=www--group=www--add-module=. ./nginx_upstream_jvm_route--add-module=. ./ngx_cache_purge-master --prefix=/usr/local/nginx

--with-http_stu b_statu s_modu le--w ith-http_ssl_m od u lemake&&make insta l l

3、配置n g in x

进入nginx的配置目录cd/usr/local/nginx/conf/vi nginx.confnginx.conf的配置user www www;wo rker_processes 8;p id/u sr/loca l/n g inx/log s/n g inx.p id;worker_rli mit_nofile 65535;ev e n ts {use epoll;worker_connections 65535;

}http{include mime.types;defau lt_type application/octet-strea m;server_names_hash_bucket_size 128;client_header_buffer_size 32k;large_clie nt_header_buffers 432k;client_max_body_size 8m;log_format access_"log'$remote_"ad'd'"r -$remote_user ["$time_local] "$request" '' '$status$body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for ;sendfi le on;tcp_nopush on;keepalive_timeout 60;tcp_nodela y on;fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 464k;fastcgi_busy_buffers_size 128k;

fastcgi_temp_fi le_write_size 128k;

##cache_start##proxy_connect_timeout 5;proxy_read_timeout 60;proxy_send_timeout 5;proxy_buffer_size 16k;proxy_buffers 464k;proxy_busy_buffers_size 128k;proxy_temp_file_writ e_size 128k;

#注 proxy_temp_path和proxy_cache_path指定的路径必须在同一分区proxy_temp_path/var/proxy_temp_dir;

#设置Web缓存区名称为cache_one 内存缓存空间大小为200MB 1天清理一次缓存硬盘缓存空间大小为1GB。proxy_cache_path/var/proxy_cache_dir levels=1:2 keys_zone=cache_one:200minactive=1d max_size=1g;

##cache_end##gzip on;gzip_min_length 1k;gzip_buffers 416k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types text/plain application/x-javascript text/css application/xml;gzip_vary on;include upstream.conf;include vhostconf/*.conf;

}vi upstream.conf

增加后端服务器时需要在此添加后端服务器。添加或修改的主要内容是上游服务器的访问地址 以及指定u pstream名称。 upstream www.test.com {server 192.168.1.102:80 weight=1 max_fails=2 fail_timeo ut=30s;server 192.168.1.103:80 weight=1 max_fai ls=2 fail_timeo ut=30s;server 192.168.1.105:80 weight=1 max_fails=2 fail_timeo ut=30s;jvm_route$cookie_JSESSIONID reverse;

}

创建v hostco nf目录mkdir vhostconf

配置相应的主机

增加后端服务器时需要在此添加后端服务器。主要修改的有绑定域名、 proxy_pass对应u p st rea m.co n f的地址vi vhostconf/test.com.confse rv e r{liste n 80;server_name www.test.com;lo c at io n/{

#root/var/www/test.com;

#index index.jsp index.htm index.html;

#如果后端的服务器返回502、 504、执行超时等错误 自动将请求转发到u p st rea m负载均衡池中的另一台服务器实现故障转移。proxy_next_upstream http_502 http_504 error timeout inval id_header;proxy_cache cache_one;

#对不同的HTTP状态码设置不同的缓存时间proxy_cache_val id 200302 1h;proxy_cache_val id 301 1d;proxy_cache_val id any 1m;

#以域名、 URI、参数组合成Web缓存的Key值 Nginx根据Key值哈希存储缓存内容到二级缓存目录内proxy_cac he_key$host$u ri$is_args$args;proxy_redirect off;proxy_set_heade r Host$host;proxy_set_header X-Real-IP$remote_add r;proxy_set_header X-Fo rwarded-For$proxy_add_x_fo rwarded_fo r;proxy_pass http://www.test.com;expires 1d;

}

#用于清除缓存假设一个U RL为http://192.168.8.42/test.txt通过访问http://192.168.8.42/pu rge/test.txt就可以清除该URL的缓存。lo c at io n~/p u rg e(/.*)

{

#设置只允许指定的IP或IP段才可以清除U RL缓存。allow 127.0.0.1;allow 192.168.0.0/16;deny al l;proxy_cache_purge cache_one$host$1$is_args$args;

}

#扩展名以.php、 .jsp、 .cgi结尾的动态应用程序不缓存。

location~.*\.(php|jsp|cgi |asp|aspx)?$

{proxy_set_heade r Host$host;proxy_set_header X-Forwarded-For$remote_add r;proxy_pass http://www.test.com;

}locat io n/ng inx_statu s{stub_status on;access_log off;allow 183.63.215.39;deny al l;

}access_log/var/log/ng inx/test.co m. log access_log;

}

测试配置文件是否正确

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

4、配置n g in x自启动

创建nginx 自启动脚本vi/etc/init.d/nginx

#!/bin/bash

#nginx Startup script for the Nginx HTTP Server

#it is v.0.0.2 version.

#c h kc on fig: -85 15

#description:Nginx is a high-performance web and proxy server.# It has a lot of featu res, but it's not for everyone.

#processname: nginx

#pidfi le: /var/run/nginx.p id

#config: /usr/local/nginx/conf/nginx.confnginxd=/usr/local/nginx/sbin/nginxnginx_config=/usr/local/nginx/conf/nginx.confnginx_pid=/var/run/nginx.pid

RETVAL=0prog="nginx"

#Source function libra ry.

./etc/rc.d/in it.d/fu nctions

#Source networking configuration.

. /etc/sy sc on fig/n etwo rk

#Check that networkin"g is" up.

[${NETWORKING}= no ]&&exit 0

[ -x$nginxd] | | exit 0

#Start nginx daemons functions.st a rt(){if [ -e$n"ginx_pid];thenecho nginx already running. . . ."exit 1fiecho-n$"Starting$prog: "daemon$nginxd-c${nginx_config}

RETVAL=$?ec h o

[$RETVAL=0]&&touch/var/lock/subsys/nginxretu rn$RETVAL

}

#Stop nginx daemons functions.sto p(){echo-n$"Stopping$prog: "ki llproc$nginxd

RETVAL=$?ec h o

[$RETVAL=0]&&rm-f/var/lock/subsys/nginx/var/run/nginx.pid}

#reload nginx service functions.reloa d(){ec ho-n$"R`eloa d ing$prog: "`

#kill -HUP cat${nginx_pid}ki l lproc$nginxd-HUP

RETVAL=$?ec h o

}

#See how we were calle d.case"$1" inst a rt)st a rt

;;sto p)sto p

;;reloa d)reloa d

;;resta rt)

sto pst a rt

;;status)sta tus$prog

RETVAL=$?

;;

*)ec ho$"Usag e: $p rog{start|sto p|resta rt|reloa d|statu s|he lp}"exit 1esacex it$RETVAL

加上x权限chmod+x/etc/init.d/nginx

加上自启动echo/etc/in it.d/ng inx start>>/etc/rc. local

启动nginx

/etc/init.d/nginx start

CheapWindowsVPS$4.5/月,美国VPS/免费Windows系统/1Gbps不限流量/,可选美洲、欧洲、亚洲等8大机房

国外商家提供Windows系统的并不常见,CheapWindowsVPS 此次提供的 2 款 VPS 促销套餐,提供 5 折永久优惠码,优惠后月付 4.5 美元起,价格还是挺诱人的,VPS 不限流量,接入 1Gbps 带宽,8 个机房皆可选,其中洛杉矶机房还提供亚洲优化网络供选择,操作系统有 Windows 10 专业版、2012 R2、2016、Linux等。Cheap Windows VPS是...

香港 1核1G 29元/月 美国1核 2G 36元/月 快云科技

快云科技: 11.11钜惠 美国云机2H5G年付148仅有40台,云服务器全场7折,香港云服务器年付388仅不到五折 公司介绍:快云科技是成立于2020年的新进主机商,持有IDC/ICP/ISP等证件资质齐全主营产品有:香港弹性云服务器,美国vps和日本vps,香港物理机,国内高防物理机以及美国日本高防物理机官网地址:www.345idc.com活动截止日期为2021年11月13日此次促销活动提供...

湖北50G防御物理服务器( 199元/月 ),国内便宜的高防服务器

4324云是成立于2012年的老牌商家,主要经营国内服务器资源,是目前国内实力很强的商家,从价格上就可以看出来商家实力,这次商家给大家带来了全网最便宜的物理服务器。只能说用叹为观止形容。官网地址 点击进入由于是活动套餐 本款产品需要联系QQ客服 购买 QQ 800083597 QQ 2772347271CPU内存硬盘带宽IP防御价格e5 2630 12核16GBSSD 500GB​30M​1个IP...

防cc为你推荐
participants37支持ipad尺寸(mm)操作區域手控iexplore.exe应用程序错误iexplore.exe---应用程序错误.是什么意思?联通版iphone4s苹果4s怎么分移动版联通版电信版?重庆电信宽带管家如何才能以正确的流程在重庆电信安装上宽带迅雷下载速度迅雷下载速度真是太慢了重庆电信测速重庆电信对BT开始限制了?win7还原系统电脑怎么恢复出厂设置win7旗舰版chrome18chrome的加速功能怎么开启呢?
动态ip的vps 如何查询域名备案号 新网域名解析 新网域名管理 ipage lnmp 阿里云搜索 google镜像 yardvps 国内永久免费云服务器 12u机柜尺寸 tk域名 免费ddos防火墙 智能骨干网 免费个人空间 工信部icp备案号 重庆双线服务器托管 华为云服务登录 国内域名 lamp兄弟连 更多