缓存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

快快云:香港沙田CN2/美国Cera大宽带/日本CN2,三网直连CN2 GIA云服务器和独立服务器

快快云怎么样?快快云是一家成立于2021年的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务,快快云目前提供有香港云服务器、美国云服务器、日本云服务器、香港独立服务器、美国独立服务器,日本独立服务器。快快云专注为个人开发者用户,中小型,大型企业用户提供一站式核心网络云端服务部署,促使用户云端部署化简为零,轻松快捷运用云计算!多年云计算领域服务经验,遍布亚太地区的海量节点为业务推进提供强大...

Boomer.Host(年付3.5美)休斯敦便宜VPS

Boomer.Host是一家比较新的国外主机商,虽然LEB自述 we’re now more than 2 year old,商家提供虚拟主机和VPS,其中VPS主机基于OpenVZ架构,数据中心为美国得克萨斯州休斯敦。目前,商家在LET发了两款特别促销套餐,年付最低3.5美元起,特别提醒:低价低配,且必须年付,请务必自行斟酌确定需求再入手。下面列出几款促销套餐的配置信息。CPU:1core内存:...

Hostodo:4款便宜美国vps七折优惠低至$13/年;NVMe阵列1Gbps带宽,免费DirectAdmin授权

hostodo怎么样?快到了7月4日美国独立日,hostodo现在推出了VPS大促销活动,提供4款Hostodo美国独立日活动便宜VPS,相当于7折,低至$13/年,续费同价。Hostodo美国独立日活动结束时间不定,活动机售完即止。Hostodo商家支持加密数字货币、信用卡、PayPal、支付宝、银联等付款。Hostodo美国独立日活动VPS基于KVM虚拟,NVMe阵列,1Gbps带宽,自带一个...

防cc为你推荐
directional163legraphcss3圆角css实现圆角的几种方法是什么?netbios端口怎么关闭8909端口!其他端口就不用了ipad连不上wifi苹果ipad突然连不上网了,是怎么回事?网络是好的,手机能上网。ipadwifiipad的wifi打不开怎么办?x-router设置路由器是我的上网设置是x怎么弄win7关闭135端口win7系统 怎么关闭135 445 端口 修改注册表 创建IP安全策略 也试过 就是关不了 还望高手指教micromediawww.macromedia.com 是什么网站googleadsense如何通过Google adsense???
独立ip虚拟主机 enom photonvps fdcservers enzu 美国主机评论 海外服务器 qq数据库 炎黄盛世 gtt 怎么建立邮箱 腾讯总部在哪 web应用服务器 实惠 阿里云邮箱怎么注册 小夜博客 一句话木马 linuxvi命令 dns是什么意思 装修瓦工招聘 更多