nginx 配置解决跨域

3 篇文章 0 订阅
3 篇文章 0 订阅

不知道你在做前端开发的时候有没有遇到一种情况,请求成功的话不会提示跨域,但是请求失败的情况却会提示跨域。我前几天就被这个情况困扰到了,我的nodejs服务没有使用cors去处理跨域,而是在nginx里面处理的,起初就是用下面的配置,当浏览器请求成功的时候一切正常,后来我加了个鉴权功能,没接口访问权限的情况返回了401的状态码,但是前端的请求代码中没办法捕获到401的状态码,然后查看控制台确实有401的状态码错误。当然这个情况不限于401的状态码,理论上40X和50X的状态码都会出现这种情况。下面我们先从一般使用nginx处理跨域的方式说起

image-20240110112115650

nginx跨域处理一般配置

location / {
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Methods *;
  add_header Access-Control-Allow-Headers *;
  if ($request_method = 'OPTIONS' ) {
    return 204;
  }
  proxy_pass ***;
 }

注意事项

乍一看上面的配置没什么问题,但是当服务器响应比如40x或者50x的状态码的时候,打开控制台你会发现还是会报跨域的错误,这时候会导致前端代码无法正确获取服务器响应状态码

add_header官方的语法说明

Syntax:add_header name value [always];
Default:
Context:http, server, location, if in location

Adds the specified field to a response header provided that the response code equals 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). Parameter value can contain variables.

There could be several add_header directives. These directives are inherited from the previous configuration level if and only if there are no add_header directives defined on the current level.

If the always parameter is specified (1.7.5), the header field will be added regardless of the response code.

这里主要注意两个点:

  • 默认情况仅会在200等状态码的情况下给响应添加请求头
  • always参数存在的话会忽略响应状态码,总是添加请求头

修改配置

location / {
  add_header Access-Control-Allow-Origin * always;
  add_header Access-Control-Allow-Methods * always;
  add_header Access-Control-Allow-Headers * always;
  if ($request_method = 'OPTIONS' ) {
    return 204;
  }
  proxy_pass ***;
 }

注意: 配置里面的“*”只是为了演示,实际的生产的项目应该根据实际情况配置。

参考

track.nginx.org

nginx.org

  • 17
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
配置Nginx服务器解决跨域问题,可以参考以下步骤: 1. 打开Nginx配置文件,通常位于`/etc/nginx/nginx.conf`或`/etc/nginx/conf.d/default.conf`。 2. 在配置文件中找到`server`块,可以使用`server_name`指令来指定服务器名称。 3. 在`location`块中添加以下配置解决跨域问题: ``` location / { add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; proxy_pass http://localhost:8080/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } ``` 这些配置将允许所有来源的请求访问服务器,并允许使用GET、POST和OPTIONS方法。同时,还会添加一些常见的请求头信息。 4. 如果你需要支持WebSocket,可以取消注释`proxy_http_version`、`proxy_set_header Upgrade`和`proxy_set_header Connection`这几行配置。 5. 保存配置文件并重启Nginx服务,以使配置生效。 请注意,以上配置仅供参考,具体配置可能因你的实际需求而有所不同。在修改配置文件之前,建议备份原始配置文件以防止意外情况发生。 #### 引用[.reference_title] - *1* [Nginx 轻松解决跨域问题](https://blog.csdn.net/leonnew/article/details/123895626)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Nginx 解决跨域](https://blog.csdn.net/chenzhao635/article/details/128079241)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值