Nginx — High-Performance Web Server & Reverse Proxy
Install, configure, and operate Nginx on Linux for serving static sites, proxying application servers, and managing SSL/TLS certificates.
What is Nginx?
Nginx is an event-driven, asynchronous HTTP web server, reverse proxy, and load balancer. Because of its low memory footprint and high concurrency capabilities, it powers over a third of all websites globally.
Installation
sudo apt update
sudo apt install -y nginx
sudo systemctl enable --now nginxVerify that Nginx is running and listening on port 80:
systemctl status nginxBasic Reverse Proxy Configuration
Create a virtual host configuration in /etc/nginx/sites-available/app.conf:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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;
}
}Enable the site by symlinking it into sites-enabled:
sudo ln -s /etc/nginx/sites-available/app.conf /etc/nginx/sites-enabled/Testing and Reloading
Always test your configuration syntax before restarting or reloading Nginx:
sudo nginx -tIf the syntax test passes (syntax is ok), reload the daemon without dropping active TCP connections:
sudo systemctl reload nginxCombine Nginx with Certbot (sudo certbot --nginx -d app.example.com) to automatically generate TLS certificates and redirect all HTTP traffic to HTTPS.