首页 > 解决方案 > 覆盖位置块上的 nginx http 版本

问题描述

我正在尝试在 Amazon Elastic Beanstalk 应用程序上设置端到端 http2 连接。我正在使用 node js 并通过 http2 支持进行 fastify(它在我的本地机器上运行良好)。默认情况下,EB 在部署代码的 EC2 实例上创建的 nginx 反向代理使用 http/1.1,所以我需要更改它。

我在这里阅读了如何做到这一点(请参阅反向代理配置部分)。问题是,如果您看到该nginx.conf文件:

#Elastic Beanstalk Nginx Configuration File

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

events {
    worker_connections  1024;
}

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

    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;
        access_log    /var/log/nginx/access.log main;

        client_header_timeout 60;
        client_body_timeout   60;
        keepalive_timeout     60;
        gzip                  off;
        gzip_comp_level       4;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

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

在最后一行include conf.d/elasticbeanstalk/*.conf;中,包含一个文件00_application.conf。该文件包含以下内容:

location / {
    proxy_pass          http://127.0.0.1:8080;
    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;
}

在那里你可以看到proxy_http_version我需要更改为的参数2.0

知道如何实现吗?我可以将配置文件添加到整个文件conf.d并替换整个nginx.conf文件,但我真的不知道如何从那里更改该值。

标签: nginxamazon-elastic-beanstalkhttp2nginx-confignginx-location

解决方案


在 .ebextension 文件夹中创建一个扩展名为 .config 的文件,例如 01-mynginx.conf

在配置文件中,使用 files 键在实例上创建文件,并在设置应用程序和 Web 服务器后使用 container_commands 键运行系统命令

files:
 “/etc/nginx/conf.d/01-mynginx.conf”:
 mode: “000644”
 owner: root
 group: root
 content: |
   keepalive_timeout 120s;
   proxy_connect_timeout 120s;
   proxy_send_timeout 120s; 
   proxy_read_timeout 120s; 
   fastcgi_send_timeout 120s; 
   fastcgi_read_timeout 120s;
container_commands:
 nginx_reload:
 command: “sudo service nginx reload”

Elastic Beanstalk 会自动在 /etc/nginx/conf.d 文件夹中创建 01-mynginx.conf 文件,并包含在主 elastic beanstalk nginx 配置中。


推荐阅读