Search code examples
nginxproxypass

NGINX server not proxying to server


I am tring to proxy a test project that is served using the npm http-server module like so:

http-server -p 8081.

I have the following nginx configurations.

nginx.conf

user  www-data;
worker_processes  4;

events {
    worker_connections  7684;
}  


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    include /etc/nginx/sites-enabled/*;
}

sites-enabled/default

upstream mysite {

    server 127.0.0.1:8081;

}

server {

    listen 80;
    listen [::]:80;

    location / {
            proxy_pass http://mysite;
}

When I hit localhost it works. When I hit localhost:8081 it works. However, when I enter http://mysite the console is giving me an ERR_NAME_NOT_RESOLVED error. When I enter mysite, it just googles. Can anybody explain what is going on and why it isn't being proxied appropriately?


Solution

  • With server.listen 80 you create a local server on port 80. All requests (location /) are redirected (proxy_pass) to mysite. mysite is on localhost on port 8081.

    So you have to run another server on port 8081. And when you call localhost:80 you are calling that on 8081.

    EDIT: mysite is just an alias for nginx for an internal collection of backends (upstream). You can not call http://mysite from your browser. Instead just call http://localhost.