nginx负载均衡笔记TCP保持长连接的

开篇特别提醒:开启nginx的时候,有可能会报错

发现报以下错误:
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory)

执行这个就可以解决:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

nginx 自定义负载均衡笔记:

特别提醒:1-4的步骤如果存在无需下载

安装所需环境

Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境。

一. gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install gcc-c++

二. PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel

三. zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel

四. OpenSSL 安装 //注意 这个是为了支持https 协议
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel

五.下载nginx安装包:
wget http://nginx.org/download/nginx-1.7.2.tar.gz 安装包
tcp 支持安装包:https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master.zip

解压

依然是直接命令:
tar -zxvf nginx-1.7.2.tar.gz

六.配置、编译安装 首先进入你解压的nginx目录中(我是在 /alidata/server/nginx 目录下)
1、添加TCP服务
patch -p1 < /mingshine/nginx_tcp_proxy_module-master/tcp.patch
1、 ./configure --add-module=/mingshine/nginx_tcp_proxy_module-master/ ( 特别提醒 :因为我们要用到TCP转发和HTTPS 所以我们必须要使用这个命令,如果不需要的话,用./configure 这个命令即可,但是我们还是下配置把,反正有总比没有好)
如果报这个错误:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl= option.

卸载掉 openssl yum remove  openssl-devel

2、make
3、make install

查找安装路径:
whereis nginx

以上我们算是安装完成了nginx服务
现在我们需要配置下:

1、nginx开机自启动

即在rc.local增加启动代码就可以了。
vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx

设置执行权限:(进入/etc目录)
chmod 755 rc.local

2、nginx 命令如下:

cd /usr/local/nginx/sbin/  (先进入nginx安装目录)
./nginx 
./nginx -s stop
./nginx -s quit
./nginx -s reload


./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

查询nginx进程:
ps aux|grep nginx

第二步:配置nginx.cof (我的目录在:/usr/local/nginx/conf)

因为我们要支持https转发所以我们需要做一下操作:
连接:http://www.cnblogs.com/saneri/p/5391821.html

1.首先确保机器上安装了openssl和openssl-devel 这个我们之前就安装了所以可以忽略
#yum install openssl
#yum install openssl-devel

2.创建服务器私钥,命令会让你输入一个口令:
openssl genrsa -des3 -out server.key

3.创建签名请求的证书(CSR):
openssl req -new -key server.key -out server.csr

4.在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key

5.配置nginx
最后标记证书使用上述私钥和CSR:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

我的配置信息如下:

#==================================================================================================

#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 {
timeout 1d;
proxy_read_timeout 10d;
proxy_send_timeout 10d;
proxy_connect_timeout 30;
upstream ssh_116 {
# simple round-robin
server 127.0.0.1:9797;
server 10.30.90.111:9797;
check interval=3000 rise=2 fall=5 timeout=1000;
#check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;
#check interval=3000 rise=2 fall=5 timeout=1000 type=http;
#check_http_send “GET / HTTP/1.0\r\n\r\n”;
#check_http_expect_alive http_2xx http_3xx;
}
server {
listen 63005;
proxy_pass ssh_116;
so_keepalive on;
tcp_nodelay on;
}
}

http {
include mime.types;
default_type application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;


upstream servers.http.mingshine.com { 
	server 127.0.0.1:8080;  
	server 10.30.90.111:8080; 
}


server{

listen 60180; 
server_name http.mignshine.com; 
location / { 
	proxy_pass http://servers.http.mingshine.com; 
	proxy_set_header Host $host; 
	proxy_set_header X-Real-IP $remote_addr; 
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
	} 
}


upstream servers.https.mingshine.com { 
	server 127.0.0.1:9898;
	server 127.0.0.1:8888;

}


server{

listen 60443; 
server_name https.mingshine.com; 
ssl                  on;        
ssl_certificate      /usr/local/nginx/sbin/server.crt;      
ssl_certificate_key  /usr/local/nginx/sbin/server.key;      
ssl_session_timeout  5m;
ssl_protocols  SSLv2 SSLv3 TLSv1;        
ssl_ciphers  HIGH:!aNULL:!MD5;              
ssl_prefer_server_ciphers   on;             

location / { 
	proxy_pass https://servers.https.mingshine.com; 
	proxy_set_header Host $host; 
	proxy_set_header X-Real-IP $remote_addr; 
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
	} 
}



# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

}

#==================================================================================================

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值