nginx解决跨域问题

介绍

使用nginx配置解决跨域问题。在nginx的配置文件location配置内添加以下配置,解决跨域问题。

add_header Access-Control-Allow-Origin '*' always;
add_header Access-Control-Allow-Headers '*';
add_header Access-Control-Allow-Methods '*';
add_header Access-Control-Allow-Credentials 'true';
if ($request_method = 'OPTIONS') {
    return 204;
}

示例

server {
    listen       22222;
    server_name  localhost;
    location  / {
        add_header Access-Control-Allow-Origin '*' always;
        add_header Access-Control-Allow-Headers '*';
        add_header Access-Control-Allow-Methods '*';
        add_header Access-Control-Allow-Credentials 'true';
        if ($request_method = 'OPTIONS') {
            return 204;
        }
        proxy_pass  http://localhost:59200;
    }
}

参考文章

你可能感兴趣的:(Nginx,nginx,运维)