跳至主要內容

Nginx 配置

Yuchen小于 1 分钟NginxNginx

Nginx 配置

跨域

location 下添加如下配置

location / {
	#CORS 配置
	add_header 'Access-Control-Allow-Origin' '*';
	add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
	#是否允许cookie传输
	add_header 'Access-Control-Allow-Credentials' 'true';
	add_header 'Access-Control-Allow-Headers'
		'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token';

	#针对浏览器的options预请求直接返回200,否则会被403 forbidden--invalie CORS request
	if ( $request_method = 'OPTIONS' ) {
		return 200;
	}
}

带下划线的 header 丢失问题

nginx 默认会忽略带下划线的请求头, 需在 http 下添加配置

http {
    # ...
    # 启用下划线
    underscores_in_headers on;
}