skip to Main Content

I’m adding NGINX to my MERN app as a reverse proxy. I need Nginx to forward all requests to the node server that runs on port 4000. I add the following server block to the default nginx.conf, in which I add the server_name and the proxy_pass.

    server{
      listen 80;
      server_name douban;
      location / {
          proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
          proxy_set_header Host $host;
          proxy_pass http://127.0.0.1:4000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
      }
  }

$ sudo nginx -t shows me the config is correct

then I run $ sudo nginx -s stop && sudo nginx to start Nginx, and $ npm start to start my backend server

The problem is, I cannot access my server using the server_name (127.0.0.1:douban). Is my reverse proxy working?

2

Answers


  1. You cannot access Nginx with the server_name.
    Please check how server_name works
    http://nginx.org/en/docs/http/server_names.html

    You can access your Nginx with YOUR_NGINX_IP:80 (listen port) only.

    Login or Signup to reply.
  2. In NGINX you server_name is not a port that you define to access the app from

    server_name is a the domain, IP, etc… that you would forward to a custom port

    for example, I want to bind the domain api.example.com to port 4000 and example.com to port 3000 and anything else to port 5000

    you’ll create 2 server blocks

    the 1st will have server_name with api.example.com and the proxy_pass will be http://localhost:4000

    the second will have server_name with example.com and the proxy_pass will be http://localhost:3000

    the last one will have server_name with _ and the proxy_pass will be http://localhost:5000

    hope that my answer is clear

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search