Nginx四层负载反代MySQL

1、安装Nginx并启用模块

Nginx从1.9.0开始发布ngx_stream_core_module模块,该模块支持tcp代理及负载均衡

  • 安装Nginx并启动模块

  ngx_stream_core_module这个模块默认没有启用,需要在便宜时通过指定with-strem参数激活这个模块

环境准备

[[email protected] /]# yum -y install pcre pcre-devel gcc gcc-c++ openssl openssl-devel

创建nginx用户

[[email protected] /]# useradd nginx -s /sbin/nologin -M

下载软件

[[email protected] /]# cd /usr/local/src/
[[email protected] src]# wget http://nginx.org/download/nginx-1.9.4.tar.gz
--2017-04-13 10:05:08--  http://nginx.org/download/nginx-1.9.4.tar.gz
Resolving nginx.org... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3, ...
Connecting to nginx.org|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 866423 (846K) [application/octet-stream]
Saving to: “nginx-1.9.4.tar.gz”

100%[=================================================================================>] 866,423     16.2K/s   in 36s     

2017-04-13 10:05:50 (23.3 KB/s) - “nginx-1.9.4.tar.gz” saved [866423/866423]

解压

[[email protected] src]# tar zxvf nginx-1.9.4.tar.gz
[[email protected] src]# cd nginx-1.9.4

配置、编译、安装

[[email protected] nginx-1.9.4]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream
[[email protected] nginx-1.9.4]# echo $?
0
[[email protected] nginx-1.9.4]# make && make install
[[email protected] nginx-1.9.4]# echo $?
0

启动服务

[[email protected] nginx-1.9.4]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[[email protected] nginx-1.9.4]# ll /usr/local/sbin/
total 0
lrwxrwxrwx 1 root root 27 Apr 13 13:29 nginx -> /usr/local/nginx/sbin/nginx
[[email protected] nginx-1.9.4]# /usr/local/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected] nginx-1.9.4]# /usr/local/sbin/nginx
[[email protected] nginx-1.9.4]# netstat -nlput | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1271/nginx
[[email protected] nginx-1.9.4]# lsof -i :80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1271  root    6u  IPv4   9108      0t0  TCP *:http (LISTEN)
nginx   1272 nginx    6u  IPv4   9108      0t0  TCP *:http (LISTEN)
[[email protected] nginx-1.9.4]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.9.4
Date: Thu, 13 Apr 2017 05:37:43 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 13 Apr 2017 03:16:08 GMT
Connection: keep-alive
ETag: "58eeed78-264"
Accept-Ranges: bytes

代理MySQL

[[email protected] nginx-1.9.4]# cd /usr/local/nginx/conf/
[[email protected] conf]# echo  >nginx.conf
[[email protected] conf]# vim nginx.conf
worker_processes auto;
events {
    worker_connections  1024;
}
error_log /var/log/nginx_error.log info;

stream {
    upstream mysqld {
        hash $remote_addr consistent;
        server 172.19.10.98:3306 weight=5 max_fails=1 fail_timeout=10s;
#        server 192.168.1.43:3306 weight=5 max_fails=1 fail_timeout=10s;
    }

    server {
        listen 3306;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass mysqld;
    }

}

[[email protected] conf]# /usr/local/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected] conf]# cat nginx.conf
worker_processes auto;
events {
    worker_connections  1024;
}
error_log /var/log/nginx_error.log info;

stream {
    upstream mysqld {
        hash $remote_addr consistent;
        server 172.19.10.98:3306 weight=5 max_fails=1 fail_timeout=10s;
#        server 172.19.10.94:3306 weight=5 max_fails=1 fail_timeout=10s;
    }

    server {
        listen 3306;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass mysqld;
    }

}
时间: 2024-10-10 14:09:18

Nginx四层负载反代MySQL的相关文章

LVS四层 VS Nginx七层反代(负载均衡)

1.场景: 最近不少朋友聊天谈到面试总是问到LVS四层反代和Nginx七层反代,有点不知所措. 负载均衡可以将用户的请求分发到 web集群的某台机器,反向代理也是实现了这个功能.如果后端是一台服务器就叫反向代理,如果有多台就是负载均衡.反向代理才能实现负载均衡 负载均衡是做反向代理的目的之一. 2.博主则探讨一下四层反代和的七层反代的区别(三层负载均衡不探讨),如有不足,请指出. 3.问到LVS四层.七层反代(负载均衡),则可以通过上面几个方式谈: 3.1)技术原理 3.2)优缺点 3.3)安全

Nginx+Keepalived 实现反代 负载均衡 高可用(HA)配置

Nginx+Keepalived实现反代负载均衡高可用(HA)配置 Nginx+Keepalived实现反代负载均衡高可用配置 OS IP 子网掩码 路由网关 Centos6.6 nginx Keepalived Eth0:192.168.26.210 255.255.252.0 192.168.25.3 VIP:192.168.27.210 Centos6.6 Nginx Keepalived Eth0:192.168.26.211 255.255.252.0 192.168.25.3 VIP

nginx做前端反代负载均衡,后端httpd+tomcat

实验内容:用nginx做前端反代负载均衡后端httpd+tomcat 实验环境:物理机win7,虚拟机centos7: node1:172.18.11.111 httpd+tomcat node2:172.18.11.112 httpd+tomcat node3:172.18.11.113 nginx反代负载均衡 说明:httpd有两种方式与tomcat通信: (1)httpd可使用http模块反代tomcat,此时tomcat使用http链接器: (2)httpd还可使用ajp模块反代tomc

nginx四层负载均衡配置

nginx四层负载均衡配置代理Mysql集群 环境如下: ip 192.168.6.203 Nginx ip 192.168.6.*(多台) Mysql 步骤一 查看Nginx是否安装stream模块 没安装则进行安装 操作步骤如下 至此 已保证在没中断服务的情况下成功添加stream模块 步骤二 配置 mysql负载均衡案例 修改Nginx配置文件nginx.conf 内容如下图 测试步骤如下 后端Mysql需做好读写分离 创建好相应权限的用户 到客户端连接Nginx创建wuguiyunwei

nginx四层负载及动静分离

阿里云实验10.0.0.132 LB10.0.0.133 web0110.0.0.134 web02 步骤:1.安装nginx1.14 获取官网repo文件 yum install -y nginx 2.配置文件,web服务器都是一样的 [[email protected] conf.d]# cat www.conf server { listen 80; server_name ali.xiao.com; location / { root /code/ali; index index.htm

nginx做为反代服务器相关配置

1.反代的模型 反代服务器实现反代功能的是 nginx-proxy 模块 虚拟服务器组实现是由 upstream 模块实现的 大概流程是:客户端发起资源请求,反代服务器上接收后(首部和body全部接收后再发,若报文小于4k内存中缓存,大于4k先缓存在硬盘上),nginx开始解析报文(此时报文源地址是客户的地址),先查看请求的资源知否在反代缓存中命中,若未命中,则自己作为客户端向服务器发起请求(此时报文源地址是反代服务器地址) 那么如何让后端服务器记录真实的客户地址呢,反代中可配置x-forwar

linux+asp.net core+nginx四层负载均衡

Linux Disibutaion:Ubuntu 16.04.1 LTS Web Server:Nginx.Kestrel 关于如何在linux中部署asp.net core我这里不再详细介绍,可以参考ASP.Net Core 运行在Linux(Ubuntu) 这里我需要三台VM:nginxvm01:做负载均衡服务器nginxvm02:服务节点1nginxvm03:服务节点2 大致服务架构如下图: 配置负载均衡节点(nginxvm01): sudo apt-get install -y ngin

Nginx反代MySQL案例

案例:一个朋友要用Nginx代理MySQL(MySQL局域网),不用VPN,不用NAT映射等,好吧,做个笔记. Nginx版本:1.9.x(持tcp的负载均衡,nginx_tcp_proxy_module(姚伟斌阿里团队也可以实现)) Nginx官方模块: ngx_stream_core_module --with-stream_ssl_module(ssl协议支持,比如MySQL ssl) 官网:http://nginx.org/en/docs/stream/ngx_stream_core_m

配置Nginx四层负载均衡

nginx 支持TCP转发和负载均衡的支持 实现下面的架构: 看配置: #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } tcp { upstream toov5{ server 1