nginx 跨域設置


 

 

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  www.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;
        }

        
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM