Setup Nginx for Your NodeJS Server on EC2
Expose our NodeJS server to receive HTTP requests
While I was in college and was working with NodeJS, a problem I often faced during hackathons, college projects, etc was hosting my project on the web. I had everything working locally but the feeling of demoing on the examiner’s device gave altogether a different kick.
Hence in this article, we’ll be discussing how we can expose our NodeJS server to receive HTTP requests over the internet in a stable reliant way, using nginx reverse proxy.
As a prerequisite to this article you should have a NodeJS server running on an EC2 instance. To set this up you can follow my previous article in the series.
Why Nginx?
We’ll be using Nginx as a reverse proxy, to abstract our backend configurations from the outside world. All kinds of requests being sent to our server will have to pass through this reverse proxy before reaching the server.
There are several benefits of having a reverse proxy, some of them being
- Security: Reverse proxies play a key role in building a zero trust architecture for organizations — that secures sensitive business data and systems. You can whitelist or blacklist specific endpoints, control resource limits, allow only specific HTTP methods, add common headers to all requests, etc via a reverse proxy.
- Scalability: Using a reverse proxy the number of servers can be scaled up and down depending on the fluctuations in traffic volume, keeping all these operations abstracted from the client.
- Configuration: We can configure things like timeout period after which the server will show a 503 error, for specifying the HTTP protocol that the service will be using.
- Caching: Businesses that serve a lot of static content like images and videos can set up a reverse proxy to cache some of that content.
- Logging: Nginx can be used to log incoming headers, status codes returned, etc for all incoming requests.
There are lots of other use cases for reverse proxy, but that will be out of scope for this article.
Steps to install Nginx
Run the following commands to install Nginx
sudo apt update
sudo apt install nginx
To verify if the installation was successful, type
systemctl status nginx
You should see the following output
As confirmed by this output, the service has started successfully. However, the best way to test this is to actually request a page from Nginx.
You can access the default Nginx landing page to confirm that the software is running properly by navigating to your server’s IP address.
So you’ll see the following page by navigating to
http://<YOUR_SERVER_IP>/
Configure traffic redirection to express server
When we type our IP in the browser, we see the Nginx default page, but we want to access our express API when we query our server’s IP.
For this, we’ll have to move to the following directory, where all our Nginx configuration resides.
cd /etc/nginx
There is a file named nginx.conf
in this directory, the entire Nginx configuration is present in this file.
Inside the http
block of this file, there is a line
include /etc/nginx/conf.d/*.conf;
This line means, to include any configuration written in any file with .conf
an extension, in the /etc/nginx/conf.d/
directory. So this directory is where we’ll write our configuration.
Create a file named configuration.conf
(file can be named anything ending with .conf
).
and add the following server block to the file
server {server_name <SERVER_IP>;location / {
proxy_pass http://127.0.0.1:3000;
}}
Note: we mentioned port as 3000 because in the previous article, we configured a NodeJS server running on PORT 3000, so make sure you keep the port as the post on which your server is running.
Now to test if our Nginx configuration is syntactically correct we run the following command.
sudo nginx -t
This will give us the following output.
Now, since our configuration is valid, we can restart our Nginx reverse proxy to see our configuration in action.
sudo service nginx restart
After this, if you go to the browser and type your server IP you’ll see the following output.
Note: You’ll get this output in case you followed along with the previous article. In case you have some other Express server running you’ll see the return value of its default route.
Conclusion
By following along with this article your NodeJS server will be exposed to the internet and you’ll be able to receive HTTP requests over it. But this is still not the ideal way to set up your remove server, since HTTP is not a secure protocol.
Hence in the next article in the series, we’ll see how we can set up Certbot on the ec2 instance and enable HTTPS for incoming requests to our server.