What is Nginx?
Nginx is a high-performance web server and reverse proxy, widely used for hosting websites, load balancing, and serving static files.
Installation
sudo apt update && sudo apt install nginx
sudo systemctl start nginx && sudo systemctl enable nginx
Site Configuration
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.html index.php;
location ~* .(css|js|jpg|png|gif|ico)$ {
expires 30d;
}
location ~ .php$ {
include fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}
Enable and Test
sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
Reverse Proxy
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}