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

青云互联19元/月,美国洛杉矶CN2GIA/香港安畅CN2云服务器低至;日本云主机

青云互联怎么样?青云互联美国洛杉矶cn2GIA云服务器低至19元/月起;香港安畅cn2云服务器低至19元/月起;日本cn2云主机低至35元/月起!青云互联是一家成立于2020年的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务。青云互联本站之前已经更新过很多相关文章介绍了,青云互联的机房有香港和洛杉矶,都有CN2 GIA线路、洛杉矶带高防,商家承诺试用7天,打死全额退款点击进入:青云互联...

轻云互联-618钜惠秒杀,香港CN2大宽带KVM架构云服务器月付22元,美国圣何塞精品云月付19元爆款!海量产品好货超值促销进行中!

官方网站:点击访问青云互联活动官网优惠码:终身88折扣优惠码:WN789-2021香港测试IP:154.196.254美国测试IP:243.164.1活动方案:用户购买任意全区域云服务器月付以上享受免费更换IP服务;限美国区域云服务器凡是购买均可以提交工单定制天机防火墙高防御保护端口以及保护模式;香港区域购买季度、半年付、年付周期均可免费申请额外1IP;使用优惠码购买后续费周期终身同活动价,价格不...

GeorgeDatacenter:洛杉矶/达拉斯/芝加哥/纽约vps云服务器;2核/8GB/250GB/2TB流量/1Gbps端口,$84/年

georgedatacenter怎么样?GeorgeDatacenter是一家2017年成立的美国商家,正规注册公司(REG: 10327625611),其实是oneman。现在有优惠,有几款特价VPS,基于Vmware。支持Paypal付款。GeorgeDatacenter目前推出的一款美国vps,2核/8GB内存/250GB NVMe空间/2TB流量/1Gbps端口/Vmware/洛杉矶/达拉...

防cc为你推荐
practicescss计划ipadiphone连不上wifi我的苹果手机连不上无线,其它手机能,怎么回事?只是家里的连不上google图片搜索如何使用google图片搜索引擎?ipad无法加入网络为什么ipad无法加入网络?morphvoxpro教程哪位大佬可以帮忙调一下morphvox pro的女声啊,我调了一天还是没调明白div居中div如何居中应用程序安卓4曲目ios6Spacefusioncharts
联通c套餐 堪萨斯服务器 fastdomain inmotionhosting gomezpeer 远程登陆工具 dropbox网盘 howfile 老左正传 免费吧 isp服务商 免费测手机号 腾讯总部在哪 登陆空间 论坛主机 购买空间 97rb mteam 上海联通 沈阳idc 更多