nginx - 设置 nginx post 请求的响应 header

标签 nginx nginx-reverse-proxy nginx-config

Nginx post 请求似乎正在删除我的 header ,那么如何才能让 header “Access-Control-Allow-Origin”保留在 post 请求上?

    location /api {
        proxy_pass http://127.0.0.1:3567/;

        if ($request_method = OPTIONS) {
            add_header "Access-Control-Allow-Origin" "http://localhost:8080";
            add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS";
            add_header "Content-Type" "text/plain";
            add_header "Access-Control-Allow-Credentials" "true";
            add_header "Access-Control-Allow-Headers" "content-type,rid,fdi-version";
            add_header "Content-Length" 0;
            return 204;
        }

        add_header "Access-Control-Allow-Origin" "http://localhost:8080";
    }

目前,当请求方法设置为 OPTIONS 时,它可以工作,但不适用于 POST 请求。我希望我的响应中会有标题“Access-Control-Allow-Origin”,但没有

最佳答案

您的 POST 请求是否有可能返回错误响应?如果是这样改变

add_header "Access-Control-Allow-Origin" "http://localhost:8080";

add_header "Access-Control-Allow-Origin" "http://localhost:8080" always;

关于nginx - 设置 nginx post 请求的响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74284436/

相关文章:

NGINX 不传递响应 header

angular - 在与根文件夹不同的文件夹中部署Angular应用

用于响应内容操作的 Kubernetes Ingress Gateway

ssl - Nginx 通过 Cloudflare 反向代理到 Jetty 应用服务器

nginx - 如何在 401 上覆盖 WWW-Authenticate

docker - Angular 与 Nginx 和 docker-compose

django - 使用 Docker-Compose 和 nginx-proxy 时出现 502 Bad Gateway 错误

nginx - ~* 在 nginx 服务器 block 中意味着什么

docker - 当上游节点不可用时,Nginx 容器退出并显示代码 1

使用查询参数值进行 NGINX 301 重定向