I have a golang web server running on AWS elastic beanstalk in this machine: Go 1 running on 64bit Amazon Linux 2;
The error occurs when I make a request to the server and it executes several processes, so it takes around 90 seconds to complete the entire process, so after 60 seconds the AWS beanstalk server ends the client connection and returns the following error, although the process ends completely after 90 seconds.
It sends the following error:
<html>
<head>
<title>504 Gateway Time-out</title>
</head>
<body>
<center>
<h1>504 Gateway Time-out</h1>
</center>
</body>
</html>
Source application files:
.
├── application.go
├── cron.yaml
└── public
└── index.html
When I tested it on my local machine It works well and It take 90 seconds, only when It runs on Beanstalk has this issue.
How can I fix it ??
504 Gateway Timeout indicates that the nginx proxy is waiting too long for a response from the upstream app. If this happens for an endpoint that usually returns after a few seconds, it is very likely that the nginx proxy is trying to reach a port that your app is not listening on (or that the app has crashed).
By default, Elastic Beanstalk configures the nginx proxy to forward requests to your application on port 5000. You can override the default port by setting the PORT environment property to the port on which your main application listens. More info: AWS Reverse Proxy Docs
Make sure that your application code listens on the right port.
http.ListenAndServe(":5000", nil);
Another reason for this could be a crash in the app code. Check the last 100 log lines in Elastic Beanstalk. You can also retrieve the logs by SSH'ing into your server and running cat /var/log/eb-engine.log.
Reverse Proxy
On a separate note, it looks like you're trying to statically host /public/index.html. You can do this in 2 separate ways.
Using Go (not recommended)
http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":3000", nil)
Using Nginx (recommended)
By extending your nginx config you can use the proxy server to statically host files and redirect routes before they hit your server application.
~/workspace/my-app/
|-- .platform
| `-- nginx
| `-- conf.d
| `-- myconf.conf
Related
This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 17 days ago.
I have started a Flask Webserver on an Amazon Linux 2 EC2 instances
(venv) [ec2-user#ip-10-0-1-63 microblog]$ flask run
* Serving Flask app 'microblog.py'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
When i try to access the Web either via the below Public V4 DNS or Public V4 IP i get "Site cannot be reached"
http://34.228.161.61:5000
http://34.228.161.61:5000/index
https://ec2-34-228-161-61.compute-1.amazonaws.com:5000
https://ec2-34-228-161-61.compute-1.amazonaws.com:5000/index
I have successfully launched an Apache Web Server into the same EC2 & VPC instances and have no issues.
Also running curl from the same server i launched flask returns the contents
[ec2-user#ip-10-0-1-63 ~]$ curl http://localhost:7999
Hello, World![ec2-user#ip-10-0-1-63 ~]$
(tried a range of other ports also)
Any clues on what to do to get it working from my Chrome/Safari browser?
Tried so far
Ensured EC2 was talking to the web
Ensured VPC had route to the public internet
Ensured other webservers could be launched successfully from the same EC2 instance
Might be your application is not allowed for the outside world (0.0.0.0). Please check the running port in your system. You can use the below command to check:
sudo netstat -tulpn | grep LISTEN
check if 0.0.0.0: in your case, 0.0.0.0:5000 is showing in output or not.
Then try to run with below command:
flask run --host=0.0.0.0
Let me know, if that works. #Cloudkaramchari
I have Django hosted with Nginx on DigitalOcean. Now I want to install Plausible Analytics. How do I do this? How do I change the Nginx config to get to the Plausible dashboard with mydomain/plausible for example?
Setup plausible by either running the software directly or in a docker container - let's say it runs on port 8080
Then in your nginx.conf - you should have a server block for your domain
Within that add a location block with the path you want plausible on and add a proxy pass directive to forward the requests to localhost:8080
Monitor access.log and error.log to debug any issues that may happen
My Django application run well for a while, then I got 502 Bad Gateway, after a few hours, I am unable to ping the domain and use SSH to connect my server(from Amazon Lightsail). My other application served by ngnix was also not available then. While if I didn't start the Django application, ther application served by ngnix would run steadily. So I guess it is the error of my Django application crashed ngnix and the server.
After rebooting the server for serveral times, the server seems recovered then I can ping the domain and use SSH to connect the server. But after a while, the same problem would occurs again. I wonder how to fix the problem.
Some diagnostic information since the start of the Django application to the end of the Nginx server provided below.
The RAM usage is high during the process.
The uwsgi log. https://bpa.st/FP7Q
The Nginx error log. https://bpa.st/35EQ
I bought a domain name using amazon Route 53, created an elastic IP address and attached it to my Amazon EC2 instance running MEAN bitnami stack, then I created an S3 bucket for static web hosting "redirecting all requests" to the www.domainname.com.
However, when I go to my root domain the page loaded is the Bitnami Congratulations page: You are now running Bitnami MEAN 3.2.11-0 in the Cloud. I created a folder, uploaded my MEAN angular 2/express app and did node server.js and I can only access my web app on mydomain.com:8080 but anything else redirects to the Bitnami congratulations page. Which setting is it that is doing this? Is it an amazon setting? A Bitnami setting? or something in my server side code.
The only place I mention 8080 in my server code is here and it prints API running on 8080 when I run the server:
const port = process.env.PORT || '8080';
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log(`API running on: ${port}` + ' or ' + process.env.PORT));
You have Apache running on your MEAN Stack. Apache is running on port 80/443 and, therefore, whenever you access your domain at port 80, Apache handle the request and shows you the "Bitnami Welcome Page".
In order to make your Angular/express application available at port 80 you need to configure Apache to redirect the requests to port 3000 (or port 8080, basically the port where your Express application is running). You could add under <VirtualHost _default_:80> and <VirtualHost _default_:443> sections in the file /opt/bitnami/apache2/conf/bitnami/bitnami.conf the line below:
ProxyPass /bitnami !
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
After that, you will need to restart Apache running:
sudo /opt/bitnami/ctlscript.sh restart apache
You can find more information at:
https://docs.bitnami.com/aws/infrastructure/mean/
Bitnami by default launches with an http config here: root/opt/apache2/conf/httpd.conf. The apache2 server pointed at a root directory containing html with an index.html that contains its Congratulations page. Using sudo /opt/bitnami/ctlscript.sh stop killed this server and all other services it had running by default on startup so when I type node server.js all traffic now goes to my Angular 2 app.
Use NGINX to reverse proxy all trafic from http://www.domainname.com to address where your node server is running ( http://localhost:8080).
Here is the sample NGINX configuration for such a use case.
To install NGINX follow one of tutorials on digitalocean.com depending upon your server OS.
A lot of Django app deployments over Amazon's EC2 use HTTP servers NGINX and Gunicorn.
I was wondering what they actually do and why both are used in parallel. What is the purpose of running them both in parallel?
They aren't used in parallel. NGINX is a reverse proxy. It's first in line. It accepts incoming connections and decides where they should go next. It also (usually) serves static media such as CSS, JS and images. It can also do other things such as encryption via SSL, caching etc.
Gunicorn is the next layer and is an application server. NGINX sees that the incoming connection is for www.domain.com and knows (via configuration files) that it should pass that connection onto Gunicorn. Gunicorn is a WSGI server which is basically a:
simple and universal interface between web servers and web applications or frameworks
Gunicorn's job is to manage and run the Django instance(s) (similar to using django-admin runserver during development)
The contrast to this setup is to use Apache with the mod_wsgi module. In this situation, the application server is actually a part of Apache, running as a module.
Nginx and Gunicorn are not used in parrallel.
Gunicorn, is a Web Server Gateway Interface (WSGI) server
implementation that is commonly used to run Python web applications.
NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.
Nginx responsible for serving static content, gzip compression, ssl,
proxy_buffers and other HTTP stuff.While gunicorn is a Python HTTP server that interfaces with both nginx and your actual python web-app code to serve dynamic content.
The following diagrams shows how nginx and Gunicorn interact.