Nginx CORS 配置
616 字
3 分钟
Nginx CORS 配置
浏览器同源策略
浏览器安全的基石是”同源策略”(same-origin policy). “同源”指的是”三个相同”:
- 协议相同
- 域名相同
- 端口相同
如果非同源,共有三种行为受到限制。
Cookie、LocalStorage、IndexDB无法读取DOM无法获得AJAX请求不能发送
CORS原理解析
CORS是跨源AJAX请求的根本解决方法. JSONP只能发GET请求, 但是CORS允许任何类型的请求.
整个CORS通信过程都是浏览器自动完成的, 不需要用户参与. 对于开发者来说, CORS通信与同源的AJAX通信没有差别, 代码完全一样. 浏览器一旦发现AJAX请求跨源,就会自动添加一些附加的头信息,有时还会多一次附加的请求,但用户不会有感觉。因此,实现CORS通信的关键是**服务器. **只要服务器支持 CORS, 就可以跨源通信.
简单请求和非简单请求
直接在请求时加上 Origin 的 header

发出请求前, 会发起一次 OPTION 请求

Nginx CORS 配置
add_header不是所有返回都会追加,只限特定状态码的返回才有效,如果想所有返回都生效需要加上always选项参数
官方的解释
Syntax: add_header name value [always];Default: —Context: http, server, location, if in location# 配置示例location /path { proxy_pass target; add_header 'Access-Control-Allow-Origin' $http_origin always; add_header 'Access-Control-Request-Method' 'GET,POST,OPTIONS,DELETE,PUT' always; add_header 'Access-Control-Allow-Credentials' 'true' always; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;}CORS 响应头说明
| header | 是否必填 | 说明 |
|---|---|---|
Access-Control-Allow-Origin | Y | Access-Control-Allow-Origin 它的值要么是请求时Origin字段的值,要么是一个*,表示接受任意域名的请求, 不推荐* |
Access-Control-Allow-Credentials | N | The Access-Control-Allow-Credentials header indicates whether or not the response to the request can be exposed when the credentials flag is true. |
Access-Control-Expose-Headers | N | The Access-Control-Expose-Headers header adds the specified headers to the allowlist that JavaScript (such as Response.headers) in browsers is allowed to access. |
Access-Control-Allow-Headers | Y | The Access-Control-Allow-Methods header specifies the method or methods allowed when accessing the resource. This is used in response to a preflight request. |
Access-Control-Allow-Methods | Y | The Access-Control-Allow-Methods header specifies the method or methods allowed when accessing the resource. This is used in response to a preflight request. |
Access-Control-Max-Age | N | The Access-Control-Max-Age header indicates how long the results of a preflight request can be cached. |
CORS 请求头说明
| header | 说明 |
|---|---|
Origin | The Origin header indicates the origin of the cross-origin access request or preflight request. |
Access-Control-Request-Method | The Access-Control-Request-Method is used when issuing a preflight request to let the server know what HTTP method will be used when the actual request is made. |
Access-Control-Request-Headers | The Access-Control-Request-Headers header is used when issuing a preflight request to let the server know what HTTP headers will be used when the actual request is made (for example, by passing them as the headers option to the Request() constructor). This browser-side header will be answered by the complementary server-side header of Access-Control-Allow-Headers. |
📎 参考文章
文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!
相关文章智能推荐
1
把 GitHub Action 抽成跨项目复用
服务器记录把飞书通知 Action 从单个项目迁移到共享 actions 仓库后的使用方式。
2
通过 GitHub Actions 实现跨仓库 Blog 自动部署
服务器介绍如何在源仓库的博客目录发生变更后,通过 GitHub Actions 自动触发另一个仓库的静态网站部署流程。
3
在 PVE 上给软路由虚拟机加一个网络 Watchdog
服务器在 PVE 宿主机上用 Bash 和 systemd timer 实现一个网络 watchdog:先检测公网连通性,连续失败后重启软路由虚拟机,多次自愈无效再升级为重启 PVE 宿主机。
4
命令行自动登录 JumpServer:SSH、TOTP 与密码提示的自动化
服务器把 JumpServer 登录流程拆成凭据读取和终端交互两层:zsh 从密码管理器安全读取 TOTP seed 与可选密码,expect 只负责响应 SSH 登录过程中的 password 和 Code 提示。
5
从一票海运入仓开始,梳理海外仓常见业务术语
业务用一批家具从宁波港发往洛杉矶海外仓的例子,串起 FCL、LCL、ETD、ETA、LFD、Demurrage、Detention 等常见术语。
随机文章随机推荐












