美文网首页
Nginx TP5.1请求跨域解决

Nginx TP5.1请求跨域解决

作者: haokeed | 来源:发表于2019-06-25 10:40 被阅读0次

    覆盖原来的tp伪静态配置

    location / {
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header "Access-Control-Allow-Credentials" "true";
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE'; # 特别注意不能有OPTIONS在里面
            add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 200;
        }
        if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header "Access-Control-Allow-Credentials" "true";
            add_header 'Access-Control-Allow-Methods' 'GET, POST,DELET,PUT,OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'Authorization,Keep-Alive,User-Agent,X-Requested-With,Content-Type';
         }
        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header "Access-Control-Allow-Credentials" "true";
            add_header 'Access-Control-Allow-Methods' 'GET, POST,DELET,PUT,OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'Authorization,Keep-Alive,User-Agent,X-Requested-With,Content-Type';
         }
        if ($request_method = 'DELETE') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header "Access-Control-Allow-Credentials" "true";
            add_header 'Access-Control-Allow-Methods' 'GET, POST,DELET,PUT,OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'Authorization,Keep-Alive,User-Agent,X-Requested-With,Content-Type';
         }
        if ($request_method = 'put') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header "Access-Control-Allow-Credentials" "true";
            add_header 'Access-Control-Allow-Methods' 'GET, POST,DELET,PUT,OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'Authorization,Keep-Alive,User-Agent,X-Requested-With,Content-Type';
         }
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=/$1  last;
        }
    
        index  index.php;
        autoindex  off;
    }
    

    相关文章

      网友评论

          本文标题:Nginx TP5.1请求跨域解决

          本文链接:https://www.haomeiwen.com/subject/rqttcctx.html