farnodes / sysadmin

0 stars 0 forks source link

nginx 101 #2

Open swetank01 opened 3 months ago

swetank01 commented 3 months ago
  1. Serve Static Content:

Imagine you have a website with a public_html directory containing your website's files. You can configure Nginx to serve content from this directory by creating a server block like this:

server {
    listen 80; # Listen on port 80 (default HTTP port)
    server_name yourdomain.com; # Replace with your domain name

    location / {
        root /var/www/html/public_html; # Path to your website files
        index index.html index.htm; # Default files to serve
    }
}
  1. Reverse Proxy:

Let's say you have two web applications running on different ports on your server. Nginx can act as a reverse proxy, directing traffic to the appropriate application based on the URL. Here's an example:

server {
    listen 80;
    server_name yourdomain.com;

    location /app1 {
        proxy_pass http://localhost:3000; # Forward requests to app on port 3000
    }

    location /app2 {
        proxy_pass http://localhost:5000; # Forward requests to app on port 5000
    }
}
  1. SSL/TLS Termination:

To enable HTTPS, you'll need an SSL certificate. Nginx can handle the encryption and decryption process. Here's a basic example (remember to replace the placeholders with your actual certificate and key files):

server {
    listen 443 ssl; # Listen on port 443 (default HTTPS port)
    server_name yourdomain.com;

    ssl on;
    ssl_certificate /path/to/your/certificate.crt;
    ssl_certificate_key /path/to/your/private.key;

    # Rest of your server block configuration
}
  1. Basic Authentication:

Nginx can restrict access to specific directories using a username and password. Here's an example configuration for a directory named admin:

location /admin {
    auth_basic "Restricted Area";
    auth_basic_user_file /etc/nginx/htpasswd.users; # Path to password file
}

You'll need to create a separate file (htpasswd.users) containing usernames and encrypted passwords using the htpasswd tool.

  1. URL Rewriting:

Nginx can rewrite URLs on the fly using regular expressions. For example, you can rewrite all requests for .php files to remove the extension:

rewrite ^/(.*)\.php$ /$1 permanent;

This will rewrite /about.php to /about while maintaining the correct behavior.

  1. Load Balancing:

Imagine you have two web servers handling traffic for your website. Nginx can distribute incoming requests between them using a simple load balancing configuration:

upstream my_backend {
    server server1.example.com:80;
    server server2.example.com:80;
}

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://my_backend; # Forward requests to the upstream block
    }
}
  1. Caching:

Nginx can cache frequently accessed static content like images and CSS files. This reduces server load and improves website loading speed. Here's an example configuration:

location /images/ {
    expires 30d; # Set cache expiration time to 30 days
    proxy_cache_valid 30d; # Cache valid for 30 days
    proxy_cache_revalidate after 1d; # Revalidate cache after 1 day
    proxy_cache http_cache; # Enable caching
}
  1. Logging and Monitoring:

Nginx can log access requests and errors for monitoring purposes. This allows you to analyze website traffic and identify potential issues. You can configure the level of detail logged in the http block of your Nginx configuration file.

  1. Geolocation:

With additional modules, Nginx can identify a user's location based on their IP address. You can then potentially serve them region-specific content or language variations.

  1. Multi-Site Management:

Nginx allows hosting multiple websites or applications on a single server using server blocks. Each server block defines a virtual host with its own domain name and document root. This allows efficient resource utilization.