FB pixel

Nginx Configuration on VPS

2 16.08.2025

Nginx is a powerful and lightweight web server often used as a reverse proxy, load balancer, and static file host. For website owners, webmasters, and beginner developers, configuring Nginx on a VPS opens up broad possibilities for traffic management, page load acceleration, and security enhancement. In this article, we'll go through Nginx's installation, configuration, and optimization for the entire operation on a virtual server.

Server Preparation

The first step is to prepare the VPS for installation. Connect to the server via SSH with administrative rights. Update the system to the latest version:

sudo apt update && sudo apt upgrade

Make sure you have root access or the ability to use sudo. Check available disk space and determine which directory you will host your projects (usually /var/www is used).

Installing Nginx

Installing Nginx on popular Linux distributions (e.g., Ubuntu) is done with a single command:

sudo apt install nginx

After downloading and installing the package, check that the web server is running:

sudo systemctl status nginx

You will see a message indicating the service is active if everything is correct. Nginx immediately starts processing requests on port 80, and when accessing the VPS IP address in a browser, the start page will open.

Firewall Configuration

To make Nginx accessible from outside, ensure that HTTP and HTTPS traffic is allowed on the server. If using UFW, allow the necessary ports:

sudo ufw allow 'Nginx Full'

If the firewall is not yet enabled, activate it with sudo ufw enable and check the status: sudo ufw status.

Starting and Managing Nginx

Server processes are managed using the standard systemctl utility. Main commands:

  • Restart: sudo systemctl restart nginx
  • Stop: sudo systemctl stop nginx
  • Start: sudo systemctl start nginx
  • Configuration check: sudo nginx -t

These commands allow you to apply changes quickly and control Nginx's operation at any stage.

Configuring Virtual Hosts (Server Blocks)

The server blocks mechanism is used to allow each site to work with its domain (analogous to virtual hosts). Create a new project folder:

sudo mkdir -p /var/www/example.com/html

Then create a new configuration file at /etc/nginx/sites-available/example.com and add the block:

server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
nginx
КопіюватиРедагувати
location / {
try_files $uri $uri/ =404;
}
}

Activate the configuration with the command:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Then reload the service: sudo systemctl reload nginx.

Configuring HTTPS with Let's Encrypt

SSL certificate must be installed to ensure secure connections and browser trust. Install the Certbot utility:

sudo apt install certbot python3-certbot-nginx

Start automatic HTTPS configuration:

sudo certbot --nginx -d example.com -d www.example.com

Certbot will automatically modify the Nginx configuration, add a 301 redirect to HTTPS, and install the certificates. Add a cron job or rely on the built-in update system to enable automatic renewal.

Performance Optimization

It's useful for enabling compression and caching to speed up server response and reduce load. In the main configuration file /etc/nginx/nginx.conf, ensure the following directives are enabled:

gzip on;
gzip_types text/plain application/javascript text/css;

You can also set caching for static files:

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
access_log off;
}

This will speed up site loading and reduce repeat requests from users.

Configuration Testing and Nginx Reload

After making any changes, always check the configuration syntax:

sudo nginx -t

If everything is correct, restart or reload the server:

sudo systemctl reload nginx

Also, check its operation in a browser by entering the domain or IP address. Make sure the correct page opens and that static resources (images, styles, scripts) are working properly.

Conclusion

Configuring Nginx on a VPS is an important step toward ensuring a stable and fast website. Nginx can handle simple HTML pages as well as proxy traffic to backend applications such as FastAPI or Python. The server can easily adapt to projects of any level — from landing pages to complex corporate portals. The main point is to follow a logical configuration process, test the setup, and regularly update the system. This approach ensures high performance, security, and scalability for your project.

Learn more about how to purchase a domain, hosting, VPS, or dedicated server.

FAQ

How to Protect Nginx From DDoS Attacks?

You can use request rate limiting, connection limits per IP address, and the ngx_http_limit_req_module. It's also recommended that a firewall and CDN be used for traffic filtering.

How to Configure a Firewall for Nginx?

You can set it up to filter incoming traffic, block suspicious IP addresses, and limit the number of connections. Use iptables, ufw on Linux, or your operating system's built-in firewall.

How Do You Configure Nginx as a Reverse Proxy?

Nginx can be used as a reverse proxy to forward requests to other servers. To configure it, use the proxy_pass directive in the configuration file.