nginx跨域配置

nginx跨域配置

    • Access-Control-Allow-Origin
    • Access-Control-Allow-Headers
    • Access-Control-Allow-Methods
    • Access-Control-Allow-Credentials
    • 示例:

Access-Control-Allow-Origin

配置服务器可以接受哪些请求源(Origin),即接受哪些跨域的请求,即哪些其他的域可以访问自身的内容。可单独指定可访问自身的域,也可配置 * ,允许所有跨域请求

Access-Control-Allow-Headers

跨域允许携带的特殊头信息字段,如 Content-Type,Content-Length,Authorization,Accept,X-Requested-With,可单独配置,也可配置 * ,允许所有

Access-Control-Allow-Methods

跨域允许的请求方法或者说HTTP动词,如 PUT,POST,GET,DELETE,OPTIONS。可单独配置,也可配置 * ,允许所有

Access-Control-Allow-Credentials

是否允许跨域使用cookies,配置 true 允许

示例:

 location / {
        # 这里配置单个代理跨域,跨域配置
		add_header 'Access-Control-Allow-Origin' 'http://localhost:8080';	#此为单独允许http://localhost:8080的请求跨域访问自身
		add_header 'Access-Control-Allow-Credentials' 'true';
		add_header 'Access-Control-Allow-Methods' 'PUT,POST,GET,DELETE,OPTIONS';
		add_header 'Access-Control-Allow-Headers' 'Content-Type,Content-Length,Authorization,Accept,X-Requested-With';

}

也可配置为:

 location / {
        # 这里配置单个代理跨域,跨域配置
		add_header 'Access-Control-Allow-Origin' *;	
		add_header 'Access-Control-Allow-Credentials' 'true';
		add_header 'Access-Control-Allow-Methods' *;
		add_header 'Access-Control-Allow-Headers' *;

}

参考:

Nginx配置跨域(CORS)

https://blog.csdn.net/weixin_44273388/article/details/124206129

HTTP的8种请求方式

https://blog.csdn.net/weixin_45659878/article/details/107927883

你可能感兴趣的:(nginx,服务器,运维)