6

Nginx 跨域问题解决方案

 2 years ago
source link: https://blog.51cto.com/u_15527728/5608624
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Nginx 跨域问题解决方案

精选 原创

Twizzz 2022-08-22 15:52:56 ©著作权

文章标签 nginx 客户端 跨域 文章分类 其它 系统/运维 阅读数186

很多人都会遇到 Nginx 跨域的问题, 而我遇到的问题是:

此时需要对 Nginx 进行跨域配置才可以访问 ​​www.c.com​​ 的获取客户端 ​​www.a.com​​ 来请求 ​​www.b.com​​ 的数据, 我的 Nginx 配置如下(重要部分):

location / {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
root html;
proxy_pass http://xxx:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 5;
}

别忘了把配置中 ​​proxy_pass​​ 对应的 ​​http://xxx:8000/​​ 地址换成你的服务地址, 当然了, 你的客户端请求的地址不可以是你的服务地址, 而是 Nginx 的地址, 这样就可以达到解决跨域的问题。感兴趣的朋友可以在3A服务器部署一套自己试验下。

  • 1收藏
  • 评论
  • 分享
  • 举报

上一篇:详解MySQL游标


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK