Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net Docker Nginx proxy-Angular应用程序中POST请求的CORS错误_Asp.net_Angular_Docker_Nginx - Fatal编程技术网

Asp.net Docker Nginx proxy-Angular应用程序中POST请求的CORS错误

Asp.net Docker Nginx proxy-Angular应用程序中POST请求的CORS错误,asp.net,angular,docker,nginx,Asp.net,Angular,Docker,Nginx,我遇到了一件非常奇怪的事情。我有以下堆栈: ASP.Net核心3.1 API 角度10前端应用程序 Nginx代理 所有应用程序都是容器化的,因此我的API在docker容器中运行,angular应用程序在docker容器中运行(也使用单独的nginx web服务器为SPA提供服务),nginx容器作为API的代理 下面是一个没有问题的典型GET请求,以及选项请求的相关标头: 因此,GET请求正在工作,但当我尝试使用POST时,选项请求立即成功,随后nginx发出400,同时浏览器发出错

我遇到了一件非常奇怪的事情。我有以下堆栈:

  • ASP.Net核心3.1 API
  • 角度10前端应用程序
  • Nginx代理
所有应用程序都是容器化的,因此我的API在docker容器中运行,angular应用程序在docker容器中运行(也使用单独的nginx web服务器为SPA提供服务),nginx容器作为API的代理

下面是一个没有问题的典型GET请求,以及选项请求的相关标头:

因此,GET请求正在工作,但当我尝试使用POST时,选项请求立即成功,随后nginx发出400,同时浏览器发出错误消息:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://restaurantapi.localhost/chats. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
上述错误消息的奇怪之处在于,POST的选项请求成功:

如何才能成功返回选项请求,但POST失败?我不太明白这怎么可能。我知道是它的Nginx导致了这个问题,因为我已经删除了代理,并使用kestrel web服务器(内置于.NET core的web服务器)将请求直接从容器中的angular应用程序发送到API,并且成功了

是否有任何配置丢失导致此问题?请注意,我是在API中添加CORS头,而不是通过nginx使用CORS。我还尝试从Nginx内的API中剥离响应头并显式添加CORS头,但仍然失败。在此方面的任何帮助都将不胜感激

我的nginx配置:

events {
    worker_connections 1024;
}
http {
    underscores_in_headers on;

    upstream api {
        server restaurantapi:5001;
    }
    upstream grpcservice {
        server restaurantapi:5010;
    }

    # redirect all http requests to https
    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        return 301 https://$host$request_uri;
    }
    server {
        server_name restaurantapi.localhost;
        listen 443 ssl http2;
        ssl_certificate /etc/certs/resapi.crt;
        ssl_certificate_key /etc/certs/resapi.key;
    
        location /CartCheckoutService/ValidateCartCheckout {
            grpc_pass grpc://grpcservice;
            error_page 502 = /error502grpc;
        }
    
        location = /error502grpc {
            internal;
            default_type application/grpc;
            add_header grpc-status 14;
            add_header grpc-message "Error connecting to gRPC service.";
            return 204;
        }
    
        location / {
            proxy_pass http://api;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Connection keep-alive;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_cache_bypass $http_upgrade;           
        }
    }
    gzip on;
    gzip_vary on;
    gzip_proxied no-cache no-store private expired auth;
    gzip_types text/plain text/css application/json application/xml;
}
API中的日志:


问题在于存在websocket头()。我不完全确定nginx是否会记录到上游服务器的连接错误,因为显示的所有日志都是对nginx的请求

删除特定于websocket的标题修复了我遇到的问题。我只需要为websocket请求添加标题