Zero to hundred Nginx web server optimization

Zero to hundred Nginx web server optimization

Today, due to the dependence of most business practices on site processes, Nginx web server optimization can be among the popular and obscure trends. In fact, the main reason for this is the popularity of the Nginx web server among users.

In such a situation, getting to know the process of optimizing the Nginx web server can be a great help in improving the performance of the web server. Therefore, in the rest of this article, we will provide more explanations regarding the optimization of the Nginx web server and the process related to this optimization.

What is Nginx web server optimization?

The first question that concerns most people’s minds in the field of web server optimization is related to what this process is and the result of it! In fact, it should be said that Engine X web server optimization is a specific technical process that is used to improve the efficiency of data processing and program configuration.

As a result, people can optimize the Nginx web server to make this server-browser communication path work as well as possible so that data processing processes can be done at a high speed and properly. Especially since this web server is introduced and known among the popular types for responding.

The first part of the Nginx web server optimization process

Considering that the Nginx web server has special capabilities that distinguish it from other types of web servers, by optimizing Nginx in line with the Worker changes in Nginx, it is possible to increase the utilization of the capabilities of this web server.

  1. Correctly setting the number of workers

In order to optimize Nginx, you can first adjust the number of workers and its connections correctly. For this, it will be necessary to follow a simple path. For this purpose, first of all, it is necessary to enter the Nginx configuration file through the command “vi /etc/nginx/nginx.conf” and change the value of worker_process to 1. Of course, you should note that changing the number of workers must be done according to the number of CPU cores.

  1. Connections of workers

In the following, you can enter the worker connections into the optimization process. For this purpose, you can see the limit of your server core through the ulimit -n command and replace the number 4096 with 512 in the Worker_conncetion section.

In this process, you can even replace the expression: “events {worker_connections 4096;use epoll;multi_accept on;}” for Worker Connection. In this case, the speed of the Nginx web server will increase to a great extent.

  1. Keep Alive optimization

Another process that comes up in the discussion of optimizing the Nginx web server is related to the optimization of Keep Alive in Nginx. In the nginx.conf file, you can check the related settings.

In the nginx.conf file, there are various control sections related to Keep Alive, which can include Keepalive_timeout, keepalive_requests, SendFile, tcp_nopush and tcp_nodelay. Each of the paths are used to adjust and apply private processes.

  1. Buffer settings

Buffer settings are another part of the Nginx web server optimization process, which is also defined in the nginx.conf file. Buffer settings depend on components such as hard disk speed, site traffic, and site information volume.

The reason for this issue is related to the type of function that is proposed for buffer settings. In fact, a buffer is a part of memory that stores data. By setting this section of the nginx.conf file, you can fully implement the Nginx optimization process.

For this purpose, it is necessary to enter various parameters during the changes. client_max_body_size, large_client_header_buffers, output_buffers and client_header_buffer_size are examples of the mentioned parameters that need to be changed.

The second part of the Nginx web server optimization process

The continuation of the Nginx web server optimization process can be explained and reviewed in the second part. Therefore, in order to complete the optimization process, it is necessary to go through the steps mentioned in this section.

  1. Changing the connection queue in the Nginx config file

Changing the connection queue in the Nginx configuration file is among the trends that are proposed to optimize the Nginx web server. The connection queue is located in the Nginx configuration file and can be changed by changing two variables in this file. The issue raised in relation to this section is related to the possibility of changing the number if a system error is observed in the Nginx log.

In this case, it is possible to prevent errors. So that if a system error is observed, it is necessary to use the numerical variable of the expressions:

net.core.somaxconn = 65536

net.ipv4.tcp_max_tw_buckets = 1440000

in the Nginx config file. In this case, you will be able to avoid errors.

  1. Using Gzip in Nginx

Using the Gzip function in Nginx is one of the other processes that are discussed in the discussion of optimizing the Nginx web server. This feature is used to increase the loading speed of the site. So that by using Gzip you can compress all the files that can be compressed.

In this case, the size of the files is reduced and thus the speed increases. As a result, in order to implement the Gzip feature in the Nginx web server, you can define the following codes in the Nginx configuration file so that the mentioned feature works for you:

gzip on;

gzip_min_length 1000;

gzip_types: text/html application/x-javascript text/css application/javascript text/javascript text/plain text/xml application/json application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application /x-font-ttf application/xml font/eot font/opentype font/otf image/svg+xml image/vnd.microsoft.icon;

gzip_disable “MSIE [1-6]\.”;

Since site loading speed is among the most important issues related to its activity, it is necessary to pay special attention to Gzip functionality. Because in general speed increase is one of the main goals of Nginx web server optimization. Of course, consider the fact that Gzip is not the only way to increase site speed. But there are other effective components.

  1. Setting timeouts in Nginx

Setting the timeouts in Nginx is one of the other components that adjust the speed of the site. Client_body_timeout and Client_header_timeout are among time control variables. So that defining the time to perform a process by the user and sending the Request time out error in case of timeout is done through these variables.

It should also be noted that another variable called Send_timeout is placed in this section, which determines the time of the request. These variables are all related to the speed of the site and have a direct impact on its performance. For this reason, the optimization process of Nginx web server largely depends on these things.

 

The third part of the web server optimization process

Another part that comes up in the web server optimization process can be explained in the form of the third part. In this part, we can mention different components, each of which has a different effect on the optimization process. It should be noted that the commands of this section are defined in the sysctl.conf file.

  1. Set temporary ports

The setting of temporary ports is one of the components that is discussed in the third part of the optimization process. This is the case when using an Nginx web server as a proxy server. Regarding these variables, it should be noted that the settings are applied in the limits.conf file.

  1. Set file descriptors

Setting file descriptors to set connections and open files can be part of the Nginx web server optimization process. This item is placed in the sysctl.conf file like any variable related to the third section. It should be noted that these settings are necessary for a system that responds to a high number of connections.

  1. Disconnecting to respond to lost customers

Disconnecting from the response of lost customers by entering the code “reset_timedout_connection on; In the sysctl.conf file, you can free the memory allocated to the socket.

In addition to the items mentioned for the third part of the Nginx web server optimization process, options for collecting packets before dropping and TCP window size are among the mentioned processes that can affect the performance of the web server.

The fourth part of the Nginx web server optimization process

The end of the optimization path will be associated with the application of the settings of the fourth section, which includes the processes of error logging, conditional logging, general disabling of logging and monitoring of server activities.

  1. Error logging

Logging errors that are placed in the Nginx.conf file is one of the important steps that must be taken into account in relation to the optimization of the Nginx web server. Regarding the logging of errors, two main parameters logs/error.log and Error_log are set to determine the location of the log file.

So that by changing a variable in the code defined in the Nginx.conf file, the logging level can be changed arbitrarily or depending on the need. In this case, it will be possible to receive information about the status of the server, depending on the changes applied.

  1. Conditional logging

Conditional logging is another process related to web server optimization that can be used to log specific requests if needed.

  1. Disable logging altogether

Disabling logging altogether is when the server administrator uses an alternative logging method. In this case, you can use the command “server { listen 80; server_name example.com; access_log off; error_log off; }” was used to completely disable logging.

  1. Monitor server activities

Server activity monitoring is a variable that can be enabled on a server to help administrators see JSON responses immediately.

In general, by applying processes related to Nginx web server optimization, you can greatly help the website to perform better and faster. Especially since the activity of the site largely depends on these components.

 

Examples of server performance enhancement files

Nginx’s official site has proposed the ideal configuration by providing examples of nginx.conf file, sysctl.conf file and limits.conf file; But it should be kept in mind that these examples are presented as a general template and things like workers or other processing items need to be adjusted according to the server’s power.

  • sysctl.conf file

net.core.somaxconn = 65536

net.ipv4.tcp_max_tw_buckets = 1440000

net.ipv4.ip_local_port_range = 1024 65000

net.ipv4.tcp_fin_timeout = 15

net.ipv4.tcp_window_scaling = 1

net.ipv4.tcp_max_syn_backlog = 3240000

  • The limits.conf file

soft nofile 4096

hard nofile 4096

  • The nginx.conf file

pid /var/run/nginx.pid;

worker_processes 2;

events {

worker_connections 65536;

use epoll;

multi_accept on;

http {

keepalive_timeout 65;

keepalive_requests 100000;

send file on;

tcp_nopush on;

tcp_nodelay on;

client_body_buffer_size 128k;

client_max_body_size 10m;

client_header_buffer_size 1k;

large_client_header_buffers 4 4k;

output_buffers 1 32k;

postpone_output 1460;

client_header_timeout 3m;

client_body_timeout 3m;

send_timeout 3m;

open_file_cache max=1000 inactive=20s;

open_file_cache_valid 30s;

open_file_cache_min_uses 5;

open_file_cache_errors off;

gzip on;

gzip_min_length 1000;

gzip_buffers 4 4k;

gzip_types text/html application/x-javascript text/css application/javascript text/javascript text/plain text/xml application/json application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application/ x-font-ttf application/xml font/eot font/opentype font/otf image/svg+xml image/vnd.microsoft.icon;

gzip_disable “MSIE [1-6]\.”;

# [ debug | info | Notice | warn | error | Crit | alert | emerg]

error_log /var/log/nginx.error_log warn;

log_format main ‘$remote_addr – $remote_user [$time_local] ‘

‘”$request” $status $bytes_sent ‘

‘”$http_referer” “$http_user_agent” ‘

“$gzip_ratio”‘;

log_format download ‘$remote_addr – $remote_user [$time_local] ‘

‘”$request” $status $bytes_sent ‘

‘”$http_referer” “$http_user_agent” ‘

“$http_range” “$sent_http_content_range”‘;

map $status $loggable {

~^[23] 0;

default 1;

server {

listen 127.0.0.1;

server_name 127.0.0.1;

root /var/www/html;

access_log /var/log/nginx.access_log main;

location /

proxy_pass http://127.0.0.1/;

proxy_redirect off;

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_connect_timeout 90;

proxy_send_timeout 90;

proxy_read_timeout 90;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

proxy_temp_path /etc/nginx/proxy_temp;

location ~* .(woff|eot|ttf|svg|mp4|webm|jpg|jpeg|png|gif|ico|css|js)$ {

expires 365 days;

support hosting100