Blog

NGINX configuration examples

NGINX configuration examples

NGINX is a powerful web server and reverse proxy that can be used to serve static and dynamic content, load balance traffic, and implement caching. It is known for its speed, scalability, and reliability.

NGINX configuration is based on directives, which are grouped into blocks. The main configuration file is typically located at /etc/nginx/nginx.conf.

Here are some examples of NGINX configuration:

Serving static files

The following example shows how to serve static files from the /var/www/html directory:

server {
  listen 80;
  server_name example.com;

  root /var/www/html;
  index index.html index.htm;
}



Serving different types of files with different content types

The following example shows how to serve different types of files with different content types:

server {
  listen 80;
  server_name example.com;

  root /var/www/html;
  index index.html index.htm;

  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
  }

  location ~ \.(js|css|png|jpg|gif)$ {
    expires 30d;
  }
}


The location directive is used to match specific URIs and route them to different handlers. In the above example, all requests for files with the .php extension are routed to the FastCGI server running on port 9000. All requests for files with the .js, .css, .png, .jpg, or .gif extension are served with a cache expiration time of 30 days.

Implementing caching for specific file types

The following example shows how to implement caching for specific file types:

server {
  listen 80;
  server_name example.com;

  root /var/www/html;
  index index.html index.htm;

  location ~ \.(js|css|png|jpg|gif)$ {
    expires 30d;
  }
}

The expires directive is used to set the cache expiration time for a response. In the above example, all responses for files with the .js, .css, .png, .jpg, or .gif extension will be cached for 30 days.

Proxying requests to a different server

The following example shows how to proxy requests to a different server:

server {
  listen 80;
  server_name example.com;

  location /api/ {
    proxy_pass http://localhost:8080/api/;
  }
}

The proxy_pass directive is used to proxy requests to a different server. In the above example, all requests to the /api/ path will be proxied to the server running on localhost on port 8080.

These are just a few examples of NGINX configuration. For more information, please see the NGINX documentation: https://nginx.org/en/docs/.

NGINX reverse proxy configuration

To configure NGINX as a reverse proxy, you need to add a server block to your NGINX configuration file. The server block should define the public IP address and port that NGINX will listen on, as well as the backend servers that NGINX will proxy requests to.

The following example shows a simple NGINX reverse proxy configuration:

server {
  listen 80;
  server_name example.com;

  location / {
    proxy_pass http://localhost:8080/;
  }
}

This configuration will proxy all requests to the / path to the server running on localhost on port 8080.

You can also use the proxy_pass directive to proxy requests to different backend servers based on the request URI. For example, the following configuration will proxy all requests to the /api/ path to the server running on localhost on port 8081, and all other requests to the server running on localhost on port 8080:

server {
  listen 80;
  server_name example.com;

  location /api/ {
    proxy_pass http://localhost:8081/;
  }

  location / {
    proxy_pass http://localhost:8080/;
  }
}

You can also use the proxy_pass directive to proxy requests to multiple backend servers using a load balancing algorithm. For example, the following configuration will load balance requests to the /api/ path across the two servers running on localhost on ports 8081 and 8082:

server {
  listen 80;
  server_name example.com;

  location /api/ {
    proxy_pass http://localhost:8081/;
    proxy_pass http://localhost:8082/;
    load_balancer round_robin;
  }
}



Tips for writing NGINX configuration

Here are a few tips for writing NGINX configuration:

  • Use the include directive to include common configuration snippets in multiple server blocks. This can help to keep your configuration files organized and DRY.
  • Use the location directive to match specific URIs and route them to different handlers. This allows you to implement complex routing rules, such as serving different types of files with different content types, or proxying requests to different servers.
  • Use the set directive to set variables that can be used in other directives. This can be useful for implementing complex configuration rules.
  • Test your configuration changes thoroughly before deploying them to production. You can use the nginx -t command to test your configuration.

NGINX is a powerful and versatile web server and reverse proxy. By understanding the basics of NGINX configuration, you can use it to implement a wide range of features, such as serving static and dynamic content, load balancing traffic, and implementing caching.

Leave a Comment