AWS Elastic beantalk Nginx反向代理设置

时间:2019-03-08 19:02:21

标签: amazon-web-services nginx reverse-proxy amazon-elastic-beanstalk

我关注了这两个帖子,但没有任何运气

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-nginx.html

https://davidojeda.mx/blog/2018/01/11/extend-nginx-config-on-aws-elasticbeanstalk

我刚开始玩elastic beanstalk

根据基础知识,我使用port:8000启动了服务器 我想做一个反向代理,所以它将监听端口80。

一开始我并没有从elb开始做这件事,因为我想在进入elb之前更加了解基础知识

这是我运行应用程序的index.js

const express = require('express');
const app = express();
const port = 8000;

app.get('/', async (req, res) => {
    return res.json({ status: true });
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

上面的方法可以确保url是否为http://eb_self_generated_url:8000,因此我想使其与https://eb_self_generated_url一起使用

我正在阅读一些帖子,但它们均不起作用。

在我的根目录中,我创建了.ebextensions/nginx/conf.d/s_proxy.conf,并且在其中拥有

upstream nodejs {
    server 127.0.0.1:8081;
    keepalive 256;
}

server {
    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 / {
        # this is actually what need to be changed
        # I tried changing from http://nodejs to http://localhost:8000 at server which then will make the reverse proxy work
        proxy_pass  http://localhost:8000;
        proxy_set_header   Connection "";
        proxy_http_version 1.1;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP   $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    gzip on;
    gzip_comp_level 4;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml appl$
}

我尝试压缩上面的内容和update / deploy,但没有任何变化

我还尝试在我的应用.ebextensions/proxy.conf下创建该文件

个文件:   /etc/nginx/conf.d/:     所有者:root     组:根     内容:       上游nodejs {         服务器127.0.0.1:8081;         保持活动256;       }

  server {
      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 / {
          proxy_pass  http://localhost:8000;
          proxy_set_header   Connection "";
          proxy_http_version 1.1;
          proxy_set_header        Host            $host;
          proxy_set_header        X-Real-IP $remote_addr;
          proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      }

      gzip on;
      gzip_comp_level 4;
      gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml appl$

      # Include the Elastic Beanstalk generated locations
      include conf.d/elasticbeanstalk/01_static.conf;
      include conf.d/elasticbeanstalk/healthd.conf;
  }

我仍然没有让反向代理工作的运气。

有人能帮我吗?

感谢您的帮助和建议。

1 个答案:

答案 0 :(得分:1)

您是否注意到您正在nginx config中使用端口8081而不是应用程序的端口(8000)?

upstream nodejs {
    server 127.0.0.1:8081;
    keepalive 256;
}