How to Set Up Nginx: A Complete Beginner’s Guide

Gaurav PatilGaurav Patil
3 min read

What is Nginx?

Nginx (pronounced "engine-x") is an open-source, high-performance web server. It can also be used for load balancing, HTTP cache, and reverse proxy. It has a non-threaded, event-driven architecture. It’s widely known for its speed, scalability, and ability to handle many concurrent connections efficiently.

Forward Proxy

A forward proxy is a server that sits between user devices and the internet. When a client requests a resource (e.g., a web page), the request goes to the proxy server first, which then forwards the request to the destination server. The response from the destination server is sent back to the proxy server, which then forwards it to the client.

Common use cases of forward proxy:

  • Anonymity: Clients can browse the web anonymously, as the destination server sees the proxy server's IP address instead of the client's.

  • Content Filtering: Organizations can restrict access to certain websites by controlling requests through the proxy.

  • Caching: Forward proxies can cache content to improve load times for frequently accessed resources.

Reverse Proxy

A reverse proxy is a server that accepts requests from the client, forwards the request to web servers, and returns to the client as if the proxy server had processed the request.

Common use cases of Reverse Proxy:

  • Load Balancing: Distributes client requests across multiple backend servers to ensure no single server is overwhelmed.

  • SSL Termination: Handles SSL/TLS encryption and decryption, reducing the load on backend servers.

  • Security: Acts as an additional layer of security, hiding the identity and structure of backend servers.

Why Nginx?

  • High Concurrency: Nginx can efficiently handle up to 10,000 concurrent requests, making it ideal for high-traffic websites.

  • Caching: It can cache HTTP requests, reducing server load and improving response times for frequently accessed content.

  • Reverse Proxy: Nginx acts as a reverse proxy, forwarding client requests to one or more backend servers, enhancing security and scalability.

  • Load Balancing: It distributes incoming traffic across multiple servers, ensuring optimal resource utilization and preventing overload on any single server.

  • API Gateway: Nginx can function as an API gateway, managing API requests and providing features like rate limiting and authentication.

  • Static File Serving and Caching: It can be used to serve static files like images, videos, etc.

  • Security: It is also used to handle SSL Certificates and secure HTTPS connections.

Install and Setup Nginx

Launch an Ubuntu container using Docker.

docker run -it -p 8080:80 ubuntu

This command will run the container in interactive mode.

Run the below commands to run the Nginx server in the container.

apt-get update
apt-get install nginx
nginx -v
nginx

Check if Nginx is running by visiting port 8080.

The configuration of Nginx can be found in the nginx.conf file.

ls etc/nginx/

View the file contents using the following commands.

apt-get install vim
cd etc
vim /etc/nginx.conf

Serve Static content with Nginx

Create a folder named website in /etc/nginx, then add a index.html file to it. Nginx will automatically detect this index.html.

cd /etc/nginx
mkdir website
cd website
touch index.html
vim index.html

Copy a simple HTML code shown below.

Save and exit.

Next, we need to update the nginx.conf file. Start by creating a backup of the current file.

cd /etc/nginx
mv nginx.conf nginx-backup.conf 
vim nginx.conf

Save and exit.

nginx -t 
nginx -s reload

Hence, here we have used nginx as a reverse proxy to serve a static webpage.

Thanks!

10
Subscribe to my newsletter

Read articles from Gaurav Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gaurav Patil
Gaurav Patil