【问题标题】:Configuring nginx config files in AWS elasticbeanstalk with .ebextensions not found在 AWS elasticbeanstalk 中使用 .ebextensions 配置 nginx 配置文件未找到
【发布时间】:2020-12-14 19:19:46
【问题描述】:

我正在尝试使用自签名 SSL 为部署在 AWS 弹性 beanstalk 上的 springboot 网络服务器后端启用 https。我按照在线教程和指南使用新的 https-instance.config 更改我的 nginx 配置。

files:
  /etc/nginx/conf.d/myconf.conf:
    mode: "conf"
    owner: root
    group: root
    content: |
      # HTTPS server

      server {
        listen 443;
        server_name localhost;

        ssl on;
        ssl_certificate /etc/pki/tls/certs/server.crt;
        ssl_certificate_key /etc/pki/tls/certs/server.key;
        ssl_prefer_server_ciphers on;

        location / {
          proxy_pass  http://localhost:5000;
          proxy_http_version  1.1;
          proxy_set_header  Connection "";
          proxy_set_header  Host  $host;
          proxy_set_header  X-Real-IP  $remote_addr;
          proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
      }

  /etc/pki/tls/certs/server.crt:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN CERTIFICATE-----
 mycert
      -----END CERTIFICATE-----
      
  /etc/pki/tls/certs/server.key:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN RSA PRIVATE KEY-----
mykey
      -----END RSA PRIVATE KEY-----

  /opt/elasticbeanstalk/hooks/appdeploy/post/03_restart_nginx.sh:
      mode: "000755"
      owner: root
      group: root
      content: |
        #!/usr/bin/env bash
        sudo service nginx restart

当我 ssh 到我的实例时,我无法在 conf.d 下找到 myconf.conf 文件。运行service nginx status 给了我

● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/nginx.service.d
           └─nginx.conf
   Active: active (running) since Wed 2020-08-26 03:06:34 UTC; 15min ago
  Process: 28894 ExecStartPost=/bin/sh -c systemctl show -p MainPID nginx.service | cut -d= -f2 > /var/pids/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 28890 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 28887 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 28886 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 28893 (nginx)
   CGroup: /system.slice/nginx.service
           ├─28893 nginx: master process /usr/sbin/nginx
           └─28897 nginx: worker process

Aug 26 03:06:34  systemd[1]: Starting The nginx HTTP and reverse proxy server...
Aug 26 03:06:34 nginx[28887]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Aug 26 03:06:34  nginx[28887]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Aug 26 03:06:34  systemd[1]: Started The nginx HTTP and reverse proxy server.

我错过了什么。这是我在 AWS EBS 上的第一个项目。

注意:我正在为 EBS 运行免费层单实例

【问题讨论】:

    标签: amazon-web-services spring-boot ssl nginx amazon-elastic-beanstalk


    【解决方案1】:

    您尝试使用的 nginx 设置 (/etc/nginx/conf.d/myconf.conf) 适用于 Amazon Linux 1

    但您似乎使用的是 Amazon Linux 2 (AL2)。因此,您应该使用不同的文件来设置 nginx。对于 AL2,nginx 设置应该在.platform/nginx/conf.d/ 中,而不是在.ebextentions 中,如docs 中所示。

    因此,您可以拥有以下 .platform/nginx/conf.d/myconfig.conf 的内容:

    server {
        listen 443;
        server_name localhost;
    
        ssl on;
        ssl_certificate /etc/pki/tls/certs/server.crt;
        ssl_certificate_key /etc/pki/tls/certs/server.key;
        ssl_prefer_server_ciphers on;
    
        location / {
          proxy_pass  http://localhost:5000;
          proxy_http_version  1.1;
          proxy_set_header  Connection "";
          proxy_set_header  Host  $host;
          proxy_set_header  X-Real-IP  $remote_addr;
          proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
    }
    

    以上只是配置文件的示例。我无法验证该设置是否真的有效,但您肯定使用了错误的文件夹来设置 AL2 中的 nginx 选项。

    我的建议是先尝试通过 ssh 手动使其工作。如果您提供自己的.platform/nginx/nginx.conf 文件不起作用,您可能会发现您需要覆盖整个 nginx 设置。

    【讨论】:

    • 谢谢 Marcin,我没有想到它可能是错误的 linux 版本!
    • 非常感谢!我花了很长时间浏览 AWS 文档/教程(但我猜这是错误的版本)。我无法弄清楚为什么当我 ssh 进入时我的配置没有显示出来。
    • 这个.platform/nginx/nginx.conf.platform/hooks之前执行吗?我有这样的设置,但总是错误地指出文件.crt 不存在。使用certbot生成的文件在.platform/hooks/prebuild上执行
    【解决方案2】:

    亚马逊 Linux 2: 您必须在文件夹中创建文件,该文件夹与您实际想要将其放置在 Elasticbeanstalk 上的层次结构相同。例如,在 EBS 中,我需要在 /etc/nginx/conf.d/elasticbeanstalk/ 下创建自定义 conf 文件 为此,我的文件夹结构将是:

    我可以在文件中添加我需要的配置:

    location / {
                try_files $uri $uri/ /index.php?$args;
          }
    

    【讨论】:

      【解决方案3】:

      在您的 Elastic Beanstalk 仪表板上找到了 Amazon Linux 2 (AL2) 的类型:

      从你的应用的根目录创建如下目录结构:

      .platform/nginx/conf.d/
      

      创建以下文件:

      .platform/nginx/conf.d/01_custom_ngix.conf
      

      将以下内容添加到您创建的 01_custom_ngix.conf 文件中:

      add_header 'Access-Control-Allow-Origin' '*' always;
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
      add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
      add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
      

      Git 提交您的更改(确保在部署之前执行此操作,因为使用 eb deploy 时您的代码不会被部署,除非您提交更改)

      部署您的代码:

      > eb deploy 
      

      选项:eb deploy --verbose --debug

      使用 http cli 验证您的 http 响应:

      > http https://name-of-nodejs-server.domain.com
      

      输出:

      HTTP/1.1 200 OK
      Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range
      Access-Control-Allow-Methods: GET, POST, OPTIONS
      Access-Control-Allow-Origin: *
      Access-Control-Expose-Headers: Content-Length,Content-Range
      Connection: keep-alive
      Content-Length: 22
      Content-Type: text/html; charset=utf-8
      Date: Thu, 26 Aug 2021 02:33:30 GMT
      ETag: W/"16-dDAEIDLSKDKGJGJGKDLASE"
      Server: nginx/1.20.0
      X-Powered-By: Express
      

      完成

      可选,您还可以通过将以下内容添加到您的 01_custom_ngix.conf 来增加您的 client_max_body_size

      client_max_body_size 1000M;
      client_body_buffer_size 100M;
      ....
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
      

      【讨论】:

        猜你喜欢
        • 2020-09-17
        • 2014-12-30
        • 2020-12-09
        • 1970-01-01
        • 2020-11-04
        • 2017-05-24
        • 2013-06-14
        • 2017-05-30
        • 2021-01-11
        相关资源
        最近更新 更多