欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;
  
        }

nginx解决跨域问题

意思:

当你从外部访问 http://此服务器的ip/wkzs-restful/login.do
相当于此服务器访问 http://localhost:8007/login.do

这样就不会跨域了。

需要注意的是,域名访问和ip访问依然会跨域。写成一致就好。

有疑问的或者有知道解决方法的。欢迎留言!

nginx解决跨域问题

相关标签: nginx 跨域