Nginx负载均衡
网站的访问量越来越大,服务器的服务模式也得进行相应的升级,比如分离出数据库服务器、分离出图片作为单独服务,这些是简单的数据的负载均衡,将压力分散到不同的机器上。有时候来自web前端的压力,也能让人十分头痛。怎样将同一个域名的访问分散到两台或更多的机器上呢?这其实就是另一种负载均衡了,nginx自身就可以做到,只需要做个简单的配置就行。
nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx还可以按照调度规则实现动态、静态页面的分离,可以按照轮询、ip哈希、URL哈希、权重等多种方式对后端服务器做负载均衡,同时还支持后端服务器的健康检查。
1.Nginx负载均衡一些基础知识:
- nginx 的 upstream目前支持 4 种方式的分配
- 轮询(默认): 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
- weight : 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
- ip_hash : 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
- fair(第三方): 按后端服务器的响应时间来分配请求,响应时间短的优先分配。
- url_hash(第三方)
2.nginx负载均衡配置,主要是proxy_pass,upstream的使用
1)dig命令:是常用的域名查询工具,可以用来测试域名系统工作是否正常。
yum install -y bind-utils
执行效果:
[root@yolks2 vhost]# dig qq.com
; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29871
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;qq.com. IN A
;; ANSWER SECTION:
qq.com. 177 IN A 111.161.64.40
qq.com. 177 IN A 111.161.64.48
;; Query time: 13 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 六 8月 18 22:16:12 CST 2018
;; MSG SIZE rcvd: 67
2)新编辑配置文件,目录为**/usr/local/nginx/conf/vhost/load.conf**,文件内容如下所示:
upstream qq_com #此处名称可自定义
{
ip_hash; #保持访问时始终在一台机器上,而不会是1页面一个ip,2页面1个ip.
server 61.135.157.156:80; #ip:sort,如果是域名是端口可以省略掉
server 125.39.240.113:80; #server可以配置多个
}
server
{
listen 80;#监听端口
server_name www.qq.com; #监听域名
location /
{
proxy_pass http://qq_com; #此处需要填写upstream名称
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
ip_hash是容易理解的,但是因为仅仅能用ip这个因子来分配后端,因此ip_hash是有缺陷的,不能在一些情况下使用:
- **nginx不是最前端的服务器。**ip_hash要求nginx一定是最前端的服务器,否则nginx得不到正确ip,就不能根据ip作hash。譬如使用的是squid为最前端,那么nginx取ip时只能得到squid的服务器ip地址,用这个地址来作分流是肯定错乱的。
- **nginx的后端还有其它方式的负载均衡。**假如nginx后端又有其它负载均衡,将请求又通过另外的方式分流了,那么某个客户端的请求肯定不能定位到同一台session应用服务器上。这么算起来,nginx后端只能直接指向应用服务器,或者再搭一个squid,然后指向应用服务器。最好的办法是用location作一次分流,将需要session的部分请求通过ip_hash分流,剩下的走其它后端去。
3)先不重新加载配置curl腾讯主页 www.qq.com, 提示访问默认页
[root@yolks2 vhost]# curl -x127.0.0.1:80 www.qq.com
This is a default site.
4)重新加载新配制的load.conf文件再看效果:反馈回了qq.com主页的html页面源码
ssl原理
即https加密访问
生成ssl密钥对
在当前虚拟机模拟生成ssl密钥对
1.进入/usr/local/nginx/conf目录
cd /usr/local/nginx/conf
2.查询openssl包
[root@yolks2 conf]# rpm -qf `which openssl `
openssl-1.0.2k-12.el7.x86_64
3.生成类型为rsa格式的私钥
关键代码:
openssl genrsa -des3 -out tmp.key 2048 #key文件为私钥
实践,确认输入密码123456,因为用户每次访问不可能都去输入密码,所以我们下面转换key去掉密码;
[root@yolks2 conf]# openssl genrsa -des3 -out tmp.key 2048
Generating RSA private key, 2048 bit long modulus
........................................................................................+++
.............................................................................................+++
e is 65537 (0x10001)
Enter pass phrase for tmp.key:
Verifying - Enter pass phrase for tmp.key:
4.转换key,取消密码
openssl rsa -in tmp.key -out yolkslinux.key
输入和tmp.key文件相同的密码确认
[root@yolks2 conf]# openssl rsa -in tmp.key -out yolkslinux.key
Enter pass phrase for tmp.key:
writing RSA key
5.删除tmp.key文件
rm -f tmp.key
6.生成证书请求文件,需要拿文件和和私钥一起生产公钥文件
openssl req -new -key yolkslinux.key -out yolkslinux.csr
过程中需要填写一些信息,因为是本地模拟,我们根据自己情况填写即可。
[root@yolks2 conf]# openssl req -new -key yolkslinux.key -out yolkslinux.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:BeiJing
Locality Name (eg, city) [Default City]:ChaoYang
Organization Name (eg, company) [Default Company Ltd]:Yolks
Organizational Unit Name (eg, section) []:yolks
Common Name (eg, your name or your server's hostname) []:yolks
Email Address []:superyolks@vip.qq.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:yolks
An optional company name []:yolks
7.设置证书有效期等信息:1年有效期
crt文件为公钥,key文件为私钥
openssl x509 -req -days 365 -in yolkslinux.csr -signkey yolkslinux.key -out yolkslinux.crt
执行效果如下:
[root@yolks2 conf]# openssl x509 -req -days 365 -in yolkslinux.csr -signkey yolkslinux.key -out yolkslinux.crt
Signature ok
subject=/C=cn/ST=BeiJing/L=ChaoYang/O=Yolks/OU=yolks/CN=yolks/emailAddress=superyolks@vip.qq.com
Getting Private key
Nginx配置ssl
1.虚拟主机下创建新配置文件/usr/local/nginx/conf/vhost/ssl.conf
server
{
listen 443;
server_name aming.com;
index index.html index.php;
root /data/wwwroot/yolks.com;
ssl on; #开启ssl即支持https
ssl_certificate yolkslinux.crt; #指定公钥
ssl_certificate_key yolkslinux.key; #指定私钥
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #协议
}
2.创建/data/wwwroot/yolks.com目录
mkdir /data/wwwroot/yolks.com
3.检查测试配置文件
/usr/local/nginx/sbin/nginx -t
错误提示 :nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7 错误原因:没有安装相对应的ssl配置 解决方法:重新编译,添加ssl参数
1.添加编译参数
./configure --prefix=/usr/local/nginx --with-http_ssl_module #添加对应模块参数编译
2.重新编译安装
make && make install
4.重新启动Nginx,查看端口号是否有ssl.conf中配置的443端口
[root@yolks2 vhost]# netstat -lntp |grep 443
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 3824/nginx: master
5.在/data/wwwroot/yolks.com创建测试文件
[root@yolks2 yolks.com]# vim index.html
[root@yolks2 yolks.com]# cat !$
cat index.html
this is the ssl test page!
6.修改虚拟机/etc/hosts文件
[root@yolks2 yolks.com]# cat !$
cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 yolks.com #添加网址dns
7.curl测试
[root@yolks2 yolks.com]# curl -x127.0.1:443 https://yolks.com/
curl: (56) Received HTTP code 400 from proxy after CONNECT #
Windows上浏览器访问,需要暂时iptables -F清空规则,再次尝试
提示不安全,则点击继续访问
因为是自己颁发的ssl证书,所以提示红色不安全标识,例如12306网站自己颁发
ssl证书申请推荐:沃通、阿里云(个人ssl有免费的证书使用)
拓展
针对请求的uri来代理 http://ask.apelearn.com/question/1049
根据访问的目录来区分后端的web http://ask.apelearn.com/question/920
nginx长连接 http://www.apelearn.com/bbs/thread-6545-1-1.html
nginx算法分析 http://blog.sina.com.cn/s/blog_72995dcc01016msi.html