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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

使用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


相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
2天前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
49 0
|
2天前
|
负载均衡 应用服务中间件 API
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
45 4
|
1天前
|
Kubernetes 负载均衡 应用服务中间件
深入理解 Kubernetes Ingress:路由流量、负载均衡和安全性配置
深入理解 Kubernetes Ingress:路由流量、负载均衡和安全性配置
|
1天前
|
负载均衡 应用服务中间件 nginx
解决nginx配置负载均衡时invalid host in upstream报错
在Windows环境下,配置Nginx 1.11.5进行负载均衡时遇到问题,服务无法启动。错误日志显示“invalid host in upstream”。检查发现上游服务器列表中,192.168.29.128的主机地址无效。负载均衡配置中,两个服务器地址前误加了"http://"。修正方法是删除上游服务器列表和proxy_pass中的"http://"。问题解决后,Nginx服务应能正常启动。
31 4
解决nginx配置负载均衡时invalid host in upstream报错
|
2天前
|
应用服务中间件 PHP nginx
php如何实现检测nginx配置的正确性
请确保在执行此操作时,PHP有足够的权限来执行Nginx命令和访问Nginx配置文件。另外,将上述代码嵌入到您的应用程序中时,要注意安全性,以防止潜在的命令注入攻击。
53 3
|
2天前
|
负载均衡 网络协议 应用服务中间件
【亮剑】在Linux中构建高可用性和高性能网络服务的负载均衡工具HAProxy、Nginx和Keepalived。
【4月更文挑战第30天】本文介绍了在Linux中构建高可用性和高性能网络服务的负载均衡工具HAProxy、Nginx和Keepalived。HAProxy是一个高性能的开源TCP和HTTP负载均衡器,适合处理大量并发连接;Nginx是一个多功能Web服务器和反向代理,支持HTTP、HTTPS和TCP负载均衡,同时提供缓存和SSL功能;Keepalived用于监控和故障切换,通过VRRP实现IP热备份,保证服务连续性。文中详细阐述了如何配置这三个工具实现负载均衡,包括安装、配置文件修改和启动服务,为构建可靠的负载均衡系统提供了指导。
|
2天前
|
运维 JavaScript Serverless
Serverless 应用引擎产品使用之在阿里函数计算中,Php环境,配置取消禁止函数exec如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
50 4
|
2天前
|
运维 负载均衡 Cloud Native
Serverless 应用引擎产品使用之在Serverless 应用引擎中,使用云原生网关的情况下,SLB(负载均衡器)和证书配置如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
26 1
|
2天前
|
负载均衡 Ubuntu 应用服务中间件
Apache(mod_proxy)+Tomcat负载均衡配置
Apache(mod_proxy)+Tomcat负载均衡配置
|
2天前
|
负载均衡 算法 网络协议
LVS、Nginx和HAProxy负载均衡器对比总结
LVS、Nginx和HAProxy负载均衡器对比总结