【问题标题】:Elastic Beanstalk + Nginx + Node + React 502 badegatewayElastic Beanstalk + Nginx + Node + React 502 badegateway
【发布时间】:2021-03-12 02:50:15
【问题描述】:

我有以下堆栈:

  • 节点后端构建
  • 反应前端构建

我想将它们部署到 ELB 应用程序中。

当我这样做并尝试访问实例时,我得到 502 Bad Gateway nginx/1.18.0 而不是主页。

后端已启动并在端口 8081 上运行(通过邮递员发出外部请求并正常工作)

问题在于静态内容。

这是我的 proxy.config 文件代码(用于 nginx 的 ebextensions)

  /etc/nginx/conf.d/proxy.conf:
    owner: root
    group: root
    mode: "000644"
    content: |
        upstream nodejs {
            server 127.0.0.1:8081;
            keepalive 32;
        }

        server {
            root /var/app/current/react_build;
            listen 8080;

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

            location /api/ {
                proxy_pass  http://nodejs;
                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;
                client_max_body_size 10M;
            }

            location /static/ {
            }

            location / {
                try_files '' /index.html =404;
            }

            gzip on;
            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;
        }

  /opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
    owner: root
    group: root
    mode: "000755"
    content: |
      #!/bin/bash -xe
      rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
      service nginx stop 
      service nginx start

container_commands:
 removeconfig:
    command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"```

【问题讨论】:

    标签: node.js nginx amazon-elastic-beanstalk nginx-reverse-proxy nginx-config


    【解决方案1】:

    您尝试设置的 nginx 配置文件 (/etc/nginx/conf.d/proxy.conf) 适用于 Amazon Linux 1 (AL1)。

    对于 AL2,应使用以下方式提供 nginx 配置文件 (aws docs):

    • .platform/nginx/conf.d/

    或者如果你想覆盖主要的nginx 配置文件,使用

    • .platform/nginx/nginx.conf

    【讨论】:

    • @AlexMititelu 没问题。很高兴它解决了:-)
    猜你喜欢
    • 2015-05-18
    • 2017-12-30
    • 2020-10-23
    • 2019-06-12
    • 2019-03-25
    • 2015-06-29
    • 2016-09-25
    • 2015-04-27
    • 2017-10-03
    相关资源
    最近更新 更多