在部署项目的时候碰到这么一个问题:XMLHttpRequest cannot load,下面阐述一下这个问题

问题背景:

用nginx+tomcat部署项目。tomcat用的8080端口,nginx用80代理8080端口。项目成功启动,但凡事涉及到ajax的数据调用全部都报 XMLHttpRequest cannot load的错

问题原因:

由于同源策略的限制,XmlHttpRequest只允许请求当前源(域名、协议、端口)的资源,只有域名、协议、端口三者都相同才会被认为是相同资源,而且ajax本身是不可以跨域的。而浏览器访问的是80端口、ajax方法访问的是8080端口,所以就会报错。

解决方案:

修改nginx配置文件,在server中加入如下代码

server {

        listen       80;

        server_name www.beib.com;

        index index.html index.htm index.php default.html default.htm default.php;

        root  /home/am/webapps/am;

location / {

add_header 'Access-Control-Allow-Origin' '*';

#

# Om nom nom cookies

#

add_header 'Access-Control-Allow-Credentials' 'true';

add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

#

# Custom headers and headers various browsers *should* be OK with but aren't

#

add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

proxy_pass http://www.beib.com:8080/;

proxy_set_header Host "www.beib.com";

}

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

问题解决了以后突然发现上传超过1M大的客户端文件无法正常上传,于是修改了下nginx的配置。

server {

        listen       80;

        server_name www.beib.com;

        index index.html index.htm index.php default.html default.htm default.php;

        root  /home/am/webapps/am;

location / {

add_header 'Access-Control-Allow-Origin' '*';

#

# Om nom nom cookies

#

add_header 'Access-Control-Allow-Credentials' 'true';

add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

#

# Custom headers and headers various browsers *should* be OK with but aren't

#

add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

proxy_pass http://www.beib.com:8080/;

client_max_body_size    1000m;

proxy_set_header Host "www.beib.com";

}

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

其实只是在location里加了如下一行代码

nginx跨域设置&文件上传大小限制的更多相关文章

  1. IIS 7 中设置文件上传大小的方法

    在IIS 6.0中设置文件上传大小的方法,就是配置如下节点: <system.web> <httpRuntime maxRequestLength="1918200&quo ...

  2. struts2设置文件上传大小

    利用struts2想要设置或者限制上传文件的大小,可以在struts.xml配置文件里面进行如下配置: <constant name="struts.multipart.maxSize ...

  3. IIS 6和IIS 7 中设置文件上传大小限制设置方法,两者是不一样的

    在IIS 6.0中设置文件上传大小的方法,只要设置httpRuntime就可以了 <system.web> <httpRuntime executionTimeout="3 ...

  4. Spring boot2.0 设置文件上传大小限制

    今天把Spring boot版本升级到了2.0后,发现原来的文件上传大小限制设置不起作用了,原来的application.properties设置如下: spring.http.multipart.m ...

  5. [PHP]Nginx与PHP的文件上传大小限制

    ---------------------------------------------------------------------------------------------------- ...

  6. php修改配置文件php.ini设置文件上传大小讲解

    打开php.ini,首先找到;;;;;;;;;;;;;;;;; File Uploads ;;;;;;;;;;;;;;;;;区域,有影响文件上传的以下几个参数: file_uploads   =   ...

  7. SpringBoot设置文件上传大小限制--默认为1M

    SpringBoot默认上传文件大小不能超过1MB,超过之后会报以下异常:org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeL ...

  8. Spring boot设置文件上传大小限制

    原文:https://blog.csdn.net/lizhangyong1989/article/details/78586421 Spring boot1.0版本的application.prope ...

  9. IIS 之 通过 Web.config 修改文件上传大小限制设置方法

    在IIS 6.0中,不设置默认大小为4M,设置文件上传大小的方法,maxRequestLength(KB),executionTimeout(毫秒),配置如下节点: <system.web> ...

随机推荐

  1. 应用安全 - PHP - CMS - DeDeCMS - 漏洞 - 汇总

    SSV-97074 Date 类型 前台任意密码修改 影响范围 前置条件 CVE-2018-20129 Date 类型前台文件上传 影响范围 前置条件(1)前台登录(2)/member/article ...

  2. 【并行计算-CUDA开发】OpenCL、OpenGL和DirectX三者的区别

    什么是OpenCL? OpenCL全称Open Computing Language,是第一个面向异构系统通用目的并行编程的开放式.免费标准,也是一个统一的编程环境,便于软件开发人员为高性能计算服务器 ...

  3. mint ui解决Navbar和Infinite scroll共存时的bug

    Navbar和Infinite scroll共同使用时会出现无限加载的问题,滑动也会出现乱加载. 只需要判断一下就可以了,代码: html: <mt-navbar v-model="s ...

  4. linux c++模拟简易网络爬虫

    /* * To change this license header, choose License Headers in Project Properties. * To change this t ...

  5. SSH代理

    参考: http://www.dkys.org/archives/1111.html SSH的-L与-D代理 SSH有三种代理参数-L,-D,-R.-R代理不是本次重点,有兴趣的读者可以自行查阅man ...

  6. 查找担保圈-step2-拆分成员表函数

    USE [test] GO /****** Object: UserDefinedFunction [dbo].[tf_split_char] Script Date: 2019/7/8 14:39: ...

  7. 【面试向】hihoCoder 1994 树与落叶

    题目链接 Implementation int n, q; scan(n,q); vi p(n + 1); vi nson(n + 1); up (i, 1, n) { scan(p[i]); nso ...

  8. Java第七周课堂示例总结

    一.super();调用基类构造方法 代码: class Grandparent{ public Grandparent(){ System.out.println("GrandParent ...

  9. jpa报错 Unable to acquire a connection from driver [null], user [null] and URL [null]

    jpa报错 Unable to acquire a connection from driver [null], user [null] and URL [null] 为啥报错 因为你在persist ...

  10. java限流工具类

    代码 import com.google.common.util.concurrent.RateLimiter; import java.util.concurrent.ConcurrentHashM ...