DEV Community

Cover image for How to Configure Nginx as a Proxy Server?
Ganesh Kumar
Ganesh Kumar

Posted on

How to Configure Nginx as a Proxy Server?

Hello, I'm Ganesh. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.

In previous article we could able to server simple html page.

Now lets see how to use nginx as a proxy server.

Setting Up a Simple Proxy Server

One of the frequent uses of nginx is setting it up as a proxy server, which means a server that receives requests, passes them to the proxied servers, retrieves responses from them, and sends them to the clients.

We will configure a basic proxy server, which serves requests on behalf of another server running in port 8080.

Here is example of how to configure it.

# This is a comment in the Main Context
worker_processes 1; 

events { 
    # This is a simple directive inside the 'events' context
    worker_connections 1024; 
}

http { 
    # We are now inside the 'http' context

    server { 
        location / {
            proxy_pass http://localhost:8080; # Proxies traffic to the backend on 8080

        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This will be a simple server that listens on the port 8080.

Add Static Files For Existing Proxy Server

let's assume we have a static files in /var/www/html directory and we want to server another application running on port 8080.

so, we can configure both these in nginx it self.

# This is a comment in the Main Context
worker_processes 1; 

events { 
    # This is a simple directive inside the 'events' context
    worker_connections 1024; 
}

http { 
    # We are now inside the 'http' context

    server { 
        location / {
            proxy_pass http://localhost:8080; # Proxies traffic to the backend on 8080

        }

        location /static/ {
            root /var/www/html; # Serves static files from /var/www/html
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This way we will be able to serve both static files and other server running in port 8080.

Conclusion

We could able to setup simple proxy server and serve both static files and other server running on different port.

git-lrc

Any feedback or contributors are welcome! It’s online, source-available, and ready for anyone to use.
⭐ Star it on GitHub: https://github.com/HexmosTech/git-lrc

Top comments (0)