In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "Nginx Optimization method tutorial". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Basic (optimized) configuration
The only file we will modify is nginx.conf, which contains all the settings for the different modules of Nginx. You should be able to find nginx.conf in the / etc/nginx directory of the server.
First, we'll talk about some global settings, and then follow the modules in the file to talk about which settings give you good performance with a large number of client accesses and why they improve performance.
There is a complete configuration file at the end of this article.
High-level configuration
In the nginx.conf file, a few advanced configurations in Nginx are on top of the module section.
User www-data;pid / var/run/nginx.pid;worker_processes auto;worker_rlimit_nofile 100000
User and pid should be set by default-we won't change them, because it makes no difference whether we change them or not.
Worker_processes defines the number of worder processes when nginx provides web services to the outside. The optimal value depends on many factors, including, but not limited to, the number of CPU cores, the number of hard drives that store data, and load patterns. When in doubt, setting it to the number of available CPU cores would be a good start (setting to "auto" will try to detect it automatically).
Worker_rlimit_nofile changes the maximum number of open files limit for worker processes. If not set, this value is the limit of the operating system. After setting up, your operating system and Nginx can handle more files than "ulimit-a", so set this value high so that nginx will not have "too many open files" problems.
Events module
The events module contains all the settings for handling connections in nginx.
Events {worker_connections 2048; multi_accept on; use epoll;}
Worker_connections sets the maximum number of connections that can be opened simultaneously by a worker process. If the worker_rlimit_nofile mentioned above is set, we can set this value very high.
Keep in mind that the maximum number of customers is also limited by the number of socket connections available on the system (~ 64K), so it's no good to set an unrealistic high.
Multi_accept tells nginx to accept as many connections as possible after receiving a new connection notification.
Use sets the polling method used to reuse client threads. If you use Linux 2.6 commands, you should use epoll. If you use * BSD, you should use kqueue. Want to know more about event polling? Take a look at Wikipedia (note that you may need the course basics of neckbeard and the operating system to know everything)
It's worth noting that if you don't know which polling method Nginx should use, it will choose the one that works best for your operating system.
HTTP module
The HTTP module controls all the core features of nginx http processing. Because there are only a few configurations here, we only select a small portion of the configuration. All of these settings should be in the http module, and you won't even notice it in particular.
Http {server_tokens off; sendfile on; tcp_nopush on; tcp_nodelay on;}
Server_tokens does not make nginx execute faster, but it can turn off the nginx version number in the error page, which is good for security.
Sendfile can make sendfile () work. Sendfile () can copy data (or any two file descriptors) between disk and TCP socket. Pre-sendfile is to request a data buffer in user space before transferring data. The data is then copied from the file to the buffer with read (), and write () writes the buffer data to the network. Sendfile () reads the data from disk to the OS cache immediately. Because this copy is done in the kernel, sendfile () is more efficient than combining read () and write () and turning on and off the discard buffer (more about sendfile).
Tcp_nopush tells nginx to send all the header files in one packet instead of one after another
Tcp_nodelay tells nginx not to cache data, but to send it piece by piece-- when you need to send data in time, you should set this property to the application so that you don't get an immediate return value when you send a small piece of data.
Access_log off;error_log / var/log/nginx/error.log crit
Access_log sets whether nginx will store access logs. Turning off this option makes reading disk IO faster (aka,YOLO).
Error_log tells nginx to log only serious errors.
Keepalive_timeout 10 reset_timedout_connection on;send_timeout _ header_timeout 10; reset_timedout_connection on;send_timeout 10
Keepalive_timeout assigns the client a keep-alive link timeout. The server will close the link after this timeout. We set it lower to make ngnix work longer.
Client_header_timeout and client_body_timeout set the timeout for the request header and the request body (respectively). We can also set this lower.
Reset_timeout_connection tells nginx to close unresponsive client connections. This will free up the memory space occupied by that client.
Send_timeout specifies the response timeout for the client. This setting is not used for the entire transponder, but between two client read operations. If the client does not read any data during that time, nginx closes the connection.
Limit_conn_zone $binary_remote_addr zone=addr:5m;limit_conn addr 100
Limit_conn sets the maximum number of connections for a given key. Here key is addr, and we set the value to 100, which means we allow a maximum of 100 connections per IP address to open at the same time.
Limit_conn_zone sets the parameters used to hold shared memory for various key, such as the current number of connections. 5m is 5 megabytes, and this value should be set large enough to store (32K5) 32byte state or (16K5) 64byte state.
Include / etc/nginx/mime.types;default_type text/html;charset UTF-8
Include is simply an instruction to include the contents of another file in the current file. Here we use it to load a series of MIME types that will be used later.
The default MIME-type used by the default_type settings file.
Charset sets the default character set in our header file.
The following two points for performance improvements are explained in the great WebMasters StackExchange.
Gzip on;gzip_disable "msie6"; # gzip_static on;gzip_proxied any;gzip_min_length 1000 / gzipkeeper compounding level 4; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript
Gzip tells nginx to send data in the form of gzip compression. This will reduce the amount of data we send.
Gzip_disable disables the gzip feature for the specified client. We set it to IE6 or lower to make our solution widely compatible.
Gzip_static tells nginx to look for resources that have been pre-processed by gzip before compressing them. This requires you to pre-compress your files (commented out in this example), allowing you to use the highest compression ratio so that nginx no longer has to compress these files (for more detailed gzip_static information, click here).
Gzip_proxied allows or disables compression of response streams based on requests and responses. We set it to any, which means that all requests will be compressed.
Gzip_min_length sets the minimum number of bytes to enable compression for data. If a request is less than 1000 bytes, we'd better not compress it, because compressing these small data will slow down all processes processing the request.
Gzip_comp_level sets the compression level of the data. This level can be any number between 1 and 9, which is the slowest but has the highest compression ratio. We set it to 4, which is a more eclectic setting.
Gzip_type sets the data format that needs to be compressed. There are already some of the above examples, you can also add more formats.
# cache informations about file descriptors, frequently accessed files# can boost performance, but you need to test those valuesopen_file_cache max=100000 inactive=20s;open_file_cache_valid 30s / Virtual Host Configs# aka our settings for specific servers## include / etc/nginx/conf.d/*.conf;include / etc/nginx/sites-enabled/*
When open_file_cache turns on caching, it also specifies the maximum number of caches and the time for caching. We can set a relatively high maximum time so that we can clear them after they are inactive for more than 20 seconds.
Open_file_cache_valid specifies the interval at which correct information is detected in open_file_cache.
Open_file_cache_min_uses defines the minimum number of files during the period of inactivity of instruction parameters in open_file_cache.
Open_file_cache_errors specifies whether error messages are cached when searching for a file, including adding files to the configuration again. We also include server modules, which are defined in different files. If your server module is not in these locations, you will have to modify this line to specify the correct location.
A complete configuration of user www-data;pid / var/run/nginx.pid;worker_processes auto;worker_rlimit_nofile 1000000; events {worker_connections 2048; multi_accept on; use epoll;} http {server_tokens off; sendfile on; tcp_nopush on; tcp_nodelay on; access_log off; error_log / var/log/nginx/error.log crit; keepalive_timeout 10; client_header_timeout 10; client_body_timeout 10; reset_timedout_connection on; send_timeout 10 Limit_conn_zone $binary_remote_addr zone=addr:5m; limit_conn addr 100; include / etc/nginx/mime.types; default_type text/html; charset UTF-8; gzip on; gzip_disable "msie6"; gzip_proxied any; gzip_min_length 1000; gzip_comp_level 6; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; open_file_cache max=100000 inactive=20s; open_file_cache_valid 30s Open_file_cache_min_uses 2; open_file_cache_errors on; include / etc/nginx/conf.d/*.conf; include / etc/nginx/sites-enabled/*;}
After editing the configuration, make sure to restart nginx to make the settings take effect.
This is the end of sudo service nginx restart's "Nginx Optimization method tutorial". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.