JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

Nginx跨域配置

wys521 2024-09-11 13:23:17 精选教程 24 ℃ 0 评论

目录

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

概述

这是关于php进阶到架构之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;
}

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

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表