Nginx 反向代理解决跨域问题
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://111.111.111.111:8080; # 代理的地址
proxy_http_version 1.1;
proxy_set_header Connection "";
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User- Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
# root html;
# index index.html index.htm;
}
}
如果请求部分资源错误,报如下错:
upstream sent invalid chunked response while reading upstream
,
那么加上 proxy_http_version 1.1; proxy_set_header Connection "";