Sometimes we have to redirect listening from one port to another. For example, if we have a listener on localhost we want to redirect traffic from listening on other IP to localhost.

In this example, we will listen on port 60000 and redirect all traffic to localhost listener with port 8000.

server{
        listen 60000;
        listen [::]:60000;
        server_name  _;
        location / {
                proxy_pass http://127.0.0.1:8000/;
        }
}

After creating a server block we have to add listeners. In the code above, you can see two rows with the keyword listen. The first one contains only a port (listen with IP v4) and the second one contains “[::]” before the port (symbol for all IP v6). The following row contains the “server_name” keyword. The symbol “_” means that all server names will be caught.

The next block is a location block. In this example, we match everything after the first “/” and redirect it to a location on localhost’s port 8000. The redirect is made with the keyword “proxy_pass“. This row (command) just maps the external port location to location where we want it to go/use.

Was this post helpful?