Dora
Dora

Reputation: 6970

aws elastic beanstalk nginx reverse proxy settings

I followed these two posts, but without any luck

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

I just started to play around with elastic beanstalk on hand.

Going with the basics, I started the server with port:8000 I want to do a reverse proxy so it would be listening to port 80 instead.

I did not do this with elb to start with because I want to get to know the basics more before going into elb

this is my index.js which runs the app

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}!`));

The above would work for sure if the url is http://eb_self_generated_url:8000 so I want to get it working with https://eb_self_generated_url

I was reading a few posts but none of them works though.

in my root, I created .ebextensions/nginx/conf.d/s_proxy.conf and inside I have

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$
}

I tried to zip the above and update / deploy but nothing changes

I also tried creating this under my app .ebextensions/proxy.conf

files: /etc/nginx/conf.d/: owner: root group: root content: | 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 / {
          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;
  }

still I got no luck with getting the reverse proxy to work.

Anyone able to give me a hand ?

Thank you for any help and suggestions.

Upvotes: 1

Views: 2393

Answers (1)

NaN140114
NaN140114

Reputation: 66

did you notice that you are using the port 8081 in nginx config instead your app's port (8000)?

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

Upvotes: 1

Related Questions