使用nginx配置多个php fastcgi负载均衡

简介:

使用nginx配置多个php fastcgi负载均衡

服务器环境是一键安装包lnmp
nginx 配置文件
参考地址:http://blog.chinaunix.net/uid-26284395-id-3084491.html

nginx 配置文件
[root@cacti vhost]# cat /usr/local/nginx/conf/vhost/www.test.conf 
log_format apilog '$remote_addr $http_x_forwarded_for [$time_local] $http_host $ssl_protocol $request $request_body '
' $request_time $upstream_response_time $status $upstream_status $body_bytes_sent '
' "$http_referer" "$http_user_agent" $upstream_addr';
upstream fpm-pool {
server 127.0.0.1:9000;
server 127.0.0.1:9001;
keepalive 4;
}

server
{
listen 80;
server_name www.test.com hadxpg.test.com;
index index.html index.htm index.php;
root /var/www/html/mtoyyweb;

#limit_conn crawler 20; 
#error_page 404

location ~ .*.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass fpm-pool;
fastcgi_index index.php;
include fastcgi.conf;

}

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*.(js|css)?$
{
expires 1h;
}

location /5998153NginxStatus
{
stub_status on;
access_log off;
}

access_log /data/wwwlogs/access_log;
error_log /data/wwwlogs/error.log;
}

[root@cacti vhost]# cat /usr/local/nginx/conf/vhost/fpmstatus.conf 
server {
listen 127.0.0.1:15001;
location ~^/fpmstatus$ {
#auth_basic "status page";
#fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
#include fastcgi_params;
include fastcgi.conf;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
}
location ~^/fpm2status$ {
#auth_basic "status page";
#fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
#include fastcgi_params;
include fastcgi.conf;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9001;
}
}

[root@cacti vhost]# cat /usr/local/php/etc/php-fpm.conf
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /data/wwwlogs/fpm-err.log
log_level = notice
events.mechanism = epoll
;process.priority = -19

[www]
listen = 127.0.0.1:9000
listen.backlog = 65535
listen.allowed_clients = 127.0.0.1
listen.owner = apache
listen.group = apache
listen.mode = 0666
user = apache
group = apache
pm = dynamic
pm.status_path = /fpmstatus
pm.max_children = 1000
pm.start_servers = 48
pm.min_spare_servers = 24
pm.max_spare_servers = 96

access.format = "%R - %u %t %T \"%m %r%Q%q\" %s %f %d"
access.log = /data/wwwlogs/pool.access.log

request_terminate_timeout = 60
;request_slowlog_timeout = 2
;slowlog = /data/wwwlogs/fpm-slow.log

[root@cacti vhost]# cat /usr/local/php/etc/php-fpm2.conf 
[global]
pid = /usr/local/php/var/run/php-fpm2.pid
error_log = /data/wwwlogs/fpm2-err.log
log_level = notice
events.mechanism = epoll

[www]
listen = 127.0.0.1:9001
listen.backlog = 10000
listen.allowed_clients = 127.0.0.1
listen.owner = apache
listen.group = apache
listen.mode = 0666
user = apache
group = apache
pm = dynamic
pm.status_path = /fpm2status
pm.max_children = 1000
pm.start_servers = 48
pm.min_spare_servers = 24
pm.max_spare_servers = 96
request_terminate_timeout = 60
;request_slowlog_timeout = 2
;slowlog = /data/wwwlogs/fpm2-slow.log

测试php-fpm.conf 配置文件正确性:

[root@cacti vhost]# /usr/local/php/sbin/php-fpm -t -y /usr/local/php/etc/php-fpm2.conf 
[01-Jan-2018 19:15:14] NOTICE: configuration file /usr/local/php/etc/php-fpm2.conf test is successful

[root@cacti vhost]# /usr/local/php/sbin/php-fpm -t -y /usr/local/php/etc/php-fpm.conf 
[01-Jan-2018 19:15:18] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful

[root@cacti vhost]# grep php-fpm2.conf /etc/init.d/php-fpm2 
php_fpm_CONF=${prefix}/etc/php-fpm2.conf
[root@cacti vhost]# grep php-fpm.conf /etc/init.d/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf

[root@cacti vhost]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done

[root@cacti vhost]# /etc/init.d/php-fpm2 restart
Gracefully shutting down php-fpm . done
Starting php-fpm done

开启错误日志查看到负载均衡日志:
[root@cacti vhost]# tail -4 /data/wwwlogs/mtoyy_error.log
2018/01/01 19:12:53 [error] 27486#0: 792819 FastCGI sent in stderr: "PHP message: PHP Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/html/mtoyyweb/include/mysql.class.php on line 24" while reading response header from upstream, client: 61.135.194.48, server: www.uuopp.com, request: "GET /flow_bx.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.uuopp.com", referrer: "http://www.uuopp.com/index.php"
2018/01/01 19:19:02 [error] 27485#0: 
792821 FastCGI sent in stderr: "PHP message: PHP Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/html/mtoyyweb/include/mysql.class.php on line 24" while reading response header from upstream, client: 61.135.194.48, server: www.uuopp.com, request: "GET /wapads.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "www.uuopp.com", referrer: "http://www.uuopp.com/flow_bx.php"
2018/01/01 19:19:03 [error] 27485#0: 792821 FastCGI sent in stderr: "PHP message: PHP Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/html/mtoyyweb/include/mysql.class.php on line 24" while reading response header from upstream, client: 61.135.194.48, server: www.uuopp.com, request: "GET /introduce.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.uuopp.com", referrer: "http://www.uuopp.com/wapads.php"
2018/01/01 19:19:04 [error] 27485#0: 
792821 FastCGI sent in stderr: "PHP message: PHP Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/html/mtoyyweb/include/mysql.class.php on line 24" while reading response header from upstream, client: 61.135.194.48, server: www.uuopp.com, request: "GET /flow_bx.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "www.uuopp.com", referrer: "http://www.uuopp.com/introduce.php"


 本文转自 wjw555 51CTO博客,原文链接:http://blog.51cto.com/wujianwei/2056430


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
相关文章
|
缓存 负载均衡 监控
135_负载均衡:Redis缓存 - 提高缓存命中率的配置与最佳实践
在现代大型语言模型(LLM)部署架构中,缓存系统扮演着至关重要的角色。随着LLM应用规模的不断扩大和用户需求的持续增长,如何构建高效、可靠的缓存架构成为系统性能优化的核心挑战。Redis作为业界领先的内存数据库,因其高性能、丰富的数据结构和灵活的配置选项,已成为LLM部署中首选的缓存解决方案。
1219 25
|
缓存 负载均衡 算法
如何配置Nginx反向代理以实现负载均衡?
如何配置Nginx反向代理以实现负载均衡?
1625 152
|
负载均衡 前端开发 应用服务中间件
Tomcat的负载均衡和动静分离(与nginx联动)
总的来说,负载均衡和动静分离是提高Web应用性能的两个重要手段。通过合理的配置和使用,我们可以让Web应用更好地服务于用户。
581 21
|
弹性计算 负载均衡 网络协议
配置SLB监听器
配置SLB监听器
1101 63
|
域名解析 弹性计算 监控
slb测试基本配置检查
slb测试基本配置检查
557 60
|
域名解析 监控 网络协议
slb配置域名注意事项
slb配置域名注意事项
539 11
|
弹性计算 负载均衡 监控
slb配置健康检查
slb配置健康检查
616 5
|
弹性计算 负载均衡 算法
slb 配置不当
【11月更文挑战第2天】
763 10
|
监控 负载均衡 容灾
slb测试配置
slb测试配置
591 5
|
安全 PHP 开发者
php中配置variables_order详解
`variables_order` 是 PHP 配置中的一个关键指令,它决定了不同来源的变量被导入到全局变量空间的顺序。正确配置 `variables_order` 不仅可以确保变量的正确处理和覆盖顺序,还能提高应用程序的安全性。开发者应根据具体应用的需求,合理配置 `variables_order`,确保应用的稳定和安全运行。
350 5