发表于: 2017-04-28 18:29:56

1 1125


今天完成的事:1,配置好了nginx的反向代理

 location /carrots-admin-ajax/{

proxy_pass http://dev.admin.carrots.ptteng.com/;

#proxy_set_header Host $host;

#proxy_set_header X-Real-IP $remote_addr;

#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

nginx为了实现反向代理的需求而增加了一个ngx_http_proxy_module模块。其中proxy_set_header指令就是该模块需要读取的配置文件。在这里,所有设置的值的含义和http请求同中的含义完全相同,除了Host外还有X-Forward-For。

        Host的含义是表明请求的主机名,因为nginx作为反向代理使用,而如果后端真是的服务器设置有类似防盗链或者根据http请求头中的host字段来进行路由或判断功能的话,如果反向代理层的nginx不重写请求头中的host字段,将会导致请求失败【默认反向代理服务器会向后端真实服务器发送请求,并且请求头中的host字段应为proxy_pass指令设置的服务器】。

       同理,X_Forward_For字段表示该条http请求是有谁发起的?如果反向代理服务器不重写该请求头的话,那么后端真实服务器在处理时会认为所有的请求都来在反向代理服务器,如果后端有防攻击策略的话,那么机器就被封掉了。因此,在配置用作反向代理的nginx中一般会增加两条配置,修改http的请求头:

proxy_set_header Host $http_host;

proxy_set_header X-Forward-For $remote_addr;

这里的$http_host和$remote_addr都是nginx的导出变量,可以再配置文件中直接使用。如果Host请求头部没有出现在请求头中,则$http_host值为空,但是$host值为主域名。因此,一般而言,会用$host代替$http_host变量,从而避免http请求中丢失Host头部的情况下Host不被重写的失误。

                 2,用jquery发起异步请求,获得正确的返回值

//原生js
   //xhr = new XMLHttpRequest();
//xhr.onreadystatechange = function () {
//    if(xhr.readyState == 4) {
//       if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
//          console.log(xhr.responseText);
//          var aaa = JSON.parse(xhr.responseText);
//           if (aaa.message==="success"){
//               window.location.href="two.html"
//           }else {
//               $("#tips").text(aaa.message)
//           }
//       } else {
//          alert("Request was unsuccessful:"+xhr.status );
//        }
//    }
//};
//xhr.open("post","/carrots-admin-ajax/a/login",true);
//xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
//xhr.send("name="+userNum+"&pwd="+passWored);

              3,有jq发起异步请求

//$.ajax({
//    type: "POST",
//    url: "/carrots-admin-ajax/a/login",
//    data:{"name": userNum,"pwd": passWored},
//    dataType: "json",
//    success: function(data){
//        if (data.message==="success"){
//                           window.location.href="two.html"
//                       }else {
//                           $("#tips").text(data.message)
//                       }
//    }
//});

//two
$.post("/carrots-admin-ajax/a/login",{
name:userNum,
pwd:passWored
},
function(date,status){
if (date.message==="success"){
window.location.href="two.html"
                      }else {
$("#tips").text(date.message)
}
},
"json"
)

明天计划的事:1开始学习angular知识。

                       2,弄透任务5知识点


遇到的问题:用到的知识没串在一起,要梳理梳理


收获:明白了如何用jq发起异步请求,



返回列表 返回列表
评论

    分享到