Category - Nginx相关

2024-06-20 14:04:42    46    0    0

跨域配置
单域名跨域

  1. # 允许所有来源的跨域请求
  2. add_header 'Access-Control-Allow-Origin' 'https://test.test.com';
  3. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  4. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,token,Authorization';
  5. # 设置预检请求的有效期
  6. add_header 'Access-Control-Max-Age' 1728000;
  7. # 允许携带身份凭证(例如,cookie)
  8. add_header 'Access-Control-Allow-Credentials' 'true';
  9. # 处理预检请求(OPTIONS请求)
  10. if ($request_method = 'OPTIONS') {
  11. return 204;
  12. }

多域名跨域

  1. # 在http块中定义一个map,将请求的Origin映射到允许的Origin
  2. map $http_origin $allowed_origin {
  3. "~^(https://test1.test.com|https://test2.test.com|https://test3.test.com)$" $http_origin;
  4. default "";
  5. }
  6. server {
  7. listen 80;
  8. listen 443 ssl http2;
  9. server_name test.test.com;
  10. index index.html index.htm index.php;
  11. charset utf-8;
  12. error_log /var/log/nginx/test.test.com-error.log error
Footer