nginx代理和负载均衡
程序员文章站
2022-05-28 22:27:59
...
- web节点
[[email protected] ~]# cat /etc/nginx/conf.d/web.oldxu.com.conf
server {
listen 8080;
server_name web.oldxu.com;
root /web;
location / {
index index.html;
}
}
- 代理配置
[[email protected] ~]# cat /etc/nginx/conf.d/proxy_web.oldxu.com.conf
server {
listen 80;
server_name web.oldxu.com;
location / {
proxy_pass http://10.0.0.7:8080;
}
}
代理参数
[[email protected] ~]# cat /etc/nginx/conf.d/proxy_web.oldxu.com.conf
server {
listen 80;
server_name web.oldxu.com;
location / {
proxy_pass http://10.0.0.7:80;
include proxy_params;
}
}
[[email protected] ~]# cat /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
负载均衡
[[email protected] ~]# cat /etc/nginx/conf.d/proxy_web.oldxu.com.conf
upstream web {
server 10.0.0.7:8000;
server 10.0.0.8:8000;
}
server {
listen 80;
server_name web.oldxu.com;
location / {
proxy_pass http://web;
include proxy_params;
}
}
后端web配置 (所有的web都需要配置一致)
[[email protected] conf.d]# cat web.oldxu.com.conf
server {
listen 8000;
server_name web.oldxu.com;
root /web;
location / {
index index.html;
}
}
[[email protected] conf.d]# cat web.oldxu.com.conf
server {
listen 8000;
server_name web.oldxu.com;
root /web;
location / {
index index.html;
}
}
blog zh 集成
1.确保所有的web节点都安装好了blog或者zh 并得确保是能正常访问的
2.接入负载均衡
如果出现问题:
将负载均衡--->代理到指定的一台服务器 测试 在换一台 测试
配置如下:
[[email protected] conf.d]# cat proxy_blog.oldxu.com.conf
upstream blog {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name blog.oldxu.com;
location / {
proxy_pass http://blog;
include proxy_params;
}
}
[[email protected] conf.d]# cat blog.oldxu.com.conf
server {
listen 80;
server_name blog.oldxu.com;
root /code/wordpress;
client_max_body_size 100m;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[[email protected] conf.d]# cat proxy_zh.oldxu.com.conf
upstream zh {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name zh.oldxu.com;
location / {
proxy_pass http://zh;
include proxy_params;
}
}
[[email protected] conf.d]# cat zh.oldxu.com.conf
server {
listen 80;
server_name zh.oldxu.com;
root /code/zh;
client_max_body_size 100m;
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
负载均衡调度算法
调度算法 | 概述 |
---|---|
轮询 | 按时间顺序逐一分配到不同的后端服务器(默认) |
weight | 加权轮询,weight值越大,分配到的访问几率越高 4 16G 8 64G |
ip_hash | 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器 |
url_hash | 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器 |
least_conn | 最少链接数,那个机器链接数少就分发 |
2.后端的节点状态指标
状态 | 概述 |
---|---|
down | 当前的server暂时不参与负载均衡 |
backup | 预留的备份服务器 |
max_fails | 允许请求失败的次数 |
fail_timeout | 经过max_fails失败后, 服务暂停时间 |
max_conns | 限制最大的接收连接数 |
[[email protected] conf.d]# cat proxy_web.oldxu.com.conf
upstream web {
server 10.0.0.7:80 max_conns=1000 max_fails=3 fail_timeout=10s;
server 10.0.0.8:80 max_conns=1000 max_fails=3 fail_timeout=10s;
keepalive 16; #最大的空闲连接数
keepalive_timeout 100s; #超时时间
keepalive_requests 50; #一个连接最大的请求数
#负载均衡与后端的连接和请求数的限制
#注意:需要配置 proxy_http_version proxy_set_header Connection "";
}
server {
listen 80;
server_name web.oldxu.com;
location / {
proxy_pass http://web;
include proxy_params;
}
}
会话保持
置一个网站:
1.测试session是否存储至于本地
2.配置多个web节点
3.接入负载均衡 --->轮询 -->正常登陆
第一步: 记得测试
[[email protected] ~]# cat /etc/nginx/conf.d/phpmyadmin.oldxu.com.conf
server {
listen 80;
server_name phpmyadmin.oldxu.com;
root /code/phpmyadmin;
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
第二步配置phpmyadmin
[[email protected] ~]# cd /code/phpmyadmin/
[[email protected] phpmyadmin]# cp config.sample.inc.php config.inc.php
第三步修改配置文件(修改为需要连接的数据库IP地址)
[[email protected] phpmyadmin]# vim config.inc.php
$cfg['Servers'][$i]['host'] = '172.16.1.51';
第四步配置第二个节点: 记得测试
第五步: 接入负载均衡
[[email protected] ~]# cat /etc/nginx/conf.d/proxy_phpmyadmin.oldxu.com.conf
upstream php {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name phpmyadmin.oldxu.com;
location / {
proxy_pass http://php;
include proxy_params;
}
}