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> ...
随机推荐
- PJzhang:一道看线索找答案的逻辑题
猫宁!!! 这道逻辑题,2年前就有打算解决,但是没上心,今天抽空梳理出来了思路,逻辑上可以跑的通,不至于以后慢慢忘了,这道题和数独题基本类似,但是也许更花时间,做这种题最好看着线索列图标,省的不停翻页 ...
- PHP Smarty模板的安装
最近开发中用到了PHP中smarty模板..作为一个长久以来的前端,开始学习PHP模板..下面将安装教程分享给大家.. 1. 下载Smarty最新版: http://www.smarty.NET/do ...
- [转帖]浪潮信息最大供应商英特尔(Intel):2018 年采购额 145.76 亿元
浪潮信息最大供应商英特尔(Intel):2018 年采购额 145.76 亿元 https://t.cj.sina.com.cn/articles/view/3172142827/bd130eeb01 ...
- 从入门到自闭之Python闭包
闭包 定义:在嵌套函数内,使用(非本层变量)非全局变量就是闭包 闭包必须是内层函数对外层函数的变量(非全局变量)的引用 函数执行完毕后,函数体内的空间自行销毁 def func(): a=1 def ...
- python PEP8常用规范(看完你会感谢我的!)
完整的规范移步传送门 pep8规范 官方文档:[https://www.python.org/dev/peps/pep-0008/](https://www.python.org/dev/peps/p ...
- Hive 教程(六)-Hive Cli
hive 有两种启动方式,一种是 bin/hive,一种是 hiveserver2, bin/hive 是 hive 的 shell 模式,所有任务在 shell 中完成,shell 就相当于 hiv ...
- Java Web开发技术教程入门-自定义标签
回顾: 昨天了解了JSP开发的两种模式Model1和Model2模式.Model1采用JSP+JavaBean技术开发Web应用,它比较适合小规模应用的开发,效率较高,易于实现.但由于在Model1中 ...
- Codeforces Round #603 F Economic Difficulties
题目大意 给你两棵树,结点分别是1-A与1-B,然后给了N台设备,并且A树和B树的叶子结点(两棵树的叶子节点数量相同)都是链接电机的.问,最多可以删掉几条边使得每个设备都能连到任意一棵(或两棵)树的根 ...
- Charles学习(一)之macOS Charles 4.x版本的安装、激活、使用以及软件功能了解
前言 Charles是mac上一款比较好用的抓包工具,那么我们什么情况下需要用到抓包工具呢?比如我想查看一个接口请求的参数.返回值,还有移动设备上的http/https请求. Charles是一个HT ...
- Jquery实现对select的操作
select实现对文本框的显示和隐藏 /** * 通过select的值实现对文本框的显示和隐藏 * #id为一个select控件 * .obj为一个文本框 */ function initSelect ...