zoukankan      html  css  js  c++  java
  • Nginx详解十二:Nginx场景实践篇之跨站访问相关

    跨站访问

    浏览器请求一个页面的时候,发送了两个域名的请求

    此情况不安全,容易出现CSRF攻击,所以浏览器禁止跨域访问

     

    Nginx设置打开跨站访问

    配置语法:add_header name value [always];
    默认状态:-
    配置方法:http、server、location、if in location

    准备一个html:

    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title>测试ajax和跨域访问</title>
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
    </head>
    <script type="text/javascript">
    $(document).ready(function(){
    $.ajax({
    type: "GET",
    url: "http://jeson.imoocc.com/1.html",
    success: function(data) {
    alert("sucess!!!");
    },
    error: function() {
    alert("fail!!!,请刷新再试!");
    }
    });
    });
    </script>
    <body>
    <h1>测试跨域访问</h1>
    </body>
    </html>

    先上传html

    把之前的location改一下

    location ~ .*.(htm|html)$ {
    #add_header Access-Control-Allow-Orgin http://www.jesonc.com;
    #add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
    root /opt/app/code;
    }

     

     检查配置并重启

    访问

    把注释打开,意思是允许“http://www.jesonc.com”这个域名进行跨域访问,如果这里配置的是“ * ”,则代表允许所有域名进行跨域访问

    检查语法与重载

     

    访问

  • 相关阅读:
    andorid(3) 使用sqllite进行数据持久化
    android(1)--hello world中的layout与 onCreate()
    android(2)--listView
    linux 常用指令
    关于虚拟内存、驻留内存与共享内存——virt res shr之间的关系
    base64 和 md5
    python阿里云短信服务
    python邮件发送
    算法
    python3 字典
  • 原文地址:https://www.cnblogs.com/zhongyehai/p/10507770.html
Copyright © 2011-2022 走看看