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

盘点AoYoZhuJi傲游主机商8个数据中心常见方案及八折优惠

傲游主机商我们可能很多人并不陌生,实际上这个商家早年也就是个人主机商,传说是有几个个人投资创办的,不过能坚持到现在也算不错,毕竟有早年的用户积累正常情况上还是能延续的。如果是新服务商这几年确实不是特别容易,问到几个老牌的个人服务商很多都是早年的用户积累客户群。傲游主机目前有提供XEN和KVM架构的云服务器,不少还是亚洲CN2优化节点,目前数据中心包括中国香港、韩国、德国、荷兰和美国等多个地区的CN...

易速互联月付299元,美国独立服务器促销,加州地区,BGP直连线路,10G防御

易速互联怎么样?易速互联是国人老牌主机商家,至今已经成立9年,商家销售虚拟主机、VPS及独立服务器,目前商家针对美国加州萨克拉门托RH数据中心进行促销,线路采用BGP直连线路,自带10G防御,美国加州地区,100M带宽不限流量,月付299元起,有需要美国不限流量独立服务器的朋友可以看看。点击进入:易速互联官方网站美国独立服务器优惠套餐:RH数据中心位于美国加州、配置丰富性价比高、10G DDOS免...

Spinservers:美国圣何塞机房少量补货/双E5/64GB DDR4/2TB SSD/10Gbps端口月流量10TB/$111/月

Chia矿机,Spinservers怎么样?Spinservers好不好,Spinservers大硬盘服务器。Spinservers刚刚在美国圣何塞机房补货120台独立服务器,CPU都是双E5系列,64-512GB DDR4内存,超大SSD或NVMe存储,数量有限,机器都是预部署好的,下单即可上架,无需人工干预,有需要的朋友抓紧下单哦。Spinservers是Majestic Hosting So...

防cc为你推荐
处理器flash导致卡巴斯基之路android2011年停止接单产品支持ipad支持ipad支持ipad三星iphonecolumnios5eaccelerator使用apmsevr中eAccelerator显示NO是什么问题
传奇服务器租用 深圳主机租用 德国vps 域名服务器上存放着internet主机的 便宜服务器 cpanel主机 鲜果阅读 web服务器的架设 php空间购买 福建铁通 免费phpmysql空间 绍兴电信 怎么建立邮箱 江苏双线服务器 服务器是干什么用的 电信网络测速器 qq金券 cdn服务 accountsuspended windowssever2008 更多