To set up Nginx on Linux, install it with sudo apt install nginx (or dnf on Fedora and RHEL), allow web traffic in the firewall, put your site’s files in /var/www/<your-domain>, create a server block that points to them, check it with sudo nginx -t, reload Nginx, and add HTTPS with Certbot. The whole process takes about 20 minutes on a fresh server.
Nginx (pronounced “engine-x”) is one of the most widely used web servers. It serves static websites very quickly, and it’s just as often used as a reverse proxy in front of applications written in Node.js, Python, PHP, or anything else. This guide covers installation, the configuration files, hosting a site, free HTTPS, reverse proxying, a few tuning and security settings, and troubleshooting.
You’ll need a Linux server with a user that has sudo rights and, for HTTPS, a domain name whose DNS A record points to the server’s IP address. If you don’t have a server yet, see Learn VPS: From Server Basics to Your First Deployment and Linux Server: A Beginner’s Guide to Server Administration.
Step 1: Install Nginx
On Debian and Ubuntu:
sudo apt update
sudo apt install nginx
On Fedora, RHEL, Rocky, and AlmaLinux:
sudo dnf install nginx
sudo systemctl enable --now nginx
On Debian and Ubuntu, Nginx starts automatically after installing. Check it:
sudo systemctl status nginx
nginx -v
Step 2: Open the Firewall
On Ubuntu with UFW, Nginx adds ready-made profiles:
sudo ufw app list
sudo ufw allow 'Nginx Full' # ports 80 (HTTP) and 443 (HTTPS)
sudo ufw status
On Fedora and RHEL with firewalld:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Now open http://<your-server-ip> in a browser. You should see the “Welcome to nginx!” page (Fedora and RHEL show their own test page).
Step 3: Learn the File Layout
| Path | What it’s for |
|---|---|
/etc/nginx/nginx.conf | Main configuration (global settings) |
/etc/nginx/sites-available/ | One file per site (Debian, Ubuntu) |
/etc/nginx/sites-enabled/ | Links to the sites that are switched on (Debian, Ubuntu) |
/etc/nginx/conf.d/*.conf | Site and extra configuration files (all distributions; the only place on Fedora and RHEL) |
/etc/nginx/snippets/ | Reusable pieces you can include |
/var/www/html | Default web root on Debian and Ubuntu (/usr/share/nginx/html on Fedora and RHEL) |
/var/log/nginx/access.log | Every request |
/var/log/nginx/error.log | Errors, the first place to look when something breaks |
Nginx configuration is made of directives (name value;, always ending in a semicolon) grouped in blocks ({ }). The blocks you’ll use most are http, server (one website), and location (rules for a set of URLs).
Step 4: Create a Website
Create a folder for the site and a test page. Replace example.com with your domain throughout:
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
echo '<h1>Hello from Nginx</h1>' > /var/www/example.com/html/index.html
Nginx runs as the www-data user (nginx on Fedora and RHEL), which only needs to read these files. The default permissions (755 for folders, 644 for files) allow that. For more on ownership and permissions, see Linux User and Permission Management.
Step 5: Create a Server Block
A server block tells Nginx which domain it answers for and where the files are. On Debian and Ubuntu, create a file in sites-available:
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}
What it does:
listen 80andlisten [::]:80accept HTTP over IPv4 and IPv6.server_namelists the domains this block serves.rootis the folder the files come from.try_files $uri $uri/ =404serves the requested file or folder, or returns a 404 if neither exists.
Switch the site on by linking it into sites-enabled:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
On Fedora and RHEL, save the same block as /etc/nginx/conf.d/example.com.conf instead; there’s no linking step.
Always test before reloading. A typo in any file stops Nginx from reloading:
sudo nginx -t
sudo systemctl reload nginx
nginx -t should print syntax is ok and test is successful. Visit http://example.com, and you should see your test page.
The default site keeps answering requests for any other name or the bare IP address. To turn it off on Debian and Ubuntu: sudo rm /etc/nginx/sites-enabled/default, then test and reload.
Step 6: Add Free HTTPS with Let’s Encrypt
Certbot gets a free certificate from Let’s Encrypt and edits your server block to use it:
sudo apt install certbot python3-certbot-nginx # Fedora/RHEL: sudo dnf install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
Certbot asks for an email address (for expiry warnings) and offers to redirect HTTP to HTTPS; accept it. It adds a listen 443 ssl section and the certificate paths to your server block.
Let’s Encrypt certificates last 90 days, and Certbot installs a timer that renews them automatically. Confirm renewal works:
sudo certbot renew --dry-run
Your DNS must point to the server, and port 80 must be open, for Certbot’s check to pass. Certbot’s developers also publish a snap package (sudo snap install --classic certbot); either works.
Step 7: Use Nginx as a Reverse Proxy
When your site is an application (a Node.js, Python, or Go server listening on, say, port 3000), Nginx sits in front of it, handles HTTPS, and forwards requests. Replace the location / block with:
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;
}
The proxy_set_header lines pass the original domain, visitor IP address, and protocol to your app, which would otherwise see every request coming from 127.0.0.1 over plain HTTP. Keep the application listening only on 127.0.0.1 so it can’t be reached around Nginx.
On Fedora and RHEL, SELinux blocks Nginx from connecting to other ports until you allow it:
sudo setsebool -P httpd_can_network_connect 1
Step 8: Useful Settings
Put these inside your server block (or in the http block of nginx.conf to apply them to every site).
Hide the Nginx version in error pages and headers:
server_tokens off;
Allow larger uploads (the default is 1 MB, which causes 413 Request Entity Too Large):
client_max_body_size 20M;
Compress text responses. Debian and Ubuntu already set gzip on; in nginx.conf, but only for HTML. Add other types:
gzip_types text/css application/javascript application/json image/svg+xml;
Let browsers cache static files:
location ~* \.(css|js|png|jpg|jpeg|gif|webp|svg|ico|woff2)$ {
expires 30d;
add_header Cache-Control "public";
}
Add basic security headers:
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
One catch: a location block that has its own add_header lines doesn’t inherit the ones from the server block. Repeat the headers there, or keep all headers at one level.
Redirect www to the bare domain (after HTTPS is set up), as its own server block:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 301 https://example.com$request_uri;
}
Remove www.example.com from the main block’s server_name when you do this. After any change, run sudo nginx -t and sudo systemctl reload nginx.
Everyday Nginx Commands
| Task | Command |
|---|---|
| Test the configuration | sudo nginx -t |
| Apply changes without downtime | sudo systemctl reload nginx |
| Restart completely | sudo systemctl restart nginx |
| Start at boot | sudo systemctl enable nginx |
| Show the full combined configuration | sudo nginx -T |
| Follow the error log | sudo tail -f /var/log/nginx/error.log |
| Follow the access log | sudo tail -f /var/log/nginx/access.log |
Troubleshooting Common Nginx Errors
| Error | Likely cause | Fix |
|---|---|---|
nginx -t fails | Missing ;, unmatched }, or a typo in a directive | Read the file and line number in the message |
bind() to 0.0.0.0:80 failed (Address already in use) | Another web server (often Apache) is using port 80 | Find it with sudo ss -tlnp 'sport = :80', then stop or remove it |
| Default page instead of your site | The site isn’t enabled, or server_name doesn’t match the domain | Check the link in sites-enabled and the server_name line |
403 Forbidden | No index file, or Nginx can’t read the files | Check the file exists and namei -l /var/www/example.com/html/index.html for permissions |
404 Not Found | Wrong root path | Compare root with where the files are |
502 Bad Gateway | The app behind proxy_pass isn’t running, or SELinux blocks it | Start the app; check the port; on RHEL run the setsebool command above |
504 Gateway Timeout | The app is too slow to answer | Fix the slow request or raise proxy_read_timeout |
413 Request Entity Too Large | Upload bigger than client_max_body_size | Raise the limit |
The error log almost always names the problem, so check sudo tail -n 50 /var/log/nginx/error.log first. For watching the server’s CPU, memory, and traffic once your site is live, see Linux Server Monitoring: Check CPU, Memory, Disk, and Network.
Conclusion
Running a site on Nginx comes down to a short routine: install it, open ports 80 and 443, create a folder and a server block, test with nginx -t, reload, and add HTTPS with Certbot. From there, a proxy_pass block puts any application behind Nginx, and a few lines add compression, caching, and security headers. Keep the server itself patched and locked down, too; see Linux Server Security: 10 Best Practices to Protect Your Server.
Frequently Asked Questions
What’s the difference between Nginx and Apache?
Both are mature, free web servers. Nginx handles many simultaneous connections with little memory and is a popular reverse proxy. Apache supports per-folder .htaccess files, which makes it common on shared hosting. Either works well for most sites.
What’s the difference between reload and restart?
reload reads the new configuration and switches to it without dropping connections. restart stops and starts Nginx, briefly interrupting service. Use reload for config changes.
Can one Nginx server host several websites?
Yes. Create one server block per domain, each with its own server_name and root. Nginx picks the block whose server_name matches the requested domain.
Is Nginx free?
Yes. The open-source version is free. NGINX Plus is a paid commercial edition with extra features and support.
Do I need Nginx in front of a Node.js or Python app?
It isn’t required, but it’s common practice. Nginx handles HTTPS, compression, static files, and slow clients efficiently, which lets your application focus on its own work.
Related Reading
Want to keep learning? These related guides pair well with this post:
