码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx基础

时间:2020-02-10 21:04:14      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:映射   turn   pass   success   访问   防盗链   remote   实现   html   

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;

}

}

Nginx基础

标签:映射   turn   pass   success   访问   防盗链   remote   实现   html   

原文地址:https://www.cnblogs.com/1314Justin/p/12292683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!