1. 程式人生 > >nginx四層負載均衡配置

nginx四層負載均衡配置

mysql nginx四層負載均衡配置 客戶端 配置文件 stream

技術分享

nginx四層負載均衡配置代理Mysql集群

環境如下:

  1. ip 192.168.6.203 Nginx

  2. ip 192.168.6.*(多臺) Mysql

步驟一

查看Nginx是否安裝stream模塊

技術分享

沒安裝則進行安裝 操作步驟如下

技術分享

技術分享

技術分享

至此 已保證在沒中斷服務的情況下成功添加stream模塊

步驟二

配置 mysql負載均衡案例

修改Nginx配置文件nginx.conf 內容如下圖

技術分享

測試步驟如下

  1. 後端Mysql需做好讀寫分離

  2. 創建好相應權限的用戶

  3. 到客戶端連接Nginx創建wuguiyunwei庫進行測試

在客戶端連接 創建測試庫

技術分享

技術分享

連接3307讀庫查看 成功如下

技術分享

技術分享

當然為了高可用以下才是我們想要的效果

技術分享

以上配置只是為了讓大家了解stream模塊。當然也可以用於生產環境,但還需完善工作如節點down剔除,完善的一些監控工作。。。

以下是實驗環境的nginx主配文件

user www www;
worker_processes auto;

error_log /usr/local/nginx/logs/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
use epoll;
worker_connections 51200;
multi_accept on;
}

stream {

server {
listen 3306;
proxy_pass Mysql_write;
}

server {
listen 3307;
proxy_pass Mysql_read;
}

upstream Mysql_write {
server 192.168.6.19:3306 weight=10;
server 192.168.6.20:3306 weight=10;
server 192.168.6.18:3306 weight=10;
}
upstream Mysql_read {
server 192.168.6.175:3306 weight=10;
server 192.168.6.176:3306 weight=10;
server 192.168.6.177:3306 weight=10;
}

}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 1024m;
client_body_buffer_size 10m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_intercept_errors on;

gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable “MSIE [1-6]\.(?!.*SV1)”;

open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
########################## vhost
include vhost/*.conf;
}

以上文章來自 烏龜運維 wuguiyunwei.com

我們的微信公共號

技術分享

QQ群 602183872


nginx四層負載均衡配置