Nginx - torarnehave1/slowyouio GitHub Wiki

Comprehensive Guide to Nginx Configuration for Node.js and Static File Serving

Nginx is a high-performance web server and reverse proxy that can serve static files efficiently and manage complex configurations, such as load balancing and HTTP caching. This guide consolidates our previous discussions into a structured format, highlighting specific configurations and providing step-by-step instructions to optimize Nginx for serving a Node.js application and static files.

Introduction to Nginx

Nginx excels in handling high concurrency without compromising performance, making it an ideal choice for both static content delivery and as a proxy server. Understanding how to configure Nginx effectively can greatly enhance the scalability and security of your applications.

Step 1: Installation of Nginx and Node.js

Before configuring Nginx, ensure that both Nginx and Node.js are installed on your server.

Installing Nginx:

On Ubuntu-based distributions, you can install Nginx using the following commands:

sudo apt update
sudo apt install nginx

This will install Nginx and start the service automatically. Use sudo systemctl start nginx and sudo systemctl enable nginx to start and enable Nginx at boot.

Installing Node.js via NVM:

Node Version Manager (NVM) allows you to manage multiple Node.js versions. Install NVM and Node.js with these commands:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc  # Or your shell's equivalent file
nvm install node  # Install the latest version of Node.js

Step 2: Nginx Configuration for Static Files and Node.js Proxy

Configure Nginx to serve static files from a designated directory and to reverse proxy requests to a Node.js application.

Creating Nginx Server Blocks:

  1. Static Files and Node.js Proxy Configuration:

Edit your Nginx configuration file for your domain, usually located in /etc/nginx/sites-available/yourdomain.com. Below is an example configuration that includes handling for static files and a proxy setup for a Node.js app:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    # Redirect all HTTP traffic to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name yourdomain.com www.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    root /var/www/yourdomain.com/public;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ /public/$uri /public/$uri/ @nodejs;
    }

    location @nodejs {
        proxy_pass http://localhost:3000;
        include /etc/nginx/proxy_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

This configuration directs Nginx to:

  • Serve static files from the /public directory.
  • Proxy requests that don’t match static files to a Node.js app running on localhost port 3000.
  • Securely handle HTTPS connections with SSL/TLS certificates.

Explanation of Directives:

  • try_files checks for the existence of files in specified locations and serves them if found, or forwards the request to the Node.js app if not.
  • Proxy settings configure Nginx to forward requests to your Node.js application, handling dynamic content.
  • Security directives (ssl_certificate, ssl_certificate_key, etc.) ensure that your HTTPS connections are secure.

Step 3: Optimizing Nginx for Performance

Adjust Nginx settings to handle increased load and optimize header processing:

http {
    proxy_headers_hash_max_size 512;
    proxy_headers_hash_bucket_size 64;
}

These settings help manage large numbers of headers efficiently, which is crucial when Nginx acts as a proxy.

Step 4: Testing and Reloading Nginx

After making configuration changes, test your Nginx configuration for errors:

sudo nginx -t

If the test is successful, reload Nginx to apply the changes:

sudo systemctl reload nginx

Conclusion

This guide provides a detailed walkthrough on setting up Nginx to serve static files and to act as a reverse proxy for a Node.js application. By following these steps, you can harness the full potential of Nginx's performance and security features to enhance your web application's scalability and reliability.