Dev.to WebDev 🛠 Dev 👁 0 📖 6 min read

My Backend Was Already Working. So Why Did I Need Nginx?

There was a point when I thought building a full-stack application was pretty straightforward. Build the frontend. Build the backend. Connect a database. Fix the errors. Deploy it somewhere. Buy a domain if you're feeli

There was a point when I thought building a full-stack application was pretty straightforward.

Build the frontend. Build the backend. Connect a database. Fix the errors. Deploy it somewhere. Buy a domain if you're feeling fancy.

And there you go. A website on the internet.

Technically, yes.

But while working on MyVigi, a project I'd been building for a while, I started realising there's a pretty big difference between getting an application to run and getting it ready to handle the real world.

MyVigi started as a relatively straightforward full-stack application. But as I kept working on it, the backend grew more complex. I started separating responsibilities, introducing services, working with Docker, and thinking about things like reliability, authentication, communication between services, and what happens when something goes wrong.

Basically, I was trying to move beyond "it works" to "but will it keep working?"

And somewhere along the way, I came across Nginx.

Now, I'd heard of Nginx before. I just didn't really understand why I would need it.

I already had a backend server.

Why would I need another server in front of my server?

Having a website online doesn't mean it's ready for everything

Imagine opening a restaurant.

You've got the kitchen, the chefs, the menu, and a few tables. Customers can walk in, place orders, and get their food.

Everything works.

Now imagine the restaurant becomes popular.

Suddenly, there are more customers, multiple chefs preparing different orders, people asking about reservations, and delivery drivers showing up.

The kitchen still knows how to cook.

But should every single person walk directly into the kitchen to place their order?

Probably not.

You'd want someone at the front to receive customers, direct orders to the right people, and keep things organised.

That's roughly the idea that helped me understand why reverse proxies exist.

And before someone comes for me in the comments, no, Nginx isn't literally a restaurant receptionist. There is a lot more going on technically.

But stay with me.

A small disclaimer: I misunderstood Nginx for quite a while

For some reason, I used to think Nginx was another kind of backend server that would replace the one I'd already built.

Which made absolutely no sense to me.

If Express was already handling my routes and requests, what was Nginx supposed to do? Rewrite my APIs? Run my business logic? Replace Node.js?

Turns out, none of those things.

Nginx doesn't have to replace your backend. It can sit in front of it.

And once I understood that, things started getting clearer.

What happens without Nginx?

Let's say you've built a Node.js application running on port 3000.

A client sends a request, Express receives it, processes it, and returns a response.

Simple.

Client → Node.js (:3000) → Response

And there's absolutely nothing wrong with that.

You can build and deploy perfectly functional applications without Nginx.

But now suppose your application grows.

You have a frontend, an authentication service, a main backend, and a notification service, all running separately.

Maybe one service is on port 3000, another on 4000, and another on 5000.

Now you have a few questions to answer.

How do requests reach the correct service?

Where should HTTPS be handled?

What happens if you want multiple instances of the same backend?

And do clients really need to know about all these different internal services?

This is where Nginx can become useful.

Think of Nginx as a PR manager for your backend servers

Okay, this analogy might be slightly ridiculous, but hear me out.

Imagine a celebrity has a manager.

Brands, event organisers, journalists, and other people don't necessarily contact the celebrity directly.

They contact the manager.

The manager receives requests, figures out what they're about, forwards the relevant ones, and handles certain things without involving the celebrity at all.

The celebrity still does their actual job.

The manager just handles the communication around it.

That's one way to think about Nginx acting as a reverse proxy.

Your backend servers are still doing the actual work.

Nginx is the entry point receiving incoming requests and deciding where to send them.

For example:

                 Client
                    |
                    v
                  NGINX
                    |
          +---------+---------+
          |         |         |
          v         v         v
        /api      /auth     /images
          |         |         |
          v         v         v
       Backend    Auth      Static
       Service   Service    Files

The client doesn't need to know that the authentication service runs on port 4000 or that the main API runs on port 3000.

It just makes requests to the public-facing address.

Nginx handles the forwarding.

And the backend continues doing exactly what it was built to do.

Okay, but how does Nginx know where to send everything?

This is where things get technical.

Nginx uses configuration rules to decide how requests should be handled.

Here's a simplified example:

server {
    listen 80;

    location /api/ {
        proxy_pass http://localhost:3000;
    }

    location /auth/ {
        proxy_pass http://localhost:4000;
    }
}

Let's say someone visits:

example.com/api/users

Nginx sees that the request starts with /api/ and forwards it to the backend running on port 3000.

If someone visits:

example.com/auth/login

It forwards the request to the authentication service on port 4000.

And that's one of the things I found interesting.

Nginx doesn't need to know what your API actually does.

It doesn't need to understand how you authenticate users or fetch data from MongoDB.

It just needs to know where the request should go.

One technical detail: this configuration assumes Nginx can reach those services through localhost. If the services are running in separate Docker containers, you'd generally configure Nginx to use their container network names instead.

And routing isn't the only thing it can do

Once I started reading more about Nginx, I realised reverse proxying was only part of the picture.

1. Load balancing

Suppose you have three instances of the same backend running.

Instead of sending every request to one instance, Nginx can distribute incoming requests among them.

That can help you handle more traffic, provided the backend and its dependencies can support it.

2. HTTPS and TLS termination

Nginx can handle encrypted HTTPS connections and then forward requests to your application.

This means your backend doesn't necessarily need to manage public-facing TLS connections itself.

3. Serving static files

Nginx can directly serve files such as images, CSS, JavaScript, and HTML.

It doesn't always need to forward those requests to your Node.js backend.

4. Rate limiting

You can configure Nginx to limit how frequently clients make requests.

That can help protect services from excessive traffic, although it isn't a complete solution to abuse or denial-of-service attacks.

5. Caching

For suitable responses, Nginx can store a copy and serve it again without contacting the backend every time.

Of course, caching needs to be configured carefully. You wouldn't want to accidentally serve one user's private data to another.

And suddenly, the idea of having something between the client and the backend didn't seem so unnecessary.

So, do we all need Nginx now?

No.

And I think this is an important part of the discussion.

If you're deploying a simple application on a managed platform, it may already handle HTTPS, routing, load balancing, and other infrastructure concerns for you.

Adding Nginx on top of that might just mean introducing another component to maintain.

Similarly, an API gateway may already provide several of the capabilities we've discussed.

The point isn't that every application needs Nginx.

It's understanding what problems Nginx solves and recognising when those problems actually exist in your architecture.

Coming back to MyVigi

As MyVigi grew, I started thinking about my backend differently.

Initially, most of my attention was on features.

Could users log in? Could guards start shifts? Could SOS alerts be triggered? Were notifications working?

Eventually, I also had to think about how the services communicated, how requests were routed, what happened when a service became unavailable, and how the system behaved when containers restarted.

My project already had an API gateway, so learning about Nginx wasn't necessarily about adding yet another layer to it.

It was about understanding the infrastructure decisions behind applications that run beyond a development environment.

And that's probably what I found most useful about learning Nginx.

Not memorising its configuration syntax.

Not discovering another tool to add to my tech stack.

Just understanding why a perfectly functional backend might eventually need something else around it.

Because apparently, getting an application to work is one problem. Figuring out how to keep it working is a whole different one.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.