How to set up a reverse proxy with nginx?

Published on Aug. 22, 2023, 12:19 p.m.

To set up a reverse proxy with nginx, you can use the proxy_pass directive in your nginx configuration file. Here is a basic example to proxy requests from example.com to localhost:3000:

server {
  listen 80;
  server_name example.com;

  location / {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  }
}

In this example, listen 80; specifies that nginx should listen for HTTP requests on port 80. server_name specifies the domain name for which you want to proxy requests to localhost:3000. location / specifies the path to match for incoming requests and proxy_pass specifies the backend server to which requests should be forwarded.

proxy_set_header Host $host; and proxy_set_header X-Real-IP $remote_addr; sets the Host and X-Real-IP headers in the forwarded requests to ensure that the backend server receives the requests with the correct headers and IP address.

Note that this is a basic example to help you get started. You may need to modify the configuration to suit your specific needs.