使用Nginx代理配置解决跨域问题##
下载nginx
配置nginx
在nginx.conf文件中找到server代码块中做如下配置,注意,’/'不能少。其中前端服务的配置尽量不要加后缀,容易出现获取不到静态资源数据的bug。访问的时候通过访问 localhost:8888 进入前端服务器 ,请求接口的地址时把之前的 ip:port/改写成 localhost:8888/api/
server {
listen 8888;
server_name localhost;
#接口服务
location /api/ {
proxy_pass http://ip:port/;
}
#前端服务
location /{
proxy_pass http://www.baidu.com/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Nginx默认请求体是1M 假如涉及到文件的上传,是不够的 ,需要配置文件上传的限制
client_max_body_size 2048M;
client_header_timeout 1m;
client_body_timeout 1m;
proxy_connect_timeout 120s;
proxy_read_timeout 1m;
proxy_send_timeout 1m;