Setting Up Nginx as a Reverse Proxy

Setting Up Nginx as a Reverse Proxy

A reverse proxy forwards incoming requests to a backend application running on a different port. This lets you run multiple apps on one server behind a single domain, and add SSL without modifying the app.

Basic Reverse Proxy Config

nano /etc/nginx/sites-available/myapp
server {
    listen 80;
    server_name myapp.yourdomain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        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;
    }
}
ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx

Add SSL with Certbot

certbot --nginx -d myapp.yourdomain.com

WebSocket Proxy Support

For apps using WebSockets (Node.js, chat apps, etc.), add:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

Proxy Multiple Apps

Create a separate config file for each app/domain:

# /etc/nginx/sites-available/app1 -> localhost:3000
# /etc/nginx/sites-available/app2 -> localhost:4000
# /etc/nginx/sites-available/app3 -> localhost:5000

Tune Timeouts for Slow Backends

proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
  • 0 用戶發現這個有用
這篇文章有幫助嗎?

相關文章

Understanding Your Server's Network Configuration

Understanding Your Server's Network Configuration Knowing how to read and manage your server's...

Setting Up Reverse DNS (rDNS/PTR Record)

Setting Up Reverse DNS (rDNS / PTR Record) A reverse DNS (PTR) record maps your IP address back...

How to Configure a Static IP Address on Linux

Configuring a Static IP Address on Linux Dedicated servers typically have a static IP configured...

How to Configure DNS Records for Your Domain

Configuring DNS Records for Your Domain DNS records tell the internet how to reach your domain....

Setting Up a Mail Server with Postfix and Dovecot

Setting Up a Mail Server with Postfix and Dovecot This guide sets up a basic mail server: Postfix...