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