Nginx Reverse Proxy Explained (With Real Examples for PHP and Next.js)

A reverse proxy sits in front of your app and decides where requests should go. It can route traffic, add headers, cache responses, and terminate SSL.

Example: route /api to PHP, / to Next.js

server {
  listen 80;

  location /api/ {
    proxy_pass http://php-backend;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location / {
    proxy_pass http://nextjs-frontend;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

Why this matters

  • Clean separation of services
  • Better security via header control
  • Faster delivery via caching/CDN integration

Once you understand routing, you can add caching rules and rate limiting for sensitive endpoints.