Nginx解决防盗链,服务器宕机,跨域,防DDOS

1.Nginx解决服务器宕机问题,Nginx配置服务器宕机策略,如果服务器宕机,会找下一台机器进行访问
  配置nginx.cfg配置文件,在映射拦截地址中加入代理地址响应方案

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

2.解决网站跨域问题
  Nginx解决跨域问题,实现方案:
    www.a.com:8080/a
    www.b.com:8081/b
  如果在b工程的页面直接发送ajax请求a时会发生跨域问题,那么解决方案为:将A和B同时代理到Nginx,由Nginx做请求路由,直接在B工程页面中直接访问Nginx即可

复制代码
复制代码
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;
                }
            }
复制代码
复制代码

B页面请求:

复制代码
复制代码
$("#button").click(function () {
                    $.ajax({
                        url:"http://www.wdksoft.com/a/AServlet?username="+$("#username").val(),
                        type:"GET",
                        success:function (result) {
                            alert(result);
                        }
                    })
                });
复制代码
复制代码

3.Nginx配置防盗链
  利用Nginx进行来源地址拦截,只要来源地址符合原资源地址,则可以访问,否则返回4.3状态码

复制代码
复制代码
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;
                    
                }
                
            }
复制代码
复制代码

4.Nginx防止DDOS流量攻击
  DDOS流量攻击:频繁的发送请求,造成宽带占用,其他客户端无法访问
  Nginx解决DDOS流量攻击,利用limit_req_zone限制请求次数 limit_conn_zone限制连接次数

复制代码
复制代码
#限制IP的每秒请求次数
        limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
        #限制同一个IP同一时间内创建连接次数
        limit_conn_zone $binary_remote_addr zone=addr:10m;
        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;
                
            }
            
        }
原文地址:https://www.cnblogs.com/mayuan01/p/12382480.html