Sobes.tech
Junior

What is Nginx used for?

sobes.tech AI

Answer from AI

Nginx is a high-performance asynchronous web server and reverse proxy server used for:

  1. Serving static files: Fast and efficient delivery of HTML, CSS, JavaScript, images, and other static resources. Thanks to its asynchronous architecture, it can handle a large number of simultaneous connections with low resource consumption.
  2. Reverse proxying: Forwarding client requests to one or more backend servers (applications, APIs) and returning responses to clients. This allows load balancing, increased security (by hiding internal network structure), and SSL termination.
  3. Load balancing: Distributing incoming requests among multiple servers to improve application availability and scalability. Supports various load balancing algorithms (e.g., round robin, least connections).
  4. Caching: Caching static and dynamic resources to reduce backend load and speed up response times.
  5. SSL/TLS termination: Decrypting HTTPS traffic before passing it to the backend, reducing load on application servers and enabling centralized certificate management.
  6. Compression: Compressing data (e.g., Gzip) before sending to the client to reduce traffic and speed up page loading.
  7. Request rate limiting: Protecting backends from overload by limiting the number of requests from a single client within a specified time frame.
  8. Web Application Firewall (WAF): Basic protection against common web attacks (e.g., SQL injection, XSS).
# Example Nginx configuration for serving static files and proxying API

server {
    listen 80;
    server_name example.com; # Domain name

    location / {
        root /var/www/html; # Path to static files
        index index.html;
    }

    location /api/ {
        proxy_pass http://backend_server:8080; # Proxy API requests to another server
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}