nginx解决服务器宕机、解决跨域问题、配置防盗链、防止DDOS流量攻击

时间:2024-01-06 17:44:26

解决服务器宕机

配置nginx.cfg配置文件,在映射拦截地址中加入代理地址响应方案

location / {
proxy_connect_timeout 1;
proxy_send_timeout 1;
proxy_read_timeout 1;
proxy_pass http://backserver;
index index.html index.htm;
}

 

proxy_connect_timeout 1;  :连接超时1秒
proxy_send_timeout 1;   :请求超时1秒
proxy_read_timeout 1;   :读取超时1秒

解决跨域问题

server {
listen 80;
server_name www.wdksoft.com; #charset koi8-r; #access_log logs/host.access.log main; location /a {
#proxy_connect_timeout 1;
#proxy_send_timeout 1;
#proxy_read_timeout 1;
proxy_pass http://www.a.com:8080/a/;
index index.html index.htm; }
location /b {
#proxy_connect_timeout 1;
#proxy_send_timeout 1;
#proxy_read_timeout 1;
proxy_pass http://www.b.com:8081/b/;
index index.html index.htm;
}
}

  

将两个工程都配置进去通Nginx在进行反向代理,还可用配置Nginx表头的方法来实现

配置防盗链

server {
listen 80;
server_name fdl.wdksoft.com; #charset koi8-r; #access_log logs/host.access.log main;
#拦截所有关于jpg|jpeg|JPG|png|gif|icon格式的请求
location ~ .*\.(jpg|jpeg|JPG|png|gif|icon)$ {
#验证blocked来源地址不为空并且符合referers配置的地址
#none允许来源地址为空
valid_referers blocked http://fdl.wdksoft.com/a fdl.wdksoft.com/a;
#如果不符合则会return 403
if ($invalid_referer) {
rewrite ^/ http://www.a.com:8080/a/img/zysx.png;
#return 403;
}
}
location /a {
proxy_pass http://www.a.com:8080/a/;
index index.html index.htm; } }

  

防止DDOS流量攻击

server {
listen 80;
server_name ddos.wdksoft.com; location /a {
limit_conn addr 1; #同一时间内只能建立一次连接
limit_req zone=one;
proxy_pass http://www.a.com:8080/a/;
index index.html index.htm; } }

  

什么是DDOS流量攻击:

DDoS攻击分为两种:要么大数据,大流量来压垮网络设备和服务器,要么有意制造大量无法完成的不完全请求来快速耗尽服务器资源。有效防止DDoS攻击的关键困难是无法将攻击包从合法包中区分出来:IDS进行的典型“签名”模式匹配起不到有效的作用;许多攻击使用源IP地址欺骗来逃脱源识别,很难搜寻特定的攻击源头。