nginx跨域设置&文件上传大小限制
在部署项目的时候碰到这么一个问题: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跨域设置&文件上传大小限制的更多相关文章
- IIS 7 中设置文件上传大小的方法
在IIS 6.0中设置文件上传大小的方法,就是配置如下节点: <system.web> <httpRuntime maxRequestLength="1918200&quo ...
- struts2设置文件上传大小
利用struts2想要设置或者限制上传文件的大小,可以在struts.xml配置文件里面进行如下配置: <constant name="struts.multipart.maxSize ...
- IIS 6和IIS 7 中设置文件上传大小限制设置方法,两者是不一样的
在IIS 6.0中设置文件上传大小的方法,只要设置httpRuntime就可以了 <system.web> <httpRuntime executionTimeout="3 ...
- Spring boot2.0 设置文件上传大小限制
今天把Spring boot版本升级到了2.0后,发现原来的文件上传大小限制设置不起作用了,原来的application.properties设置如下: spring.http.multipart.m ...
- [PHP]Nginx与PHP的文件上传大小限制
---------------------------------------------------------------------------------------------------- ...
- php修改配置文件php.ini设置文件上传大小讲解
打开php.ini,首先找到;;;;;;;;;;;;;;;;; File Uploads ;;;;;;;;;;;;;;;;;区域,有影响文件上传的以下几个参数: file_uploads = ...
- SpringBoot设置文件上传大小限制--默认为1M
SpringBoot默认上传文件大小不能超过1MB,超过之后会报以下异常:org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeL ...
- Spring boot设置文件上传大小限制
原文:https://blog.csdn.net/lizhangyong1989/article/details/78586421 Spring boot1.0版本的application.prope ...
- IIS 之 通过 Web.config 修改文件上传大小限制设置方法
在IIS 6.0中,不设置默认大小为4M,设置文件上传大小的方法,maxRequestLength(KB),executionTimeout(毫秒),配置如下节点: <system.web> ...
随机推荐
- PI膜概述
一.概述 1.简述 聚酞亚胺薄膜又称PI薄膜(polyimide filin)是一种含有酞亚胺或丁二酞亚胺的绝缘类高分子材料.是目前工程塑料中耐热性最好的品种之一. 2.发展简史 1908年,PI聚合 ...
- NOIp2018D1T2 货币系统【分析&完全背包】
题目传送门 看到题目瞬间想起某凯的疑惑,感觉不会做....然后观察样例可以知道,去掉原来货币系统中能够被其他币值凑出来的数就是答案(样例分析法),然后就完事了(huaji). 简单理解一下吧: 首先, ...
- 【机器学习】Learning to Rank之Ranking SVM 简介
Learning to Rank之Ranking SVM 简介 排序一直是信息检索的核心问题之一,Learning to Rank(简称LTR)用机器学习的思想来解决排序问题(关于Learning t ...
- json字符串转成 json对象 json对象转换成java对象
import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject; 依赖包 <dependency> ...
- cp 命令
cp - copy files and directories 用法: cp [OPTION] source dest DESCRIPTION Copy SOURCE to DEST, or mult ...
- 【转贴】使用sar进行性能分析
使用sar进行性能分析 https://www.cnblogs.com/bangerlee/articles/2545747.html 很早之前就看过 但是自己一直没用过.. 2012-06-12 0 ...
- 【Python】【demo实验3】【显示素数,显示两个数范围内的所有素数】
打印两个整数之间的所有素数: (使用平方根来判断 是否应停止验证该数值是否为素数) for i in range(956253526252,9956253526252): k = 1 if i == ...
- Java后端技术面试汇总(第五套)
1.Java相关 • 乐观悲观锁的设计,如何保证原子性,解决的问题:• char和double的字节,以及在内存的分布是怎样:• 对象内存布局,然后讲下对象的死亡过程?• 对象头,详细讲下:• syn ...
- 测试Random类nextInt()方法连续两次结果一样的概率
public static void main(String[] args) { int count = 0; int a = 0; Random r = new Random(); while (t ...
- shell脚本if判断语句报错[: too many arguments的两种原因
shell脚本,if判断语句报错[: too many arguments 我遇到过两种情况: 1.第一中情况就是网上大家说的,字符串变量中可能存在空格,shell解析时将其认为是多个参数,再进行判断 ...