nginx配置中:
# options请求过滤
# 这里配置允许跨域的域名,* 代表所有,也可以写域名:http://www.xxx.com 或者IP+端口 http://192.168.1.10
add_header 'Access-Control-Allow-Origin' '*';
# 允许的请求类型
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Credentials' true;
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
if ($request_method = 'OPTIONS') {
# 不写入日志
access_log off;
# 这里配置允许跨域的域名,* 代表所有,也可以写域名:http://www.xxx.com 或者IP+端口 http://192.168.1.10
add_header 'Access-Control-Allow-Origin' '*';
# 允许的请求类型
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Credentials' true;
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
# 允许跨域的最大时间,超过这个时间又会重发一次OPTIONS请求获取新的认证
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
# 直接在这里返回204响应,不转发到后台服务程序
return 204;
}
# 反向代理
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto $scheme;
# proxy_connect_timeout 5;
配置完成。杀掉nginx进程,重启启动nginx服务。
windows: taskkill /f /t /im nginx.exe (杀掉所有nginx进程)
start nginx (进入到nginx.exe文件夹内,启动)