How to Install Nginx: Web Server and Reverse Proxy
What is Nginx and how to install it on Ubuntu: server block setup, a reverse proxy for Node.js, free Let's Encrypt SSL and fixes for common errors.

Contents
Nginx is one of the most widely used web servers in the world. It serves static files quickly, handles thousands of simultaneous connections with little memory and is used above all as a reverse proxy: it greets visitors with your domain and SSL certificate and forwards their requests to the Node.js, Python or PHP application running behind it. Installing Nginx takes a few commands; what you really need to learn is how its configuration files work.
This guide covers how to install Nginx on an Ubuntu server, configure server blocks, publish an application through a reverse proxy, get a free SSL certificate with Let's Encrypt and fix the most common errors.
What is Nginx and what is it used for?
Nginx is used for three main jobs:
- Web server: Serves static files such as HTML, CSS, JavaScript and images directly.
- Reverse proxy: Forwards incoming requests to one or more applications behind it. It takes over jobs such as SSL, compression and caching from the application.
- Load balancer: Spreads traffic across several copies of the same application.
| Feature | Nginx | Apache | LiteSpeed |
|---|---|---|---|
| Architecture | Event-driven, low memory | Process/thread-based | Event-driven |
| .htaccess support | No | Yes | Yes (Apache-compatible) |
| Typical use | VPS, reverse proxy, static sites | Classic PHP sites | Shared hosting, WordPress |
| License | Open source | Open source | Commercial (OpenLiteSpeed is open source) |
If you manage your own VPS, Nginx is the most practical choice for most projects. On shared hosting, the provider manages the web server; Nodesty web hosting plans, for example, run on LiteSpeed.
Installing Nginx on Ubuntu and Debian
After connecting to your server over SSH:
sudo apt update
sudo apt install nginx
Nginx starts automatically once the installation finishes. Check its status:
systemctl status nginx
Allow web traffic through the firewall. The Nginx Full profile opens ports 80 (HTTP) and 443 (HTTPS) together:
sudo ufw allow 'Nginx Full'
If you open http://server-ip in your browser and see the "Welcome to nginx!" page, the installation is done. We cover ports and firewall settings in detail in What is a port?
On AlmaLinux and Rocky Linux, you install it with sudo dnf install nginx and sudo systemctl enable --now nginx; for the firewall, run sudo firewall-cmd --permanent --add-service=http --add-service=https followed by sudo firewall-cmd --reload.
Nginx configuration files
| Path | What it contains |
|---|---|
/etc/nginx/nginx.conf | Main configuration; you usually don't need to change it |
/etc/nginx/sites-available/ | One configuration file (server block) per site |
/etc/nginx/sites-enabled/ | Shortcuts (symlinks) to enabled sites |
/var/www/ | The usual folder for site files |
/var/log/nginx/ | access.log and error.log |
On Ubuntu and Debian, sites are defined in the sites-available folder and enabled by adding a shortcut to the sites-enabled folder. AlmaLinux doesn't have these folders; site files go directly under /etc/nginx/conf.d/ with a .conf extension.
Make these two commands a habit after every change:
sudo nginx -t
sudo systemctl reload nginx
nginx -t tests the configuration and shows any errors with their line numbers. reload loads the new configuration without dropping open connections.
Publishing your first site: server blocks
Make sure your domain's A record points to your server's IP address; we explain this in What is DNS? Then create a folder for the site:
sudo mkdir -p /var/www/yourdomain.com
echo "Hello Nginx" | sudo tee /var/www/yourdomain.com/index.html
Create the file /etc/nginx/sites-available/yourdomain.com:
server {
listen 80;
listen [::]:80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Enable the site and reload Nginx:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
server_name tells Nginx which site an incoming request belongs to. To host several sites on the same server, just create a separate file for each domain.
Reverse proxy: publishing a Node.js application
A Node.js, Python or other application usually runs on a port such as 3000. Instead of exposing that port to the internet, have the application listen only on 127.0.0.1 and put Nginx in front of it:
server {
listen 80;
listen [::]:80;
server_name app.yourdomain.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
proxy_pass: The address of the application the request is forwarded to.X-Real-IPandX-Forwarded-For: Let the application see the visitor's real IP address. Without these headers, the application sees every request as coming from127.0.0.1.UpgradeandConnection: Required for WebSocket connections to work. Don't skip these lines if you use real-time libraries such as Socket.IO.
To keep your application running, you can use pm2 or systemd; we cover both in our guide to hosting a Discord bot 24/7. If your application runs in Docker, publish the port as 127.0.0.1:3000:3000; we explain why in our Docker installation guide.
Free SSL with Let's Encrypt
Certbot obtains a Let's Encrypt certificate, updates your Nginx configuration and sets up automatic renewal:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Certbot reaches your server on port 80 to verify the domain. So before running the command, make sure your DNS records point to the server and port 80 is open. The redirect from HTTP to HTTPS is also configured during setup.
To test that automatic renewal works:
sudo certbot renew --dry-run
We explain how SSL works and the types of certificates in What is an SSL certificate?
Useful settings
A few common settings you can add inside a server block:
client_max_body_size 50M;
gzip on;
gzip_types text/css application/javascript application/json image/svg+xml;
location ~* \.(css|js|png|jpg|jpeg|webp|svg|woff2)$ {
expires 30d;
add_header Cache-Control "public";
}
client_max_body_size: The largest file size that can be uploaded. The default is 1 MB; larger uploads return a413error.gzip: Compresses text-based files so pages load faster.expires: Lets browsers cache static files.
To hide the server version in response headers, enable the server_tokens off; line in the http block of /etc/nginx/nginx.conf.
Common Nginx errors
- 502 Bad Gateway: Nginx can't reach the application behind it. Check that the application is running and that the port in the
proxy_passline is correct. - 504 Gateway Timeout: The application is responding too slowly. You can raise
proxy_read_timeoutfor long-running operations, but the real fix is to solve the application's slowness. - 413 Request Entity Too Large: The uploaded file is larger than
client_max_body_size. - 403 Forbidden: Nginx can't access the files, or there is no
indexfile in the folder. Check the file permissions and therootpath. - The default page opens instead of your domain: The
server_nameline doesn't match the domain, or the site isn't linked in thesites-enabledfolder.
In every case, the first place to look is the error log:
sudo tail -f /var/log/nginx/error.log
Nodesty servers for Nginx
To install and manage Nginx yourself, you need a server with root access. Nodesty AMD Ryzen 9 9950X VPS plans respond quickly to your web applications with high single-core speeds, while AMD EPYC 7763 VPS plans offer plenty of RAM for many sites and databases. Both come with NVMe storage, L3–L7 nShield DDoS protection and free daily backups kept for 7 days. If you just want to publish a website without managing a server, our web hosting plans with LiteSpeed are the more practical choice.
Frequently Asked Questions
Should I use Nginx or Apache?
On a new VPS, Nginx is the better starting point in most cases. It handles a large number of simultaneous connections with little memory and is very widely used as a reverse proxy. Apache makes per-folder configuration easy thanks to .htaccess support; if older PHP applications rely on that file, Apache will make your life easier.
What is a reverse proxy?
A reverse proxy is a server that receives a visitor's request and forwards it to another application running behind it. For example, Nginx handles your domain and SSL on ports 80 and 443 and forwards the request to your Node.js application on port 3000. That way your application is never exposed directly to the internet.
What causes a 502 Bad Gateway error?
Nginx returns 502 when it can't reach the application it forwards the request to. The most common causes are the application not running, forwarding to the wrong port or the application crashing. Check that the application is running and that the port number in the proxy_pass line is correct; the details are in /var/log/nginx/error.log.
Can I host more than one site on a single server?
Yes. You create a separate server block file for each domain. Nginx looks at the domain of the incoming request and serves the right site. Certbot can also issue a separate SSL certificate for each site with a single command.
How long is a Let's Encrypt certificate valid?
Let's Encrypt certificates are short-lived and need to be renewed regularly. Certbot adds an automatic renewal timer during installation; you can test that renewal works with the sudo certbot renew --dry-run command.
Can I configure Nginx on Nodesty web hosting?
Our web hosting plans run on the LiteSpeed web server and we manage the server; you configure things through the control panel and .htaccess. If you want to install and manage Nginx yourself, you need a VPS with root access.
About the author
Nodesty Team · Infrastructure & Support
The Nodesty team runs our servers, network and DDoS protection day to day and answers customer support requests around the clock. These guides come from that work.
Related articles

How to Upgrade Ubuntu Server to the Next LTS Release
How to upgrade an Ubuntu server to the next LTS release: backups, preparation, do-release-upgrade, SSH access, post-upgrade checks and a clean install.

What Is a Load Balancer and How Does It Work?
What is a load balancer and what does it do? Balancing methods, health checks, session handling, load balancing with Nginx, HAProxy and when you need one.

How to Deploy a Laravel App to a VPS
How to deploy Laravel to a server: PHP-FPM, Composer, .env, the database, file permissions, Nginx, queue workers, the scheduler, SSL and shipping updates.