Nginx TP5.1请求跨域解决

2019-06-25  本文已影响0人  haokeed

覆盖原来的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;
}
上一篇下一篇

猜你喜欢

热点阅读