How to Configure Nginx as a Proxy Server?

posted Originally published at dev.to 3 min read

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

        }
    }
}

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
        }
    }
}

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

More Posts

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelskiverified - Mar 19

The Audit Trail of Things: Using Hashgraph as a Digital Caliper for Provenance

Ken W. Algerverified - Apr 28

Local-First: The Browser as the Vault

Pocket Portfolioverified - Apr 20

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

Breaking the AI Data Bottleneck: How Hammerspace's AI Data Platform Eliminates Migration Nightmares

Tom Smithverified - Mar 16
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!