Nginx configuration example for NodeJS app


Nginx configuration example for NodeJS app

Here is a simple example of how you can setup an nginx file to run a nodejs app. The lines which have the coment # managed by Certbot in the end are added automatically by Certbot.

# save this file in /etc/nginx/conf.d/your_file.conf
limit_req_zone $binary_remote_addr zone=one:10m rate=50r/s;

map $request_method $limit {
    default         "";
    POST            $binary_remote_addr;
}
# Creates 10mb zone in memory for storing binary ips
# Use this zone to limit the login route only 1 request per minute
limit_req_zone $limit zone=login_zone:10m rate=1r/m;

server {
    server_name  YOUR_IP_OR_DNS;

    # Limit the payload size
    client_max_body_size 10M;

    location / {
        # proxi the public port (https - 443) to the local port of the app (in this case 8000, but use yours)
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host      $host;
        # forward the real ip
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $remote_addr;
        proxy_set_header  X-Forwarded-Host $remote_addr;
        # use the limit zone created previously to limit at maximum 50 requests per second (line 2)
        limit_req zone=one burst=10 nodelay;
    }

    location /auth/login {
        # Creates 10mb zone in memory for storing binary ips
        limit_req zone=login_zone;
        proxy_pass http://127.0.0.1:8000;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/YOUR_IP_OR_DNS/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/YOUR_IP_OR_DNS/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = YOUR_IP_OR_DNS) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen       80;
    listen       [::]:80;
    server_name  YOUR_IP_OR_DNS;
    return 404; # managed by Certbot
}

Newsletter


Related Posts

A Week in the Life of an Invoice Wrangler: Navigating Ridesharing and Food Delivery Chaos

As an app founder in the ridesharing and food delivery industry, I found myself knee-deep in invoice reports from companies like Bolt, Uber, Glovo, and Bolt Food

Free HTML templates list for Startups

Free HTML templates list for startup. A complete list with free resources to build your next startup's website and gain the traction to the sky.

Deal with client requests in SaaS

How to deal with client requests in Saas which are seeing only their interests, not the product interest.

The first client of LoyalXpert is not answering anymore

Trying to implement LoyalXpert app, I lost my first customer, he's not answering anymore

Experiments with Tiktok Ads

Recently tried out TikTok ads for the first time and here are some of my learnings and challenges

People don’t care about you, until they know you care about them.

People don’t care about you, until they know you care about them. The same happens in business, you need to take care of your clients.

The One Word That Can Ruin Your SaaS Business Anyone

As a SaaS founder, you probably know how important it is to have a clear and specific target market for your product.

How I got my digital certificate connected it with ANAF

How I got my digital certificate from certSIGN and connected it with ANAF

The Ultimate List of Company Directories to Boost Your Networking

Discover a wide range of company directories to boost your business's visibility and connect with potential clients.

Discover the Best Free AI Art Tools for Your Next Masterpiece

Explore a curated collection of the finest free AI art tools, designed to help you bring your artistic vision to life.