浏览 3.8k

按点赞数排序
按时间排序
有2个解决方案:
1、基于CORS,即Cross-Origin Resource Sharing,解决浏览器端的安全策略问题。又可以细分为2种,一种叫复杂请求,也就是上面dongchch的答案,对于OPTIONS请求的处理,当然后面的proxy_pass是无关的。一种叫简单请求,直接添加Access_Control_Allow-Origin头部就可以了,浏览器会处理的。
具体你可以参见我的这一课:《如何合法的跨域》https://time.geekbang.org/course/detail/100026801-98913
2、用反向代理解决。你可以直接用proxy_set_header等指令,用Nginx代理时模拟非跨域的客户端,访问实际的应用服务器,并修改应用服务器响应中的域名即可。
2
回答于2020-06-12 07:49
location /request {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin 'https://XXXXX' always;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
add_header Access-Control-Allow-Headers 'requestname,signature,signkey' always;
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
proxy_pass http://127.0.0.1/request;
}
以上请参考。
0
回答于2020-06-11 21:21
这个问题已经的到解决,需要在nginx controller的service 中配置environment, gateway, api, certs等相关参数,即可得到解决
可以这么进行调试:
1. error_log 等级设置为 debug
2. 在日志中找到“http init upstream“的字样,然后往下找会看到nginx想后端发送的http请求,可以从这个请求构造curl命令直接访问后端进行查看该资源是啥样的情况
微信公众号
加入微信群