nginx反向代理解决CORS跨域问题

nginx反向代理解决CORS跨域问题的代码直接列出
需要的参考下面代码

       location ~* / { 
        proxy_pass https://www.cloudflare.com;
        proxy_redirect off; 
        proxy_ssl_name $proxy_host;
        proxy_ssl_server_name on;
        proxy_set_header Accept-Encoding "";  
        proxy_set_header Host $proxy_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;  
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        #CORS 跨域
          if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' '*';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
        if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' '*';
        }
        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' '*';
        }
        #跨域第二种
       #  add_header 'Access-Control-Allow-Methods' 'GET,OPTIONS,POST,PUT,DELETE' always;
       #  add_header 'Access-Control-Allow-Credentials' 'true' always;
       #  add_header 'Access-Control-Allow-Origin' '$http_origin' always;
       #  add_header Access-Control-Allow-Headers $http_access_control_request_headers;
       #  add_header Access-Control-Max-Age 3600;
        
        }

发表评论