Docs / Web Servers / Configuring Nginx for WebSocket Proxying

Configuring Nginx for WebSocket Proxying

By Admin · Mar 18, 2026 · Updated Apr 23, 2026 · 4 views · 3 min read

Getting nginx right from the start saves hours of debugging later. In this comprehensive guide, we'll cover everything from initial setup to production-ready configuration, including websocket and proxy considerations.

Prerequisites

  • A VPS running Ubuntu 22.04 or later (2GB+ RAM recommended)
  • Root or sudo access to the server
  • Basic familiarity with the Linux command line
  • A registered domain name (for public-facing services)

Server Installation

The websocket component plays a crucial role in the overall architecture. Understanding how it interacts with nginx will help you make better configuration decisions.


# /etc/nginx/sites-available/nginx.conf
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        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;
    }
}

Each line in the configuration serves a specific purpose. The comments explain the reasoning behind each setting, making it easier to customize for your specific use case.

Security Implications

When scaling this setup, consider vertical scaling (adding more RAM/CPU) first, as it's simpler to implement. Horizontal scaling adds complexity but may be necessary for high-traffic applications.

Virtual Host Configuration

The nginx configuration requires careful attention to resource limits and security settings. On a VPS with limited resources, it's important to tune these parameters according to your available RAM and CPU cores.


# Test and reload Nginx configuration
sudo nginx -t
sudo systemctl reload nginx

# Check access and error logs
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

This configuration provides a good balance between performance and resource usage. For high-traffic scenarios, you may need to increase the limits further.

SSL/TLS Setup

The websocket component plays a crucial role in the overall architecture. Understanding how it interacts with nginx will help you make better configuration decisions.


# Install Nginx with modules
sudo apt update
sudo apt install -y nginx

# Enable the site
sudo ln -s /etc/nginx/sites-available/nginx.conf /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo systemctl restart nginx

Note that file paths may vary depending on your Linux distribution. The examples here are for Debian/Ubuntu; adjust paths accordingly for RHEL/CentOS-based systems.

Configuration Options

For production deployments, consider implementing high availability by running multiple instances behind a load balancer. This approach provides both redundancy and improved performance under heavy load.

  • Start with the minimum required resources
  • Use connection pooling for database connections
  • Scale vertically before scaling horizontally
  • Profile before optimizing - measure first

Summary

You've successfully configured nginx on your VPS. Remember to monitor performance, keep your software updated, and maintain regular backups. If you run into issues, consult the official documentation or open a support ticket for assistance.

Was this article helpful?