Nginx跨域无法访问,通常报错:
Failed to load http://172.18.6.30:8086/CityServlet: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://test.dingkailinux.cn’ is therefore not allowed access.
可以在nginx的配置文件中对应的localtion中添加:
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Max-Age 3600;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Headers
h
t
t
p
a
c
c
e
s
s
c
o
n
t
r
o
l
r
e
q
u
e
s
t
h
e
a
d
e
r
s
;
i
f
(
http_access_control_request_headers; if (
httpaccesscontrolrequestheaders;if(request_method = OPTIONS){
return 200;
}
重启nginx,就可以访问了