Monday, 6 January 2025

Nginx configuration with explanations

 Nginx configuration with explanations for various keywords and directives you will encounter in a typical Nginx setup:

1. Basic Nginx Configuration File Structure

The main configuration file for Nginx is usually located at /etc/nginx/nginx.conf.

user nginx; worker_processes auto; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; server { listen 80; server_name example.com www.example.com; location / { root /usr/share/nginx/html; index index.html index.htm; } } }

2. Global Directives

  • user: Specifies the user and group under which Nginx workers will run.
  • worker_processes: The number of worker processes that Nginx will spawn. auto will set this to the number of CPU cores.
  • pid: The file location where Nginx stores its process ID.

3. Events Block

The events block defines settings that affect the operation of worker processes.

  • worker_connections: Specifies the maximum number of simultaneous connections each worker process can handle.
events { worker_connections 1024; }

4. HTTP Block

The http block contains directives that configure the HTTP server functionality. This is where you define most of your server configurations.

  • include: Includes other configuration files (e.g., MIME types).
  • default_type: Specifies the default MIME type if it cannot be determined.
http { include /etc/nginx/mime.types; default_type application/octet-stream; }

5. Server Block

Each server block defines a virtual server.

  • listen: Specifies the port and/or IP address to listen on (e.g., listen 80;).
  • server_name: Defines the domain names or IP addresses the server will respond to.
  • location: Defines how to handle requests for specific URI patterns.
server { listen 80; server_name example.com www.example.com; location / { root /usr/share/nginx/html; index index.html index.htm; } }

6. Location Block

The location block is used to define how to handle specific URI patterns or locations.

  • root: Specifies the directory from which files will be served.
  • index: Specifies the index file to serve when a directory is requested.
  • try_files: Tries to serve the file and, if not found, can redirect to another location.

Example:

location / { root /var/www/html; index index.php index.html index.htm; }

7. Rewrite and Redirects

  • rewrite: This directive allows you to rewrite URLs based on regular expressions.
  • return: This is used to send an HTTP response directly, often for redirects.

Example:

rewrite ^/old-page$ /new-page permanent; return 301 https://example.com$request_uri;

8. SSL Configuration

To serve HTTPS traffic, you’ll need to include SSL certificates and enable SSL in your server block.

server { listen 443 ssl; server_name example.com; ssl_certificate /etc/nginx/ssl/example.crt; ssl_certificate_key /etc/nginx/ssl/example.key; location / { root /usr/share/nginx/html; index index.html; } }

9. Proxy and Reverse Proxy Configuration

You can use Nginx as a reverse proxy to forward traffic to an upstream server.

server { listen 80; server_name example.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }

10. Error Handling

You can define custom error pages for certain HTTP status codes.

server { listen 80; server_name example.com; error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } }

11. Gzip Compression

Enabling Gzip can improve your website's performance by compressing responses.

http { gzip on; gzip_types text/plain text/css application/javascript application/json application/xml text/javascript; gzip_min_length 1000; }

12. Caching

You can configure caching rules for certain resources.

location ~* \.(jpg|jpeg|png|gif|css|js)$ { expires 30d; add_header Cache-Control "public, no-transform"; }

13. Access Control

  • allow: Grants access to a specific IP address or range.
  • deny: Denies access to a specific IP address or range.

Example:

location /admin { allow 192.168.1.1; deny all; }

14. Logging

You can configure logging for access and errors.

http { access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; }

15. Load Balancing

To load balance requests across multiple servers, use the upstream directive.

upstream backend { server backend1.example.com; server backend2.example.com; } server { location / { proxy_pass http://backend; } }

16. Rate Limiting

Rate limiting is used to control the number of requests a client can make in a given period.

http { limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s; server { location / { limit_req zone=mylimit burst=5; } } }

Conclusion

This is a general overview of the essential Nginx configuration keywords and directives. You can use these directives to set up a basic to advanced Nginx configuration for serving static files, reverse proxying, SSL setup, caching, logging, error handling, and more. Be sure to consult the official Nginx documentation for more detailed information on any specific directive.

Summary

File/DirectoryPurpose
/etc/nginx/nginx.confMain Nginx configuration file.
/etc/nginx/sites-available/Virtual host configuration files.
/etc/nginx/sites-enabled/Symlinks to enabled virtual host configurations.
/etc/nginx/mime.typesMaps file extensions to MIME types.
/etc/nginx/conf.d/Additional configuration files (e.g., SSL).
/etc/nginx/snippets/Reusable configuration snippets.
/var/log/nginx/Stores access and error logs.
/etc/nginx/ssl/Stores SSL certificates and keys.

Thank you.

No comments:

Post a Comment

Golang Advanced Interview Q&A