Nginx 跨域配置

  1. 需求
  2. 跨域配置
    1. 配置前
    2. 配置后

需求

我们在开发API接口经常遇到跨域问题,这里通过配置nginx准许跨域来解决

跨域配置

配置前

server
{
    listen 134;
    server_name zsgy.qunx.site;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/zsgy.qunx.site/public;
    ...
}

配置后

server
{
    listen 134;
    server_name zsgy.qunx.site;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/zsgy.qunx.site/public;

    # 加入
    #允许跨域请求的域,*代表所有
    add_header 'Access-Control-Allow-Origin' *;
    #允许带上cookie请求
    add_header 'Access-Control-Allow-Credentials' 'true';
    #允许请求的方法,比如 GET/POST/PUT/DELETE
    add_header 'Access-Control-Allow-Methods' *;
    #允许请求的header
    add_header 'Access-Control-Allow-Headers' *;
    ...
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 [email protected]

×

喜欢就点赞,疼爱就打赏