Linux-related optimizations

Source: Internet
Author: User

LVS Four layer Dr Direct return to user
Support for large concurrency four layer load balancing complex cannot change port
LVS load balancing (large concurrency) NGINX for web dynamic and static separation mobile and PC separation
Resolve Web High concurrency


MySQL Tuning parameter tuning
Open MySQL configuration file my.cnf
1 Back_log parameter value: Modified by default of 50 to 500. (256kb per connection, occupied: 125M)
back_log=500
The value indicates how many requests can be present in the stack for a short period of time before MySQL temporarily stops answering the new request. That is, if the MySQL connection data reaches max_connections, the new request will be present in the stack, waiting for a connection to release the resource, the number of that stack is back_log, and if the number of waiting connections exceeds back_log, the connection resource will not be granted. Will be reported: unauthenticated user | xxx.xxx.xxx.xxx | NULL | Connect | NULL | Login | NULL for the pending connection process.
The Back_log value cannot exceed the size of the listening queue for TCP/IP connections. If it is not valid, view the size of the listening queue for the current system's TCP/IP connection command: Cat/proc/sys/net/ipv4/tcp_max_syn_backlog The current system is 1024. For Linux systems, the recommended setting is an integer less than 512.

Modify system kernel parameters,)
View MySQL current system default Back_log value, command:
Show variables like ' Back_log '; View Current Quantity
2 Change the Wait_timeout parameter value, by the default of 8 hours, modified to 30 minutes. (Not this time)
wait_timeout=1800 (unit is wonderful) MySQL client's database connection idle maximum time value
The site has a large number of MySQL link requests (each MySQL connection is to memory resource cost), due to your program for reasons of a large number of connection requests idle do nothing, take up memory resources, or cause MySQL more than the maximum number of connections can never create a new connection caused by "Too many Connections "Error
3 Modify the Max_connections parameter value, by default 151, modified to (750M).
max_connections=3000
Max_connections refers to the maximum number of MySQL connections, if the server's concurrent connection request volume is large, it is recommended to increase this value, so that the number of parallel connections, of course, this is based on the machine can support the case, because if the number of connections between MySQL will provide a connection buffer for each connection, It will cost more memory, so adjust the value appropriately, you can't blindly increase the value
4 Modify the Max_user_connections value by default of 0, modified to 800
Max_user_connections=800 refers to the maximum number of connections per database user
5 Modify the Thread_concurrency value, by the current default of 8, modified to 64
Thread_concurrency=64
The thread_concurrency should be set to twice times the number of CPU cores. For example, there is a dual-core CPU, that thread_concurrency should be 4; 2 Dual-core CPUs, the value of thread_concurrency should be 8
6default-storage-engine (set default storage engine for MySQL)
Default-storage-engine= InnoDB (Set InnoDB type (supports physical type), also can set MyISAM (read more write less build engine)
Two global Caches
The global cache that will be allocated and always present when you move MySQL. There are currently: Key_buffer_size (default: 402653184, or 384M), innodb_buffer_pool_size (default: 134217728 i.e.: 128M), innodb_additional_mem_ Pool_size (Default: 8388608 is: 8M), innodb_log_buffer_size (default: 8388608 is: 8M), query_cache_size (default: 33554432 is: 32M), etc. five. Total: 560M.
These variable values can be obtained by command such as: Show variables like ' variable name '; look.
Three-part cache
Read_buffer_size (default: 2097144 or 2M)
READ_BUFFER_SIZE=4M is the MySQL read-in buffer size. A request to sequentially scan a table allocates a read-in buffer, and MySQL allocates a memory buffer for it. The Read_buffer_size variable controls this

The size of the buffer. If the sequential scan requests for a table are frequent, and you think that frequent scans are going too slowly, you can improve their performance by increasing the value of the variable and the size of the memory buffer.
Sort_buffer_size,read_rnd_buffer_size,tmp_table_size the size of the memory space. However, they are allocated only when they are needed and are released after those operations have been done. Some are immediately allocated as separate blocks. Tmp_table_size can be as high as MySQL can allocate to this operation's maximum memory space
Four other caches
Thread_cache_size (server thread cache)
Table_cache (default: 512)

#######################################################
Nginx optimization
1 worker_processes 1; Normally adjusted to the same number of CPUs
2 Optimizing binding of different nginx processes to different CPUs
Optimization of 3 Nginx Event processing model
4 Adjust the maximum number of client connections allowed by Nginx single process
Parameter syntax: worker_connections number
Default configuration: Worker_connections 512
Drop Location: Events tab
Events {
Worker_connections 1024; #一个worker进程的并发
}
Total concurrency = worker_processes* worker_connections
5 Configuring the maximum number of open files for Nginx worker process
Parameter syntax: worker_rlimit_nofile number
Drop Location: Main TAB segment
Description: The function is to change the maximum number of files that the worker processes can open
Worker_rlimit_nofile 65535;
This parameter is limited by the maximum number of open system files, and the workaround:
[Email protected] nginx]# Cat/proc/sys/fs/file-max
8192
File system maximum number of open files
[Email protected] nginx]# ulimit-n
1024
Program limit can only open 1024 files
Use # Ulimit-n 8192 to adjust
or permanently adjust the number of open files can be added at the end of the startup file/etc/rc.d/rc.local (add fs.file-max=xxx Invalid at the end of/etc/sysctl.conf)
6 Enable efficient file transfer mode
Set parameter sendfile on;
The Sendfile parameter is used to open the file's efficient transfer mode. The Tcp_nopush and tcp_nodelay Two instructions are also set to on to prevent network and disk I/O blocking and improve nginx productivity.
HTTP {
Sendfile on; #放在http, Server,location can
7 fastcgi Correlation parameter tuning
Fastcgi_connect_timeout 240;
Fastcgi_send_timeout 240;
Fastcgi_read_timeout 240;
Fastcgi_buffer_size 64k;
Fastcgi_buffers 4 64k;
Fastcgi_busy_buffers_size 128k;
Fastcgi_temp_file_write_size 128k;
#fastcgi_temp_path/data/ngx_fcgi_tmp;
Fastcgi_cache_path/data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g;
8 Deploy Site program permission settings
Contents: 755
Files: 644
Owner: Root
cd/application/apache/html/
Chown-r Root.root Blog
Find./blog/-type F|xargs chmod 644
Find./blog/-type D|xargs chmod 755
9. Control the number of Nginx concurrent connections
Ngx_http_limit_conn_module This module is used to limit the number of connections per defined key that are worth the number of connections, especially a single TP.
Not all connections will be counted. A connection that meets the count requirement is a connection that has been read by the entire request header.
Description of the parameters for controlling the number of Nginx concurrent connections
1) limit_conn_zone Parameters:
Syntax: Limit_conn_zone key zone=name:size;
Context: http
Used to set the shared memory area, key can be a string, Nginx comes with a variable or the first two combinations. Name is the memory area, and size is the memory area.

2) Limit_conn Parameters
Syntax: limit_conn zone number;
Context: HTTP, server, location
Specifies the maximum number of connections for the key setting. When the maximum number of connections is exceeded, the server returns an error of 503.

10. Controlling the rate of client request Nginx
The Ngx_http_limit_req_module module is used to limit the request rate per IP access for each defined key.
The limit_req_zone parameters are described below.
Syntax: Limit_req_zone key zone=name:size rate=rate;
Context: http
Used to set the shared memory area, key can be a string, Nginx comes with a variable or the first two combinations. Name is the memory area, size is the memory area, rate is r/s, and one request per second.
The limit_req parameters are described below:
Syntax: Limit_req zone=name [Burst-number] [Nobelay]
Context: HTTP, server, location
Here the token bucket principle is used, burst=num, a fast token with NUM, after the token is sent out, the extra requests will return 503.
Nodelay default is not more than burst worth the premise will be queued for processing, if you use this parameter, will be processed to complete the num+1 request, the remaining request is timed out, return 503.
###########################################################
Modify the Linux kernel parameters to improve the performance of Nginx server at high time
The number of TCP time_wait sockets often reaches 20,000 or 30,000, and the server can easily be dragged to death. By modifying the Linux kernel parameters, you can reduce the number of time_wait sockets on the Nginx server.

Vi/etc/sysctl.conf
Add the following lines:
Reference
Net.ipv4.tcp_fin_timeout = 30
Net.ipv4.tcp_keepalive_time = 1200
Net.ipv4.tcp_syncookies = 1
Net.ipv4.tcp_tw_reuse = 1
Net.ipv4.tcp_tw_recycle = 1
Net.ipv4.ip_local_port_range = 1024 65000
Net.ipv4.tcp_max_syn_backlog = 8192
Net.ipv4.tcp_max_tw_buckets = 5000
Net.ipv4.tcp_syncookies = 1 means that Syn Cookies are turned on. When there is a SYN wait queue overflow, cookies are enabled to protect against a small number of SYN attacks, the default is 0, which means close;
Net.ipv4.tcp_tw_reuse = 1 means turn on reuse. Allows time-wait sockets to be re-used for new TCP connections, which defaults to 0, which means shutdown;
Net.ipv4.tcp_tw_recycle = 1 means a fast recycle of time-wait sockets in the TCP connection is turned on, and the default is 0, which means shutdown.
Net.ipv4.tcp_fin_timeout = 30 means that if the socket is closed by the local side, this parameter determines how long it remains in the fin-wait-2 state.
Net.ipv4.tcp_keepalive_time = 1200 indicates the frequency at which TCP sends keepalive messages when KeepAlive is employed. The default is 2 hours, which is changed to 20 minutes.
Net.ipv4.ip_local_port_range = 1024 65000 indicates the range of ports used for an outward connection. Small by default: 32768 to 61000, 1024 to 65000.
Net.ipv4.tcp_max_syn_backlog = 8192 Indicates the length of the SYN queue, the default is 1024, and the queue length is 8192, which can accommodate more network connections waiting to be connected.
Net.ipv4.tcp_max_tw_buckets = 5000 indicates that the system maintains the maximum number of time_wait sockets at the same time, and if this number is exceeded, the time_wait socket is immediately cleared and a warning message is printed. The default is 180000, which changes to 5000. For Apache, Nginx and other servers, the parameters of the last few lines can be a good way to reduce the number of time_wait sockets, but for squid, the effect is not small. This parameter controls the maximum number of time_wait sockets, preventing squid servers from being dragged to death by a large number of time_wait sockets.
The command to take effect immediately after changing the Linux kernel parameters!
/sbin/sysctl-p
Nginx optimization
Using the FASTCGI cache
Fastcgi_cache TEST
Turn on the fastcgi cache and set a name for it. The personal sense of unlocking the cache is useful to reduce CPU load and prevent 502 errors.
Fastcgi_cache_path/usr/local/nginx/fastcgi_cache Levels=1:2
keys_zone=test:10m
inactive=5m;
This instruction specifies a path for the fastcgi cache, a directory structure level, a keyword area to store time, and an inactive delete time.

Linux-related optimizations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.