<- Guides

Performance Tuning Tips for NGINX

NGINX is a popular web server and reverse proxy that is used for hosting web applications and websites. It is a high performance and low-resource consuming server that is used by many organizations and individuals for serving web content.

Due to its popularity, there are a lot of tips and tricks that can be used to make sure that the performance of the server is optimized. In this article, we will discuss some of the performance tuning tips that can be used to maximize the performance of NGINX.

1. Enable Gzip Compression

Gzip compression is a great way to reduce the size of the files that are being served by NGINX. This can drastically improve the performance of the server as it reduces the amount of data that needs to be transferred. To enable Gzip compression, you can add the following lines to your NGINX configuration file:

gzip on;
gzip_vary on;
gzip_comp_level 6;
gzip_min_length 256;
gzip_proxied any;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

2. Enable Keepalive Connections

Keepalive connections are a great way to improve the performance of NGINX. Keepalive connections allow multiple requests to be processed by the same connection, which reduces the amount of time needed to establish new connections. To enable keepalive connections, you can add the following lines to your NGINX configuration file:

keepalive_timeout 65;
keepalive_requests 100;

3. Enable Caching

Caching can significantly improve the performance of NGINX by reducing the amount of time needed to serve static files. To enable caching, you can add the following lines to your NGINX configuration file:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 302 1m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;

4. Enable FastCGI Caching

FastCGI caching is another great way to improve the performance of NGINX. FastCGI caching allows NGINX to cache dynamic content, which reduces the amount of time needed to process requests. To enable FastCGI caching, you can add the following lines to your NGINX configuration file:

fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_valid 200 302 1m;
fastcgi_cache_valid 404 1m;
fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;

5. Enable HTTP/2 Protocol

HTTP/2 is the latest version of the HTTP protocol. It is designed to be faster and more efficient than its predecessor. To enable HTTP/2, you can add the following line to your NGINX configuration file:

http2_idle_timeout 65s;

6. Use Caching Headers

Caching headers can be used to improve the performance of NGINX by allowing the browser to cache static content for a certain amount of time. This reduces the amount of time needed to serve static files and significantly improves the performance of the server. To enable caching headers, you can add the following lines to your NGINX configuration file:

expires 12h;
add_header Cache-Control "public";

7. Optimize Logging

Logging can be a major performance bottleneck for NGINX. To reduce the amount of time needed to process requests, it is important to optimize the logging process. To do this, you can add the following lines to your NGINX configuration file:

access_log off;
log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

8. Use Proxy Buffers

Proxy buffers can be used to reduce the amount of time needed to process requests. To enable proxy buffers, you can add the following lines to your NGINX configuration file:

proxy_buffers 8 16k;
proxy_buffer_size 32k;

9. Use OpenSSL Session Caching

OpenSSL session caching can be used to improve the performance of NGINX by reducing the amount of time needed to establish an SSL connection. To enable OpenSSL session caching, you can add the following lines to your NGINX configuration file:

ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

10. Use Upstream Keepalives

Upstream keepalives can be used to reduce the amount of time needed to establish a connection with the upstream server. To enable upstream keepalives, you can add the following line to your NGINX configuration file:

upstream_keepalive_timeout 65;

These are some of the tips and tricks that can be used to optimize the performance of NGINX. By following these tips, you can make sure that your server is running at its peak performance.

Ready to run your test?
Start your first test within minutes.