您的位置 首页 php

Nginx跨域配置

目录

  • 概述
  • nginx 跨域配置
  • php7进阶到架构师相关阅读

概述

这是关于php进阶到架构之 Nginx进阶 学习的第 篇文章:Nginx跨域配置

  • 第一篇:
  • 第二篇:
  • 第三篇:
  • 第四篇:Nginx跨域配置

Nginx 跨域配置

使用场景:

网站A通过jqeury请求网站B的 api接口 时,会提示跨域问题,这时候有2种跨域处理。

一是: 服务器端设置跨域问题 ,比如后端是php开发

 header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, X-Token");
header("Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH");
header("Access-Control-Allow-Credentials: true");  

二是: 在nginx设置跨域问题配置 。即本文重点介绍的方法

在nginx.conf文件http里配置

     add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Credentials true;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
    add_header Access-Control-Allow-Headers DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since, Cache-Control ,Content-Type;  

2、 在(有多个项目配置的)具体的项目配置里location /里配置

 if ($request_method = 'OPTIONS') {
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
    add_header Access-Control-Allow-Credentials true;
    add_header Access-Control-Allow-Headers DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type;
    return 204;
}  

最后,欢迎大家留言补充,讨论~~~

文章来源:智云一二三科技

文章标题:Nginx跨域配置

文章地址:https://www.zhihuclub.com/78463.shtml

关于作者: 智云科技

热门文章

网站地图