nginx 跨域

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
    <br />
    <h2>a页面</h2>
    <br />
    <div id = "testcors"></div>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
//$("#testcors").text("Hello world!");
    $.ajax({  
         url:"http://cors.enjoy.com/allow_url",  
         async :false,  
         type:"GET",  
         success:function(data){  
             $("#testcors").html(data);  
             }  
     });
</script>

server {
        listen       80;
        server_name  static.enjoy.com;
    
        location / {
            root   html/static;
            index  index.html index.htm;
        }
    location ^~ /cors {
            alias   html/cors;
            index  cors.html;
        }
    location ~ /(.*)\.(html|js|css|jpg|jpeg|png|gif)$ {#覆盖/re/a.htm路径
        gzip on; # 启用gzip压缩,默认是off,不启用
        
        # 对js、css、jpg、png、gif格式的文件启用gzip压缩功能
        gzip_types application/javascript text/css image/jpeg image/png image/gif;
        gzip_min_length 1024; # 所压缩文件的最小值,小于这个的不会压缩
        gzip_buffers 4 1k; # 设置压缩响应的缓冲块的大小和个数,默认是内存一个页的大小
        gzip_comp_level 1; # 压缩水平,默认1。取值范围1-9,取值越大压缩比率越大,但越耗cpu时间
        
        root html/gzip;
    }
    location ^~ /qq.png {
    #    expires 2s;#缓存2秒
        expires 2m;#缓存2分钟
    #    expires 2h;#缓存2小时
    #    expires 2d;#缓存2天
        root html/gzip;
    }
    location ^~ /chrome.png {
                expires 2m;#缓存2分钟
                root html/gzip;
        }
    location ^~ /mall {
        valid_referers *.enjoy.com;
            if ($invalid_referer) {
                return 404;
            }
                root html/gzip;
        }

    }

upstream nginx {
        ip_hash;
                 server 172.17.0.4:8081 weight=2;
        server 172.17.0.5:8081 weight=1;
         }

server {
        listen       80;
        server_name  cors.enjoy.com;

    if ( $http_origin ~ http://(.*).enjoy.com){
                  
                 set $allow_url $http_origin;
                 
        }
         
       #是否允许请求带有验证信息
         add_header Access-Control-Allow-Credentials true;
         #允许跨域访问的域名,可以是一个域的列表,也可以是通配符*
         add_header Access-Control-Allow-Origin  $allow_url;
         #允许脚本访问的返回头
         add_header Access-Control-Allow-Headers 'x-requested-with,content-type,Cache-Control,Pragma,Date,x-timestamp';
         #允许使用的请求方法,以逗号隔开
         add_header Access-Control-Allow-Methods 'POST,GET,OPTIONS,PUT,DELETE';
         #允许自定义的头部,以逗号隔开,大小写不敏感
         add_header Access-Control-Expose-Headers 'WWW-Authenticate,Server-Authorization';
         #P3P支持跨域cookie操作
         add_header P3P 'policyref="/w3c/p3p.xml", CP="NOI DSP PSAa OUR BUS IND ONL UNI COM NAV INT LOC"';
    add_header test  1;

     if ($request_method = 'OPTIONS') {
             return 204;
         }

        location / {
            root   html/static/;
            index  index.html index.htm;
        }

    location /rout {
        rewrite ^/rout/(.*)  /static/$1.html break;
        root   html/;
            index  index.html index.htm;
        }
    location /proxy {
        echo "我是www.enjoy.com内容:$http_origin";
    #    proxy_pass http://172.17.0.4:8081/nginx;
        }
    location /nginx {
                proxy_pass http://nginx;
        }

       
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        location ^~ /cors {
            alias   html/cors;
            index  cors.html;
        }


       location ^~ /allow_url {
             echo '111';
        }
        
    }

访问

猜你喜欢

转载自blog.csdn.net/pf1234321/article/details/83095437