nginx的跨域设置

官方文档 中说,只有当响应状态码为以下几种类型中之一时,add_header 才会生效。如果需要 add_header 在所有情况下都生效,可以在后面加上 always 参数即可解决。

Adds the specified field to a response header provided that the response code equals 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). The value can contain variables.
There could be several add_header directives. These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level.
If the always parameter is specified (1.7.5), the header field will be added regardless of the response code.

参考配置


server {
    listen 80;
    server_name servername;
    root /var/www/html;
    
    add_header Access-Control-Allow-Origin * always;
    add_header Access-Control-Allow-Methods * always;


    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include fastcgi.conf;
    }

}

猜你喜欢

转载自www.cnblogs.com/lovesKey/p/11371242.html