Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with High concurrency in nginx and php

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to deal with high concurrency in nginx and php. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Main configuration of nginx

The load of the php-fpm server is realized through nginx. When the user accesses the service, the request is assigned to different php-fpm servers.

# nginx starts worker processes worker_processes auto; # Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes. Define the maximum number of files a process can open. The same as the linux kernel, you can define the priority of the worker process. The smaller the number, the higher the priority of the worker process. The lower the number, the higher the priority worker_priority-20; events {# The maximum number of connections that each worker process can handle simultaneously. The number of simultaneous connections per process worker_connections 10240; # If multi_accept is disabled, a worker process will accept one new connection at a time. Otherwise, a worker process will accept all new connections at a time. Multi_accept on;} http {access_log / var/log/nginx/access.log main buffer=32k; # Nginx will use the sendfile kernel to call to handle file delivery. Sendfile on; # requests are assigned to different servers according to weight. In the following configuration, when there are six requests, five are sent to the 9000 port server and one to the 9001 port server upstream phpload {server 127.0.0.1 weight=6; server 9000 weight=6; server 127.0.1 server 9001 weight=1;} server {listen 443; root / data/www/webserver Index index.php; location / {if (!-e $request_filename) {rewrite ^ (. *) $/ index.php?s=$1 last; break;}} location ~ .php ($| /) {set $script $uri; set $path_info "" If ($uri ~ "^ (. + .php) (/. +)") {set $script $1; set $path_info $2;} fastcgi_param SCRIPT_FILENAME $document_root$script; fastcgi_param SCRIPT_NAME $script; fastcgi_param PATH_INFO $path_info; try_files $uri = 404 # using payload to distribute requests to the upstream php server fastcgi_pass myfastcgi; fastcgi_index index.php; include fastcgi_params;}

Php-FPM is a PHP FastCGI manager, which is actually a patch for PHP source code, designed to introduce FastCGI process management into the PHP package, which we must patch into PHP source code before compiling before using it. Now we can turn it on and use it directly in PHP 5.3.2 and later, because PHP has incorporated it into the package from this version, so it is no longer a patch pack.

Main configuration of php-fpm

When pm is configured for dynamic, when php-fpm starts up to 100 child threads, the performance is the best according to the test results. Max_requests sets the number of get requests as large as possible

The php server listens on the port number listen = 127.0.0.1 pm 9001 pm = dynamicpm.max_children = 100pm.start_servers = 2pm.min_spare_servers = 2pm.max_spare_servers = 40; the php-fpm worker process automatically restarts the number of requests after it has finished processing pm.max_requests = 10240

Start the php-fpm instance

Php-fpm starts the instance and sets different configuration files, each of which listens for different port numbers

Php-fpm-y / etc/php-fpm.d/9001.conf

Php-fpm-h view help for more command usage

Php-fpm operation command

PID is the php-fpm instance master process number

Reload instance configuration

Kill-USR2 [PID]

Stop php-fpm calmly

Kill-QUIT [PID] so much about how to deal with high concurrency in nginx and php. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report