Understanding Proxies: Forward vs Reverse

πŸš€ Introduction

Whether you're building modern web apps or exploring computer networking as a CS student or developer, you've likely encountered proxy serversβ€”especially while dealing with CORS errors or setting up dev environments.

But what exactly is a proxy server? Why are there forward and reverse proxies? And how do you use one effectively in a React (Vite) app?

Let’s break it all down β€” the what, why, and how, with developer-friendly explanations and real-world setups.

🧠 What is a Proxy Server?

A proxy server acts as an intermediary between a client (like your browser or frontend app) and a destination server (like an API or a website). Instead of sending your request directly to the target, your request first goes to the proxy, which forwards it to the actual server.

Think of it as a digital middleman that routes your traffic behind the scenes.

πŸ”€ Types of Proxies: Forward vs Reverse

➀ 1. Forward Proxy

πŸ“ Position: Between the client and the internet/server

πŸ“₯ Use Case: Helps clients access resources indirectly (common in schools, organizations, or VPNs)

πŸ’‘ Example:

  • A university blocks YouTube, but you use a forward proxy to access it.

  • You use a proxy in development to bypass CORS errors when calling external APIs.

πŸ–ΌοΈ Visual:
Client β†’ [Forward Proxy] β†’ Server

graph TD
    A[Client] --> B[Forward Proxy Server]
    B --> C[Target Server]

➀ 2. Reverse Proxy

πŸ“ Position: Between the internet/client and the server

πŸ“€ Use Case: Routes incoming requests to appropriate backend services, load balances traffic, hides internal services.

πŸ’‘ Example:

  • Nginx receives traffic at example.com/api and forwards it to your Node.js API server.

  • Used in microservices architecture, CDN, or API gateways.

πŸ–ΌοΈ Visual:
Client β†’ [Reverse Proxy] β†’ Multiple Internal Servers

graph TD
    A[Client/ User] --> B[Reverse Proxy e.g Nginx]
    B --> C[Web Server e.g React frontend ]
    B --> D[API Server e.g Node/Express]
    B --> E[Auth Service e.g JWT/OAuth]

πŸ§‘β€πŸ’» Why Proxies Matter for Developers

As a frontend dev or full-stack engineer, you may use proxies to:

  • Avoid CORS issues in development.

  • Route API calls through a local server.

  • Keep environment configurations clean.

  • Simulate real-world API architecture.

βš™οΈ Proxy Setup in React (Vite)

Let’s say you have a React app created with Vite and a backend API running at http://localhost:5000. You want to fetch data from /api/users.

πŸ§ͺ Problem:

If your frontend is on localhost:5173, calling fetch('http://localhost:5000/api/users') will trigger a CORS error.

βœ… Solution: Use Vite’s Dev Proxy (Forward Proxy)

Vite provides a clean proxy option in vite.config.js.

πŸ”§ Step-by-Step Setup

Modify vite.config.js:

// vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:5000', // Your backend server
        changeOrigin: true,
        rewrite: path => path.replace(/^\/api/, '')
      }
    }
  }
})

Fetch API in your frontend:

// src/App.jsx
import { useEffect, useState } from 'react'

function App() {
  const [users, setUsers] = useState([])

  useEffect(() => {
    fetch('/api/users') // This will be forwarded to http://localhost:5000/users
      .then(res => res.json())
      .then(data => setUsers(data))
  }, [])

  return (
    <div>
      <h1>Users</h1>
      <ul>
        {users.map((user, i) => <li key={i}>{user.name}</li>)}
      </ul>
    </div>
  )
}

export default App

Start your backend and frontend:

# Backend on port 5000
# Frontend on port 5173
npm run dev

βœ… Done! You’ve just set up a forward proxy using Vite!

πŸ” What About Reverse Proxy?

While Vite helps during development, reverse proxies like Nginx or Apache are often used in production to:

  • Serve React app statically.

  • Forward /api/* calls to the backend server.

  • Provide SSL termination (HTTPS).

  • Load balance between multiple backends.

🌐 Sample Nginx Config for React + Node:

nginxCopyEditserver {
  listen 80;
  server_name yourdomain.com;

  location / {
    root /var/www/react-app;
    index index.html;
    try_files $uri /index.html;
  }

  location /api/ {
    proxy_pass http://localhost:5000/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

πŸ’¬ Common Questions

❓ Why not call the backend directly from React?

Because of CORS restrictions. Browsers prevent JS from calling a different origin. A proxy acts as a workaround.

❓ Can I use reverse proxy in development?

You can, but it's overkill. Vite's forward proxy is optimized for DX (Developer Experience).

πŸ“š Summary

Proxy TypePositioned BetweenUse Case
Forward ProxyClient ↔ Proxy ↔ ServerCORS fix, access control, VPN
Reverse ProxyClient ↔ Proxy ↔ BackendAPI routing, load balancing

🧩 Bonus: Debugging Tips

  • Use console.log(req.headers) on backend to inspect forwarded headers.

  • Use browser dev tools β†’ Network tab β†’ check if /api/* is being redirected.

  • Use tools like http-proxy-middleware if using CRA instead of Vite.

πŸ“Œ Final Thoughts

Understanding proxies is a crucial skill for every web developer and computer science student. Whether you’re fixing a CORS issue or deploying to production, proxies are your silent helpers behind the scenes.

βœ… Start with forward proxy in Vite, then explore reverse proxies like Nginx for production-level control.

If you found this blog helpful, do share it with your developer friends. Got questions or feedback related to the topic? Drop them in the comments β€” I’d love to discuss!
Stay tuned for more such deep-dives. Until then, keep building and keep learning. πŸš€

10
Subscribe to my newsletter

Read articles from Anuj Kumar Upadhyay directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anuj Kumar Upadhyay
Anuj Kumar Upadhyay

I am a developer from India. I am passionate to contribute to the tech community through my writing. Currently i am in my Graduation in Computer Application.