Detailed Explanation of Nginx Configuration File Optimization

1. Main configuration file optimization:

# vi /usr/local/nginx/conf/nginx.conf

—————————————–

pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 204800; events { use epoll; worker_connections 204800; } http { include mime.types; default_type application/octet-stream; log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘ ‘$status $body_bytes_sent “$http_referer” ‘ '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 20m; sendfile on; tcp_nopush on; keepalive_timeout 60; fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; open_file_cache max=204800 inactive=20s; open_file_cache_min_uses 1; open_file_cache_valid 30s; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; }

—————————————–—————————————–—————————————–

II. Detailed Configuration Explanation:
worker_processes 8;
The number of nginx processes should ideally be specified based on the number of CPUs, and is generally a multiple of that number.

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
Each process is assigned a CPU. In the example above, 8 processes are assigned to 8 CPUs. Of course, you can write multiple CPUs, or assign one process to multiple CPUs.

For details on this setting, please read: Nginx multi-core CPU worker_cpu_affinity usage methods and examples.

worker_rlimit_nofile 204800;
This command specifies the maximum number of file descriptors a single nginx process can open. Theoretically, it should be the maximum number of open files (ulimit -n) divided by the number of nginx processes. However, nginx doesn't distribute requests evenly, so it's best to keep the value consistent with ulimit -n. Note: Here, you need to set ulimit -SHn 204800.

use epoll;
The epoll I/O model is self-explanatory.

worker_connections 204800;
The maximum number of connections allowed per process, theoretically, is worker_processes * worker_connections per nginx server.

keepalive_timeout 60;
Keepalive timeout period.

client_header_buffer_size 4k;
The buffer size for client request headers. This can be set based on your system's page size. Generally, the header size of a request will not exceed 1KB, but since system pages are usually larger than 1KB, this is set to the page size. The page size can be obtained using the command `getconf PAGESIZE`.

open_file_cache max=102400 inactive=20s;
This will specify caching for open files. It is not enabled by default. max specifies the number of caches, and it is recommended to match the number of open files. inactive specifies how long a file should remain in the cache before it is deleted.

open_file_cache_valid 30s;
This refers to how often to check the cache for valid information.

open_file_cache_min_uses 1;
The `inactive` parameter in the `open_file_cache` directive specifies the minimum number of times a file must be used within a given time period. If this number is exceeded, the file descriptor remains open in the cache. In the example above, if a file is not used even once within the `inactive` time period, it will be removed.

This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:Detailed Explanation of Nginx Configuration File Optimization

175
0 0 175

Further Reading

Post a reply

Log inYou can only comment after that.
Share this page
Back to top