Nginx Configuration and Cluster

Source: Internet
Author: User

One: Compile Nginx, and configure

cd/app/pcre-8.12

./configure

Make && make install

Cd nginx-1.2.7

./configure--prefix=/usr/local/nginx--add-module=/app/ngx_http_consistent_hash-master

Note: The Red Line part is nginx 3rd party module, need to download.

Installation of statistical modules for easy observation of nginx status

./configure--prefix=/usr/local/nginx/--add-module=/app/ngx_http_consistent_hash-master--with-http_stub_status_ Module

PHP installation Configuration

1 tar-xzvf/path/'

2 cd/path/

3. Configure--prefix=/usr/local/php--

server cluster and load balancer built

1: Problem c-->a MySQL connection is slow

FIX: In the [mysqld] node in my.cnf, add

Skip-name-resolve//This sentence causes the MySQL connection to ignore resolves the domain name, in the development MySQL permission, only according to the IP limit, cannot according to the domain name limit.

2: Problem when there is no corresponding data in Memcache, callback data from the background,

HTTP status code is 404, (although the content is normal), this is not conducive to SEO

Solution: nginx/conf/nginx.conf

Error_page 404 =200/callback.php; So 404 is rewritten into a 200来 response.

Stress test:

The first 100,000 of the simulation is thermal data,

10.2 million is the unpopular data

Request hot Data 0-10, request 9 times

Request permission to request data 1 times,-----1 million requests.

Optimization ideas:

Nginx Response Request

1: Establish socket connection

2: Open the file and return along the socket.

To troubleshoot the problem, you should also observe these two points,

Mainly from the system of DMESG, and Nginx Error.log to observe

Optimization process

1: Determine the bottleneck of nginx

1.1: First, the performance of the AB Test side is improved, so that it can be high concurrent requests.

Easy to issue: too many open files

Cause: AB in the stress test, open the socket too much

Resolution: Ulimit-n 30000 (restart disabled)

Observation Result: Nginx does not need special optimization case, 5,000 connections, response within 1 seconds.

Satisfies the requirements, but the wating state has too many connections.

1.2: Solve the problem of excessive waiting process.

Workaround: keepalive_timeout = 0;

That is, the TCP connection is not preserved after the result is requested.

In the case of high concurrency, the keepalive will occupy a large number of socket connections.

Results: The connection of the waiting state was significantly reduced.

1.3: Resolve Server Too many open files

Analysis: Nginx to respond,

1 is to establish a socket connection,

2 is to read local file

These two were limited.

As can be seen, nginx problem easily at 2 points:

1:nginx accept more TCP connections, can build up?

2:nginx response process, to open many files, can open? Ah, yes.

Question 1th: At the kernel level (see below)

Question 2nd (see below)

System Kernel Level:

Net.core.somaxconn = 4096 allow waiting for monitoring

net.ipv4.tcp_tw_recycle = 1 TCP connection Fast Recycle

Net.ipv4.tcp_tw_reuse = 1 TCP connection Reuse

Net.ipv4.tcp_syncookies = 0 does not withstand flood attacks

Ulimit-n 30000

Nginx level:

Resolution: Nginx.conf below: work_connection enlargement

Worker_connections 10240;

Worker_rlimit_nofiles 10000;

Keepalive_timeout 0;

The optimization between Nginx---->PHP-FPM

For example, when many nginx accesses FPM, the FPM process is not enough to generate a child process.

Generating child processes requires the kernel to dispatch, which is time-consuming,

If the Web site is larger and more concurrent,

We can generate several sub-processes at once in a static way and remain in memory.

Method--Modify Php-fpm.conf

Pm = static keeps the FPM process at all times and does not generate dynamically

Pm.max_children= 32 Number of child processes always maintained

Optimization of Php-mysql

Linux machine, PHP through the IP connection to other MySQL server, easy to issue

Can ping can, but connect not to.

This is generally caused by: the firewall of the MySQL server is affected.

Concurrent 10,000 connection, the response time is too long.

Optimization ideas: Nginx, ibid.

1: At the kernel level, increase the number of connections and speed up TCP recovery

2:mysql level, increase the number of connections

3:php level, with long connection, save connection number

4: Reduce MySQL burden with memcached cache

Specific:

1.1, PHP server increase ulimint-n option

1.2 MySQL server kernel configuration

Add or modify the following options

Net.ipv4.tcp_tw_recycle = 1

Net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_syncookies = 0

# Syscttl-p make changes effective immediately

2.1 Modifying MYSQL.CNF

Vi/etc/my.conf

# service mysqld Restart restart MySQL

3.1 php level, with long connections

Mysql_connect---> Mysql_pconnect

Note: Pconnect has no effect when PHP is in the form of an Apache module.

Nginx+phjp+mysql+nginx

After introduction of memcached, the performance improvement is not obvious, even slightly decreased

Memcached makes 50% of requests faster, but part of it is slow.

The reason is that--php->memcached also has to establish a TCP connection, the cost is very high,

But after the data is cached, the query time for MySQL is eliminated.

Summary: memcached is suitable for SQL results with complex SQL, especially connection queries/fuzzy queries

memcached Server Optimization (centralized on IPv4 settings of the kernel, no longer duplicates)

Nginx Configuration and Cluster

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.