php think 负载,Swoole+ThinkPHP5.0——负载均衡

本文介绍了如何在Linux环境下安装Nginx并配置负载均衡,通过权重算法将请求分发到多个Swoole服务。首先,详细阐述了Nginx的安装过程,包括依赖库的安装和配置文件的编辑。然后,展示了如何设置Nginx转发规则,使得无法找到静态资源的请求被转发到Swoole服务。最后,通过在Nginx配置中添加upstream块,实现了负载均衡,确保请求能够均匀分布到多个服务器。测试结果显示,负载均衡工作正常,不同请求可以被分配到不同的服务器上运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Swoole+ThinkPHP5.0——负载均衡

羡仙. • 2019 年 06 月 26 日

安装依赖包# Nginx源码安装依赖gcc环境

yum install -y gcc-c++

# 安装正则表达式库

yum install -y pcre pcre-devel

# 安装zlib,可以支持Nginx的gzip

yum install -y zlib zlib-devel

# 安装安全密码库,简单理解就是支持https

yum install -y openssl openssl-devel

# 一句话的事

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

Nginx安装

编译安装参数参数说明prefix安装目录

sbin-path启动Nginx服务需要的文件.

conf-path配置文件路径,文件名

error-log-path错误日志文件路径,文件名

pid-path进程pid设置的文件路径,文件名

http-log-path请求日志的文件路径,文件名

编译安装# 官网下载最新版本

cd /data/softpackage/

# 2019-06-25发布的最新版本1.17.1

wget -c https://nginx.org/download/nginx-1.17.1.tar.gz

# 解压

tar -zxvf nginx-1.17.1.tar.gz

cd nginx-1.17.1

# 配置

./configure --prefix=/user/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/config/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --pid-path=/user/local/nginx/logs/nginx.pid --http-log-path=/usr/local/nginx/logs/access.log

# 编译安装

make && make install

端口开放vim /etc/sysconfig/iptables

# 添加

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 8000:9999 -j ACCEPT

# 重启iptabels

service iptables restart

# 重启防火墙,建议都执行一下

// 即时生效,重启生效

service iptables start

// 重启后生效

chkconfig iptables off

chkconfig iptables on

Nginx使用

启动cd /usr/local/nginx/

# 编辑配置文件

vim config/nginx.conf

# 去掉注视

error_log logs/error.log;

error_log logs/error.log notice;

error_log logs/error.log info;

pid logs/nginx.pid;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;

# 保存退出,启动nginx

./sbin/nginx

# 查看是否启动成功

netstat -anp | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 86817/nginx: master

静态资源vim /usr/local/nginx/config/nginx.conf

# 修改root,静态资源路径

server {

listen 80;

server_name localhost;

location / {

root /data/wwwroot/liveTelecast/public/static/;

index index.html index.htm;

}

...

}

Nginx转发到Swoole# 修改nginx配置

vim /usr/local/nginx/config/nginx.conf

-- 思想

# 在location添加判断,目录下没有静态资源转发到swoole

# 访问swoole服务

curl 192.168.128.131:9503/index/index/qvbilam

# 返回

123Array

(

[success] => 0

[error_phone_empty] => 100001

[error_phone_type] => 100002

[error_code_empty] => 100003

[error_code] => 100004

[error_sms_error] => 100005

[error_phone_code_empty] => 100006

[error_login_set] => 100007

[error_upload_image] => 100008

[error_perfect] => 100009

[error_perfect_token] => 100010

[error_perfect_empty] => 100010

[error_admin_game_id_empty] => 100011

[error_admin_game_data_empty] => 100012

)

qvbilam%

# 访问nginx服务

curl 192.168.128.131

# 返回

hello!羡仙.

# 修改nginx配置

vim /usr/local/nginx/config/nginx.conf

# 添加proxy_pass做转发

location / {

root /usr/local/nginx/html;

index index.html index.htm;

if (!-e $request_filename) {

proxy_pass http://127.0.0.1:9503;

}

}

# 重启nginx访问nginx服务

curl 192.168.128.131/index/index/qvbilam

# 返回9503到swoole服务内容

123Array

(

[success] => 0

[error_phone_empty] => 100001

[error_phone_type] => 100002

[error_code_empty] => 100003

[error_code] => 100004

[error_sms_error] => 100005

[error_phone_code_empty] => 100006

[error_login_set] => 100007

[error_upload_image] => 100008

[error_perfect] => 100009

[error_perfect_token] => 100010

[error_perfect_empty] => 100010

[error_admin_game_id_empty] => 100011

[error_admin_game_data_empty] => 100012

)

qvbilam%

负载均衡

对于初学者来说这个东西听起来很高大深奥,其实也没啥牛逼的.只需修改nginx.conf加个几行代码就能实现的东西.

Nginx配置# 修改nginx.conf

vim /usr/local/nginx/config/nginx.conf

# 在http中添加upstream,修改proxy_pass转发

http {

...

upstream qvbilam_swoole{

server 192.168.128.131:9503 weight=2;

server 192.168.128.132:9503 weight=1;

server 192.168.128.133:9503 weight=1;

server 192.168.128.134:9503 weight=1;

}

server {

...

root /usr/local/nginx/html;

index index.html index.htm;

if (!-e $request_filename) {

# proxy_pass http://127.0.0.1:9503;

proxy_pass http://qvbilam_swoole;

}

}

}

# 重启nginx服务

#

测试// 添加新的测试方法

vim application/index/controller/Index.php

public function test()

{

// 添加不同的返回内容

}

设置返回内容服务器返回内容192.168.128.131QvBilam 第一台虚拟机

192.168.128.132QvBilam 第二台虚拟机

192.168.128.133QvBilam 第三台虚拟机

192.168.128.134QvBilam 第四台虚拟机

测试结果➜ /Users/qvbilam curl 192.168.128.131/index/index/test

QvBilam 第一台虚拟机% ➜ /Users/qvbilam curl 192.168.128.131/index/index/test

QvBilam 第一台虚拟机% ➜ /Users/qvbilam curl 192.168.128.131/index/index/test

QvBilam 第二台虚拟机% ➜ /Users/qvbilam curl 192.168.128.131/index/index/test

QvBilam 第三台虚拟机% ➜ /Users/qvbilam curl 192.168.128.131/index/index/test

QvBilam 第四台虚拟机% ➜ /Users/qvbilam curl 192.168.128.131/index/index/test

QvBilam 第一台虚拟机%

关于负载均衡

nginx负载均衡一共有五种,本次采用的是权重算法,131 weight=2; // 访问概率2/6

132 weight=1; // 访问概率1/6

133 weight=1; // 访问概率1/6

134 weight=2; // 访问概率2/6

还有一种通过ip分配到不同的服务器,比如同一个ip访问会一直第一次到的机器上,不会跑到别的机器.upstream qvbilam_swoole{

ip_hash;

server 192.168.128.131:9503;

server 192.168.128.132:9503;

server 192.168.128.133:9503;

server 192.168.128.134:9503;

}

在以后的Nginx篇将会详细介绍各种算法的用法.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值