nginx 负载均衡

发布于 / 1年前 - 更新于 / 1年前


NGINX的负载均衡设置

集群一直是系统稳定和分布式的首选,作为NGINX的使用者,理当理解NGINX的负载均衡。

docker 用惯了,做什么都想用docker了

首先配置环境,编写docker-compose.yml

version: '3' services: php1: hostname: php1 restart: always container_name: php1 image: php:7.3-fpm ports: - "9004:9001" volumes: # php项目文件夹挂载 - ../test1:/var/www/html php2: hostname: php2 restart: always container_name: php2 image: php:7.3-fpm ports: - "9002:9002" volumes: # php项目文件夹挂载 - ../test2:/var/www/html php3: hostname: php3 restart: always container_name: php3 image: php:7.3-fpm ports: - "9003:9003" volumes: # php项目文件夹挂载 - ../test3:/var/www/html nginx: hostname: nginx restart: always container_name: nginx image: nginx:1.17.0 ports: - "80:80" - "443:443" links: - "php1:php1" - "php2:php2" - "php3:php3" volumes: - ./nginx/conf.d:/etc/nginx/conf.d - ./nginx/logs:/var/log/nginx - ./nginx/data:/usr/share/nginx/html

以上我们创建3个PHP服务容器来负载我们的程序,创建一个NGINX来做负载均衡

nginx配置信息,conf.d/fuzai.conf

# 负载均衡服务器列表 upstream mytesthost { server 127.0.0.1:8081; server 127.0.0.1:8082; server 127.0.0.1:8083; } server { listen 80; server_name fuzai.test; location / { # 选择 mytesthost proxy_pass http://mytesthost; } } server { listen 8081; root /var/www/html/; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php?$query_string; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { root /var/www/html; # 解析php时 的root设置 fastcgi_pass php1:9000; #php为php容器名称 index index.php index.html fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } server { listen 8082; root /var/www/html/; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php?$query_string; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { root /var/www/html; # 解析php时 的root设置 fastcgi_pass php2:9000; #php为php容器名称 index index.php index.html fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } server { listen 8083; root /var/www/html/; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php?$query_string; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { root /var/www/html; # 解析php时 的root设置 fastcgi_pass php3:9000; #php为php容器名称 index index.php index.html fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

以上nginx配置了

// 8081端口的php代码 echo 8081; // 8082端口的php代码 echo 8082; // 8083端口的php代码 echo 8083;

此时我们访问 fuzai.test 时会轮询的出现 8081 8082 8083 的打印信息

主要配置

# 轮询方式 upstream mytesthost { server 127.0.0.1:8081; server 127.0.0.1:8082; server 127.0.0.1:8083; } # 权重方式 # weight 的值就按比例的权重来负载,如下总值为10(没有设置的默认为1) # 8081就为10%,8082就为40%,8083就为50% upstream mytesthost { server 127.0.0.1:8081; server 127.0.0.1:8082 weight=4; server 127.0.0.1:8083 weight=5; } # ip_hash 解决相同ip发送统一服务器,保证session会话不能夸服务器 upstream mytesthost { ip_hash; server 127.0.0.1:8081; server 127.0.0.1:8082 weight=4; server 127.0.0.1:8083 weight=5; } # least_conn 最少连接,轮询也算是平均分配,但是每个请求的时间是不等的,均分不确定。 # 最少连接,可以更好的均分 upstream mytesthost { least_conn; server 127.0.0.1:8081; server 127.0.0.1:8082 weight=4; server 127.0.0.1:8083 weight=5; } # 其他需要第三方插件的暂不介绍

本作品采用《CC 协议》,转载必须注明作者和本文链接