Nginx Apache CORS OPTIONS预检请求配置

2024-09-30 355 0

nginx

 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,PUT,POST,DELETE,OPTIONS';
         add_header 'Access-Control-Allow-Headers' 'Token,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,X_Requested_With,If-Modified-Since,Cache-Control,Content-Type';
         add_header 'Access-Control-Max-Age' 1728000;
         add_header 'Content-Type' 'text/plain charset=UTF-8';
         return 200;
     }
      try_files $uri $uri/ /index.php?$query_string;
   }

apache

<VirtualHost *:80>
    DocumentRoot "D:\XWX\static.demo.sundayhk.com/public"
    ServerName static.zsp.yyq.com

    <Directory "D:\XWX\static.demo.sundayhk.com/">
        AllowOverride all
        Require all granted
        Header always set Access-Control-Allow-Origin "*"
    </Directory>
    <IfModule mod_headers.c>
        <If "%{REQUEST_METHOD} == 'OPTIONS'">

            Header always set Access-Control-Allow-Origin "*"
        Header always set Access-Control-Allow-Methods "GET,PUT,POST,DELETE,OPTIONS"
            Header always set Access-Control-Allow-Headers "Token,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,X_Requested_With,If-Modified-Since,Cache-Control,Content-Type"
            Header always set Access-Control-Max-Age 1728000
            Header always set Content-Type "text/plain charset=UTF-8"
            RewriteEngine On
            RewriteRule .* - [R=200,L]
        </If>
    </IfModule>
</VirtualHost>

相关文章

MacBook系统升级到Sequoia15.1 SSH密钥无权限解决
haproxy 负载rabbitmq集群 报client unexpectedly closed TCP connection
windows 2012 命令行批量修改文件权限 删除
CICD持续部署 Jenkins 部署
CICD持续集成 SonarQube 代码检测部署
win11 Microsoft Store 微软账户无法登陆 0x80190001 解决

发布评论