网站的访问量愈来愈大,服务器的服务模式也得进行相应的升级,好比分离出数据库服务器、分离出图片做为单独服务,这些是简单的数据的负载均衡,将压力分散到不一样的机器上。有时候来自web前端的压力,也能让人十分头痛。怎样将同一个域名的访问分散到两台或更多的机器上呢?这其实就是另外一种负载均衡了,nginx自身就能够作到,只须要作个简单的配置就行。
nginx不单能够做为强大的web服务器,也能够做为一个反向代理服务器,并且nginx还能够按照调度规则实现动态、静态页面的分离,能够按照轮询、ip哈希、URL哈希、权重等多种方式对后端服务器作负载均衡,同时还支持后端服务器的健康检查。php
1.Nginx负载均衡一些基础知识:html
2.nginx负载均衡配置,主要是proxy_pass,upstream的使用前端
1)dig命令:是经常使用的域名查询工具,能够用来测试域名系统工做是否正常。linux
yum install -y bind-utils
执行效果:nginx
[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**,文件内容以下所示:web
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是有缺陷的,不能在一些状况下使用:算法
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页面源码vim
即https加密访问后端
在当前虚拟机模拟生成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
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