【问题标题】:ElasticBeanstalk nginx returning 400 Bad Request when Context-Length header is sentElasticBeanstalk nginx 在发送 Context-Length 标头时返回 400 Bad Request
【发布时间】:2020-09-05 01:24:52
【问题描述】:

我们正在 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 标头,请求就会到达服务器,但是似乎没有包含正文。

我已经在 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 的网站。 ???更不用说发布带有身体的东西了。

提前致谢,

附带问题:有谁知道我在哪里可以找到 AWS 用于其 Amazon Linux 2 映像的“默认”nginx 配置?

【问题讨论】:

  • 要获取 EB 上的默认 nginx 设置,您可以通过 ssh 进入 EB 实例并从那里进行检查。
  • @Marcin - 谢谢,这是个好主意。我也想过这个,但是,我正在网上寻找资源。我在 Linux 上不强,但是,我可能会弄明白。
  • 我可以在我的 EB 实例上检查它,然后发布它,如果这有帮助吗?
  • @Marcin - 谢谢。如果它是 AWS Amazon Linux 2 AMI,我认为这就是我要寻找的东西。我正在研究如何在通过 ElasticBeanstalk 部署时设置私钥,以便我可以按照您的建议直接从我的 EC2 实例中提取它。

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


【解决方案1】:

我最终请求 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;

【讨论】:

  • 可以确认。更改 proxy_set_header Connection $http_connection; 对我来说是这样做的。我被困在这个问题上太久了,我几乎放弃了。我不明白这不是一个更大的问题。我无法向该服务器发送 POST 请求。身体总是在下坠。编辑:对于后来出现的任何人,我还为整个 nginx 配置提供弹性 beanstalk。对于 Amazon Linux 2 环境,将nginx.conf 放入您在部署期间上传的任何内容中的.platform/nginx/nginx.conf。
【解决方案2】:

基于 cmets。

我使用Docker running on 64bit Amazon Linux 2/3.1.0 上的示例应用程序检查了默认的 nginx 配置文件。

/etc/nginx/nginx.conf

# Elastic Beanstalk Nginx Configuration File

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

events {
    worker_connections  1024;
}

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    Upgrade                $http_upgrade;
            proxy_set_header    Host                   $host;
            proxy_set_header    X-Real-IP              $remote_addr;
            proxy_set_header    X-Forwarded-For        $proxy_add_x_forwarded_for;
        }

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

/etc/nginx/conf.d/elasticbeanstalk-nginx-docker-upstream.conf

upstream docker {
    server 172.17.0.2:8000;
    keepalive 256;
}

/etc/nginx/conf.d/healthd_logformat.conf

log_format healthd  '$msec"$uri"'
                    '$status"$request_time"$upstream_response_time"'
                    '$http_x_forwarded_for';[ec2-user@ip-172-31-36-64 conf.d]$ readlink -f ./healthd_logformat.conf

/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;
}

【讨论】:

  • 谢谢。 :) 基于此,我可能必须将我的包含文件移动到“elasticbeanstalk”文件夹中才能将其放入正确的部分。再次感谢。
  • @J.Chaney 没问题。如果您想在 Amazon Linux 2 here 上自定义 nginx,您将找到有关如何执行此操作的信息。您可以通过将配置文件添加到 .platform/nginx/conf.d/ 来扩展它,甚至可以使用 .platform/nginx/nginx.conf 完全覆盖默认的 nginx 配置文件。顺便说一句,如果您觉得我的回答有帮助,一旦您对它感到满意,我们将不胜感激。
  • 是的,这就是我获取有关如何自定义 nginx 配置的信息的地方。我不确定我的 conf 扩展是否进入了正确的位置以产生影响,我什至不确定 client_max_body_size 是否是问题所在。您可以在配置中看到它未在默认配置中指定。 nginx 文档说在这种情况下它应该是 1m。我的有效载荷只有 147 个字节。我认为设置 client_max_body_size 甚至不能解决我的主要问题,即我无法让 nginx 将 body 传递给我的应用程序。
  • 我发现了如何将私钥添加到我的 ElasticBeanstalk EC2 实例并与 Putty 连接。我能够找到我的 nginx 配置。谢谢:)
  • 对不起。这是一个中间问题的答案,“如何找到当前的 nginx 配置”,但不是如何解决问题的答案,“400 Bad Request when there are a body on the request”。虽然在调试和寻找答案方面很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 2017-11-30
相关资源
最近更新 更多