YouTip LogoYouTip

Nginx Reverse Proxy

Introduction

A reverse proxy forwards client requests to backend servers. Nginx excels at this role, providing load balancing, SSL termination, and caching capabilities.

Basic Reverse Proxy

server {
    listen 80;
    server_name example.com;

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

Proxy Multiple Services

server {
    listen 80;
    server_name example.com;

    location /api/ {
        proxy_pass http://localhost:8080/;
    }

    location /app/ {
        proxy_pass http://localhost:3000/;
    }

    location /static/ {
        alias /var/www/static/;
        expires 30d;
    }
}

WebSocket Support

location /ws/ {
    proxy_pass http://localhost:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 86400;
}

Summary

Nginx reverse proxy is essential for microservices architecture. Configure proxy_pass, set proper headers, and handle WebSocket upgrades for modern applications.

← Nginx SSL ConfigurationNginx Tutorial - Getting Start β†’