8 ways to tweak NGINX performance on Linux
NGINX is a popular, free and open source web server. The default NGINX configurations are good enough for the web server to work.
However, if you want to use NGINX to the fullest, you need to tinker with its configuration files and set parameters that will optimize server performance. You will find the configuration files in the /etc/nginx directory on your Linux machine.
1. Configure worker processes in NGINX
The NGINX architecture consists of a master process and several worker processes. The master process's job is to evaluate configuration and manage workers. On the other hand, the role of the worker process is to handle incoming requests and create a connection between cclient and server.
The progress value is set to auto by default. This sets the number of worker processes equal to the number of available CPU cores. To know how many CPU cores are in your system, run the following command:
grep processor /proc/cpuinfo | wc -l
If you want to increase the number of worker processes, you need to do this in the NGINX configuration file.
Open the file with nano:
nano etc/nginx/nginx.conf
To configure more worker processes, change the default value to the maximum number of available CPU cores in your system.
2. Configure worker connections
Another parameter you can modify to enhance NGINX performance is worker connections. This is the maximum number of TCP connections that each worker process can handle simultaneously.
Most systems have a default value of 512 connections, but many modern systems also support larger numbers. You can check how many connections your system supports with:
ulimit -n
The output will be the maximum number of connections supported. You can then modify the worker_connections variable in the NGINX configuration file to improve performance.
3. Enable GZIP compression in NGINX
NGINX uses GZIP to compress and decompress files. If enabled in the NGINX configuration file, you can save bandwidth and increase website load time on slow connections.
To enable GZIP compression, add the following lines to the NGINX configuration file:
server { gzip on; gzip_vary on; gzip_min_length 10240; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml; gzip_disable "MSIE [1-6]."; }
4. Limit timeout value in NGINX
Reduced timeout values also play an important role in enhancing NGINX performance. Persistent connections reduce the processor and network overhead of opening and closing connections.
You can modify the following parameters in the configuration file to limit timeouts:
http { client_body_timeout 12; client_header_timeout 12; keepalive_timeout 15; send_timeout 10; }
5. Adjust buffer size
You can also adjust NGINX buffers to optimize server performance. If the buffer size is too low, NGINX will write to a temporary file causing large I/O operations to run continuously.
You need to set the following buffer parameters for NGINX to work best:
http { client_body_buffer_size 10K; client_header_buffer_size 1k; client_max_body_size 8m; large_client_header_buffers 4 4k; }
6. Disable access logs or enable access log caching
Logs consume large amounts of disk space and CPU/IO cycles which can affect the server's performance if it logs every request.
You can disable access logs, which will save some disk space and CPU processing. To disable access logs, add the following line to the NGINX configuration file:
access_log off;
Logs are important because they help troubleshoot problems. Disabling logs completely is not a good thing. In this case, you can enable access log caching. This will allow NGINX to cache a batch of logs and put them into the log file at once, instead of applying different log operations to each request.
Add the following line to the NGINX configuration file to enable access log caching:
access_log /var/log/nginx/access.log main buffer=16k
7. Adjust static content cache time in NGINX
Content on a website that does not change across pages is called static content. Caching of this content allows it to be placed in easily accessible locations. This mechanism reduces bandwidth usage, allows for fast access, and subsequently improves website performance.
When a client requests static content, the server provides a cached version of the content. Add the following lines to the virtual hosts file located in the /etc/nginx/sites-available directory:
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ { expires 90d; }
This configuration will cache files for 90 days from the last browser access.
8. Enable Open File Cache in NGINX
You can also use the Open File Cache parameters in the NGINX configuration file to enhance its performance. This directive allows file descriptors and frequently accessed files to be cached on the server.
Add the following lines to the http section of the configuration file to enable Open File Cache:
http { open_file_cache max=1024 inactive=10s; open_file_cache_valid 60s; open_file_cache_min_uses 2; open_file_cache_errors on; }
A good practice to follow when changing configurations is to work through each setting one by one and test it. If it works, move on to the next installation. If not, you can always change the configuration back to the default values.
By modifying parameters configured in NGINX configuration files, such as nginx.conf and virtual host files, you can hack NGINX to deliver the best performance.
You should read it
- How to install fcgiwrap for Nginx on Ubuntu 20.04
- 10 Nginx rules to enhance WordPress security
- System performance in Unix / Linux
- How to improve gaming performance on Linux
- How to prevent DDoS attack with Nginx
- How to use Nginx as a reverse proxy
- How to create file swap in Linux
- How to monitor Nvidia GPU performance on Linux
May be interested
- How to monitor Nvidia GPU performance on Linuxeven when using the best linux distribution for gaming, you still have gpu performance problems if not configured correctly. check the graphics card management tools below to determine if the gpu has a problem.
- Instructions for configuring Reverse Proxy with the latest NGINXin today's article, tipsmake will help you learn and configure reverse proxy with nginx fully and in detail.
- Microsoft introduces browser performance measurement tool on Android and Linuxperformance is often a universal aspect that matters a lot to many people in choosing between different browsing options.
- What's new in Linux Kernel 5.9?linus torvalds has announced the new, stable linux kernel 5.9. this linux kernel version is a major release of hardware, graphics, and many other performance updates.
- 5 measures to increase Linux boot speedlinux users can be proud that the speed of operating on linux is quite fast when compared to other operating systems. not only that, linux does not seem to suffer the bogging down effect that windows has always encountered when users install hundreds of applications on the system.
- 17 lightweight Linux distributions bring new life to old computersolder computers are often slow and upgrading components such as ram, cpu and hard drive can alleviate performance problems. however, the best solution to bring new life to your old computer is to install a compact linux distribution.
- How to check for simple Linux server performancethere are many options for virtual private servers or professional servers in the market, so how do i know which server is the best and suitable for me?
- 7 ways to run Linux software on Windowslinux users want to run windows software on linux, whereas windows users want to use linux software. even if you are looking for a better development environment, more powerful command-line tools, you can run linux software without removing windows.
- Check the current user on Linuxin linux, you can perform simple tasks such as checking the current user in many ways. this tutorial will show you how to use some of the quickest and easiest ways to check users on linux.
- 5 ways to make Linux desktops look greatvarious tools, tips and tweaks can be used to personalize linux desktops. use the following 5 methods to personalize your linux desktop environment.