Ad

Simply Nginx Reverse Proxy From Localhost:80 To Localhost:8080 Not Working

- 1 answer

im learning reverse proxy w/ nginx for the first time, and the following isnt working for me im trying to reroute requests from http://localhost to an api server i have running at http://localhost:8080

server {
    listen 80;
    location / {
        proxy_pass http://localhost:8080;
    }
}

when i hit http://localhost, I simply get shown the welcome to nginx splash screen. if i hit http://localhost:8080, i see my api

I have a node express service running at :8080, which i can hit manually, but shouldn't http://localhost be proxied there too?

Ad

Answer

When I setup a nginx domain that forwards requests to a node server, it looks like this, for the server_name, you can use localhost as a parameter for accessing it via localhost. You can also pass default_server to make this the default server config.

Note: Only one active config can contain default_server otherwise Nginx will throw errors.

Note: When using default_server, Nginx will catch localhost in that server config. Otherwise you need to specify localhost in the list of server_name's (separated by a space).

server {
  # Setup the domain name(s)
  server_name example.com;
  listen 80 default_server;

  # If you would like to gzip your stuff
  gzip on;
  gzip_min_length 1;
  gzip_types *;

  # Setup the proxy
  # This will forward all requests to the server
  # and then it will relay the servers response back to the client
  location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_cache_bypass $http_upgrade;
  }
}
Ad
source: stackoverflow.com
Ad