Nginx反向代理跨域访问气象局天气接口

Nginx反向代理跨域访问气象局天气接口,第1张

Nginx反向代理跨域访问气象局天气接口

nginx设置如下,客户端直接访问本地服务器83端口,通过http://localhost:83/wmaps/xml/baoding.xml实现对国家气象局天气实况的实时访问。
nginx.conf设置如下:

   server{
	listen	83;
	server_name	192.168.1.1;
	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';
        if ($request_method = 'OPTIONS') {
            return 204;
        }
		proxy_pass http://flash.weather.com.cn;
	}
    }

此处注意
if ($request_method = ‘OPTIONS’) {
必须有,不然浏览器还是会报跨域错误。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5433717.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存