docker - ElasticBeanstalk nginx 在发送 Context-Length header 时返回 400 Bad Request

标签 docker nginx .net-core nginx-reverse-proxy

我们正在 Docker 容器中尝试我们的第一个 .Net Core 3.0 Web 应用程序。我们正在通过 AWS ElasticBeanstalk 部署到 Amazon Linux 2 AMI。我们能够毫无问题地执行服务器的任何 GET 请求。请求中包含正文的任何​​内容都会失败,并出现 400 Bad Request 错误。 PATCH 和 POST 都失败。如果端点不期望正文,则 PATCH 有效。我们发送的内容只有 147 字节,所以它甚至不应该达到默认的 client_max_body_size 1m。
我发现如果我在我的 Postman 测试中删除 Content-Length header ,请求就会到达服务器,但是似乎没有包含正文。
我已经测试了从 VisualStudio 和 Docker Desktop 本地运行的结果,它可以工作。我的本地操作系统是 Windows 10,而不是 Linux,而且我没有在本地使用 nginx 或 IIS。
我尝试通过将名为 nginxext.conf 的文件放入“.platform/nginx/conf.d”中,将我的 client_max_body_size 设置为 0(和 20m)。我很确定它正在被读取,因为我第一次收到错误,因为该文件不是 UTF-8 编码的。
我知道我错过了一些非常基本的东西。这看起来不像是一个现成的配置,在 Linux 上的 Docker 中使用 .Net Core 3.0 用于接受 POST 的网站。 ???更不用说用 body 发布东西了。
提前致谢,
附带问题:有谁知道我在哪里可以找到 AWS 用于其 Amazon Linux 2 镜像的“默认”nginx 配置?

最佳答案

我最终请求 AWS 提供支持。在来回查看他提供的一些链接后,我们能够解决问题。
https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.1
https://github.com/aspnet/KestrelHttpServer/issues/1263
我的部署最终得到了一个完整的 nginx.conf 文件,但是,我很确定这些行是为我解决问题的原因。

  http
    server
        location / {
            proxy_pass                                  http://docker;
            proxy_http_version                          1.1;
            proxy_set_header    Connection              $http_connection;
            proxy_set_header    Upgrade                 $http_upgrade;
            proxy_set_header    Host                    $host;
            proxy_set_header    X-Real-IP               $remote_addr;
            proxy_set_header    X-Forwarded-Host        $http_host;
            proxy_set_header    X-Forwarded-For         $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto       $scheme;
            proxy_cache_bypass                          $http_upgrade;

            client_max_body_size 100M;
我还将这些行添加到我的 Startup.Configuration() 方法的开头。
using Microsoft.AspNetCore.HttpOverrides;
        
        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });
感谢 Ali P(AWS 支持)和 @Marcin 的帮助。
完整的 nginx.conf 文件
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
# Elastic Beanstalk Nginx Configuration File ( nginx  for my app ) :

user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;
worker_rlimit_nofile    32136;

events {
    worker_connections 2048;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log    /var/log/nginx/access.log;


    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

    include  conf.d/*.conf;

    map $http_upgrade $connection_upgrade {
            default       "upgrade";
    }

    server {
        listen 80 default_server;
        gzip on;
        gzip_comp_level 4;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        access_log    /var/log/nginx/access.log main;

        location / {
            proxy_pass                                  http://docker;
            proxy_http_version                          1.1;
            #proxy_set_header    Connection              $connection_upgrade;
            #proxy_set_header    Connection keep-alive;
            #proxy_set_header    Connection              "Upgrade";
            proxy_set_header    Connection              $http_connection;
            proxy_set_header    Upgrade                 $http_upgrade;
            proxy_set_header    Host                    $host;
            proxy_set_header    X-Real-IP               $remote_addr;
            proxy_set_header    X-Forwarded-Host        $http_host;
            proxy_set_header    X-Forwarded-For         $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto       $scheme;
            proxy_cache_bypass                          $http_upgrade;

            client_max_body_size 100M;

        }

        # Include the Elastic Beanstalk generated locations
        include conf.d/elasticbeanstalk/*.conf;
    }
}

# configuration file /etc/nginx/mime.types:

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    font/woff                                        woff;
    font/woff2                                       woff2;

    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;
    application/postscript                           ps eps ai;
    application/rtf                                  rtf;
    application/vnd.apple.mpegurl                    m3u8;
    application/vnd.google-earth.kml+xml             kml;
    application/vnd.google-earth.kmz                 kmz;
    application/vnd.ms-excel                         xls;
    application/vnd.ms-fontobject                    eot;
    application/vnd.ms-powerpoint                    ppt;
    application/vnd.oasis.opendocument.graphics      odg;
    application/vnd.oasis.opendocument.presentation  odp;
    application/vnd.oasis.opendocument.spreadsheet   ods;
    application/vnd.oasis.opendocument.text          odt;
    application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                     pptx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                     xlsx;
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                     docx;
    application/vnd.wap.wmlc                         wmlc;
    application/x-7z-compressed                      7z;
    application/x-cocoa                              cco;
    application/x-java-archive-diff                  jardiff;
    application/x-java-jnlp-file                     jnlp;
    application/x-makeself                           run;
    application/x-perl                               pl pm;
    application/x-pilot                              prc pdb;
    application/x-rar-compressed                     rar;
    application/x-redhat-package-manager             rpm;
    application/x-sea                                sea;
    application/x-shockwave-flash                    swf;
    application/x-stuffit                            sit;
    application/x-tcl                                tcl tk;
    application/x-x509-ca-cert                       der pem crt;
    application/x-xpinstall                          xpi;
    application/xhtml+xml                            xhtml;
    application/xspf+xml                             xspf;
    application/zip                                  zip;

    application/octet-stream                         bin exe dll;
    application/octet-stream                         deb;
    application/octet-stream                         dmg;
    application/octet-stream                         iso img;
    application/octet-stream                         msi msp msm;

    audio/midi                                       mid midi kar;
    audio/mpeg                                       mp3;
    audio/ogg                                        ogg;
    audio/x-m4a                                      m4a;
    audio/x-realaudio                                ra;

    video/3gpp                                       3gpp 3gp;
    video/mp2t                                       ts;
    video/mp4                                        mp4;
    video/mpeg                                       mpeg mpg;
    video/quicktime                                  mov;
    video/webm                                       webm;
    video/x-flv                                      flv;
    video/x-m4v                                      m4v;
    video/x-mng                                      mng;
    video/x-ms-asf                                   asx asf;
    video/x-ms-wmv                                   wmv;
    video/x-msvideo                                  avi;
}

# configuration file /etc/nginx/conf.d/elasticbeanstalk-nginx-docker-upstream.conf:
upstream docker {
    server 172.17.0.3:80;
    keepalive 256;
}
# configuration file /etc/nginx/conf.d/healthd_logformat.conf:
log_format healthd  '$msec"$uri"'
                    '$status"$request_time"$upstream_response_time"'
                    '$http_x_forwarded_for';
# configuration file /etc/nginx/conf.d/elasticbeanstalk/healthd.conf:
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
    set $year $1;
    set $month $2;
    set $day $3;
    set $hour $4;
}

access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;

关于docker - ElasticBeanstalk nginx 在发送 Context-Length header 时返回 400 Bad Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63670909/

相关文章:

c# - ASP.NET核心: Parse complex struct from URL

.net - 无法测试部署到 Docker 的简单 Asp.net 核心 Web API

postgresql - docker-compose:创建数据库容器时控制台不返回主机

docker - 我可以在 AMD64 机器上为 Raspberry Pi 构建 Docker 包含吗?

node.js - Docker 中的远程调试 Node 应用程序

deployment - Nginx 位置、别名、重写、root

.net - HttpWebRequest 似乎没有发送客户端 SSL 证书

tomcat - 能否指示浏览器缓存具有可变查询字符串的资源?

c# - 函数的监听器无法启动。为什么?

c# - 无法使用 C# 将消息添加到存储队列