Search code examples
htmlnode.jsamazon-web-servicesnginxamazon-ec2

504 Gateway Time-out AWS


I made an AWS architecture like this : 3 subnets : public then private 1 then private 2. I put a web server (EC2) using nginx in private 1 and an app server (EC2) (node.js) in private 2. I also added 2 application load balancers : one internet facing in the public subnet for the web server and one internal in subnet private 2 for the app server. i succeded in hosting the website and it works online. the only problem is that i have a forum that needs to be sent to the app server using a button submit. when i press submit a 504 Gateway Time-out appears. I assume i have a problem in my nginx.conf : i added these two :

    location /api/ {
        proxy_pass http://DNS-NAME:80/;
    }

    location /submit {
        proxy_pass http://DNS-NAME:3000;
        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_set_header X-Forwarded-Proto $scheme;
    }

The security groups are correct because they worked for other people, also i think the load balancer himself isn't getting the traffic because when i decreased the waiting time for the LB i noticed the waiting time didn't change. I believe it's either in nginx.conf or they way i'm sending the submit from the html file : form action="/submit" method="post".

I tried changing the rules and listeners and target groups but didn't help.


Solution

  • Hey guys i found the solution. first the api block is useless and the proxy in the submit file should forward to port 80 not 3000 because the ALB accepts HTTP requests and then he forwards it to the app server on port 3000 the file should be like this location /submit { proxy_pass DNS-NAME:80; 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_set_header X-Forwarded-Proto $scheme; }