nginx解决跨域问题
程序员文章站
2022-07-10 12:43:50
...
场景
- A:是放在nginx/html 下的html页面 属于静态资源! (不是用tomcat发布过的)
- B:是部署在本服务器上的接口层。开放端口为8007
- 目的:A.html 里面 写个ajax -----》请求到B的某个端口 比如 8007 下的方法。
很多方法可以解决。网上找了一下。
很多都是前端允许携带cookie,后端再配个过滤器,允许跨域访问。
而我这个B 他是接口层的。(给app端提供调用) 怎么配过滤器。。(楼住知识有限)
解决办法:
打开nginx/conf/nginx.conf
添加如下代码:
location /wkzs-restful {
root html;
proxy_pass http://localhost:8007/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
index index.html ind ml index.htm;
}
意思:
当你从外部访问 http://此服务器的ip/wkzs-restful/login.do
相当于此服务器访问 http://localhost:8007/login.do
这样就不会跨域了。