Processing math: 100%

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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
应用型负载均衡 ALB,每月750个小时 15LCU
简介:

使用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 'remoteaddrhttp_x_forwarded_for [timelocal]http_host sslprotocolrequest requestbodyrequest_time upstreamresponsetimestatus upstreamstatusbody_bytes_sent '
' "httpreferer""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/2056431

相关实践学习
小试牛刀,一键部署电商商城
SAE 仅需一键,极速部署一个微服务电商商城,体验 Serverless 带给您的全托管体验,一起来部署吧!
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
打赏
0
0
0
0
344
分享
相关文章
今日小结通过aliyun的本地容器镜像部署我的nginx和php环境
简介: 本教程介绍如何基于 Dragonwell 的 Ubuntu 镜像创建一个运行 Nginx 的 Docker 容器。首先从阿里云容器镜像服务拉取基础镜像,然后编写 Dockerfile 确保 Nginx 作为主进程运行,并暴露 80 端口。最后,在包含 Dockerfile 的目录下构建自定义镜像并启动容器,确保 Nginx 在前台运行,避免容器启动后立即退出。通过 `docker build` 和 `docker run` 命令完成整个流程。
145 24
今日小结通过aliyun的本地容器镜像部署我的nginx和php环境
一文读懂什么是Nginx?它能否实现IM的负载均衡?
Nginx(及其衍生产品)是目前被大量使用的服务端反向代理和负载均衡方案,从某种意义上来讲,Nginx几乎是低成本、高负载Web服务端代名词。 如此深入人心的Nginx,很多人也想当然的认为,在IM或消息推送等场景下是否也能使用Nginx来解决负载均衡问题? 另外,即时通讯网的论坛和QQ群里也经常有人问起,Nginx是否能支持TCP、UDP、WebSocket的负载
91 4
Nginx中配置HTTP2协议的方法
Nginx中配置HTTP2协议的方法
321 7
Nginx进程配置指令详解
Nginx进程配置指令主要包括:`worker_processes`设置工作进程数;`worker_cpu_affinity`绑定CPU核心;`worker_rlimit_nofile`设置最大文件描述符数量;`worker_priority`设置进程优先级;`worker_connections`设置最大连接数;`daemon`控制守护进程模式;`master_process`启用主进程模式;`pid`设置PID文件路径;`user`指定用户和组;`error_log`配置错误日志。这些指令在`nginx.conf`中配置,用于优化和控制Nginx的运行行为。
87 10
Nginx长连接负载均衡详细说明以及案例
本文详细介绍了Nginx长连接负载均衡的配置与原理。长连接(Keepalive)允许客户端和服务器保持连接,减少建立和关闭连接的开销。Nginx支持多种负载均衡算法,如轮询、IP哈希等。通过在Nginx配置文件中使用`upstream`模块和`keepalive`指令,可以实现长连接负载均衡,从而提高系统的性能和响应速度。示例配置展示了如何设置后端服务器组、长连接数及HTTP/1.1协议,确保连接复用,降低延迟。
124 5
当你的nginx服务器和php服务器不在一起的时候,这个nginx 的root目录问题
两个服务器的网站代码目录需要对齐,docker容器里面也是一样
nginx配置证书和私钥进行SSL通信验证
nginx配置证书和私钥进行SSL通信验证
155 4
配置Nginx反向代理时如何指定后端服务器的权重?
配置Nginx反向代理时如何指定后端服务器的权重?
308 61
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
343 60