十二周五次课php
12.17 Nginx负载均衡
12.18 ssl原理
12.19 生成ssl密钥对
12.20 Nginx配置sslhtml
12.17 Nginx负载均衡mysql
upstream qq_comlinux
{nginx
ip_hash;web
server 61.135.157.156:80;算法
server 125.39.240.113:80;sql
}vim
serverwindows
{
listen 80;
server_name www.qq.com;
location /
{
proxy_pass http://qq_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;
}
}
1.配置负载均衡,负载均衡的配置借助了upstream 模块
2.这里将qq.com做为演示对象
[root@tianqi-01 ~]# yum install -y bind-utils
[root@tianqi-01 ~]# dig qq.com
; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7_4.2 <<>> qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 9571
;; 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. 331 IN A 125.39.240.113
qq.com. 331 IN A 61.135.157.156
;; Query time: 14 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: Thu Mar 15 22:06:46 CST 2018
;; MSG SIZE rcvd: 67
[root@tianqi-01 vhost]#
3.会看到返回出两个IP,这个就是域名解析,也就是qq.com解析到了两个IP上
4.这时候就能够用这两个125.39.240.113IP和61.135.157.156IP,去作负载均衡
5.写一个配置文件vim /usr/local/nginx/conf/vhost/load.conf
//写入如下内容
upstream qq_com //upstream后的名称自定义
{
ip_hash; //目的是为了让同一个用户始终保持在同一个机器上
server 61.135.157.156:80; //若是域名解析端口是80,这段配置上的指定端口80是能够省略的
server 125.39.240.113:80;
}
server
{
listen 80; //定义监听端口
server_name www.qq.com; //域名
location /
{
proxy_pass http://qq_com; //这里填写的是upstream 的名字
即“http://upstream”,由于做为一个模块,代理访问的是经过解析后的IP访问;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
保存退出
6.upstream来指定多个web server
7.在未加载配置的时候,本机去访问qq.com,回去访问默认虚拟主机
[root@tianqi-01 ~]# curl -x127.0.0.1:80 www.qq.com
This is the default site.
[root@tianqi-01 ~]#
8.测试访问qq.com
9.检查配置文件语法,并从新加载
[root@tianqi-01 ~]# /usr/local/nginx/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
[root@tianqi-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 ~]#
10.这时再来访问qq.com,会看到的是qq.com的主页,反馈回来的是网页的源码
11.这个就是负载均衡
[root@tianqi-01 vhost]# cat load.conf
upstream qq_com
{
ip_hash;
server 61.135.157.156:80;
server 125.39.240.113:80;
}
server
{
listen 80;
server_name www.qq.com;
location /
{
proxy_pass http://qq_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;
}
}
[root@tianqi-01 vhost]#
• cd /usr/local/nginx/conf
• openssl genrsa -des3 -out tmp.key 2048//key文件为私钥
• openssl rsa -in tmp.key -out aminglinux.key //转换key,取消密码
• rm -f tmp.key
• openssl req -new -key aminglinux.key -out aminglinux.csr//生成证书请求文件,须要拿这个文件和私钥一块儿生产公钥文件
• openssl x509 -req -days 365 -in aminglinux.csr -signkey aminglinux.key -out aminglinux.crt
• 这里的aminglinux.crt为公钥
在本身的虚拟机生成ssl 须要用到openssl工具
1.首先得有一个openssl工具
2.切换到/usr/local/nginx/conf/目录下
[root@tianqi-01 ~]# cd /usr/local/nginx/conf/
[root@tianqi-01 conf]#
3.如果没有openssl工具,能够安装下
4.查看openssl工具是由哪一个安装包安装的
[root@tianqi-01 conf]# rpm -qf `which openssl`
openssl-1.0.2k-8.el7.x86_64
[root@tianqi-01 conf]#
5.生成一个私钥,命令openssl genrsa -des3 -out tmp.key 2048
[root@tianqi-01 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: //输入密码123456
Verifying - Enter pass phrase for tmp.key: //再次输入密码123456
[root@tianqi-01 conf]#
6.在生成这个秘钥后比较麻烦,在nginx的配置文件里指定密码,每次访问浏览器,在https这个网址输入这个密码会很不方便,因此还须要去除这个密码
7.转换key,取消密码,命令 openssl rsa -in tmp.key -out gurui.key
[root@tianqi-01 conf]# openssl rsa -in tmp.key -out gurui.key
Enter pass phrase for tmp.key: //输入tmp.key的密码
writing RSA key
[root@tianqi-01 conf]#
8.这时候tmp.key和gurui.key是属于同一个
9.删除tmp.key
[root@tianqi-01 conf]# rm -f tmp.key
[root@tianqi-01 conf]#
10.生成证书请求文件,须要拿这个请求文件和私钥一块儿生产公钥文件
[root@tianqi-01 conf]# openssl req -new -key gurui.key -out gurui.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 //国家,2个字母
State or Province Name (full name) []:GuangDong //省或州
Locality Name (eg, city) [Default City]:ShenZhen //城市
Organization Name (eg, company) [Default Company Ltd]:cao //公司
Organizational Unit Name (eg, section) []:cao //组织
Common Name (eg, your name or your server's hostname) []:tianqi //您的主机名
Email Address []:cgjtaiyang@126.com //邮箱
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456 //设置密码123456
An optional company name []: //一个可选的公司名称
//用请求证书文件和私钥文件,生成一个公钥
[root@tianqi-01 conf]#
11.由于这是本身给本身颁发的证书,能够随意填写,如果购买那些正式的证书,那证书的信息就须要填写相对应的信息
12.生成公钥,命令openssl x509 -req -days 365 -in gurui.csr -signkey gurui.key -out gurui.crt
[root@tianqi-01 conf]# openssl x509 -req -days 365 -in gurui.csr -signkey gurui.key -out gurui.crt
Signature ok
subject=/C=cn/ST=GuangDong/L=ShenZhen/O=cao/OU=cao/CN=tianqi/emailAddress=cgjtaiyang@126.com
Getting Private key
[root@tianqi-01 conf]#
13.gui.crt是公钥,gurui.key是私钥
12.20 Nginx配置ssl
•vim /usr/local/nginx/conf/vhost/ssl.conf//加入以下内容
server
{
listen 443;
server_name aming.com;
index index.html index.php;
root /data/wwwroot/aming.com;
ssl on;
ssl_certificate aminglinux.crt;
ssl_certificate_key aminglinux.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}
• -t && -s reload //若报错unknown directive “ssl” ,须要从新编译nginx,加上--with-http_ssl_module
• mkdir /data/wwwroot/aming.com
• echo “ssl test page.”>/data/wwwroot/aming.com/index.html
• 编辑hosts,增长127.0.0.1 aming.com
• curl https://aming.com/
1.生成新的配置文件 vim /usr/local/nginx/conf/vhost/ssl.conf
[root@tianqi-01 ~]# vim /usr/local/nginx/conf/vhost/ssl.conf
添加如下内容
server
{
listen 443; //监听端口为443
server_name aming.com; //主机名
index index.html index.php;
root /data/wwwroot/aming.com; //root 目录
ssl on; //开启ssl
ssl_certificate gurui.crt; //指定公钥
ssl_certificate_key gurui.key; //指定私钥
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; //ssl 的协议
}
保存退出
2.建立/data/wwwroot/aming.com目录
[root@tianqi-01 ~]# mkdir /data/wwwroot/aming.com
[root@tianqi-01 ~]#
3.检查配置文件语法
[root@tianqi-01 ~]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@tianqi-01 ~]#
报错:
[root@tianqi-01 ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments: --prefix=/usr/local/nginx
[root@tianqi-01 ~]#
解决办法
4.从新编译nginx
[root@tianqi-01 ~]# cd /usr/local/src/nginx-1.12.1/
[root@tianqi-01 nginx-1.12.1]# ./configure --help |grep -i ssl
--with-http_ssl_module enable ngx_http_ssl_module
--with-mail_ssl_module enable ngx_mail_ssl_module
--with-stream_ssl_module enable ngx_stream_ssl_module
--with-stream_ssl_preread_module enable ngx_stream_ssl_preread_module
--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenSSL
[root@tianqi-01 nginx-1.12.1]#
5.初始化./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@tianqi-01 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
6.编译make && make install
[root@tianqi-01 nginx-1.12.1]# make && make install
7.查看nginx的编译参数,会看到增长了--with-http_ssl_module
[root@tianqi-01 nginx-1.12.1]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
[root@tianqi-01 nginx-1.12.1]#
8.检查配置文件语法错误
[root@tianqi-01 nginx-1.12.1]# /usr/local/nginx/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
[root@tianqi-01 nginx-1.12.1]#
9.重启nginx
[root@tianqi-01 nginx-1.12.1]# /etc/init.d/nginx restart
Restarting nginx (via systemctl): [ OK ]
[root@tianqi-01 nginx-1.12.1]#
10.查看监听端口,会看到多出一个443端口
[root@tianqi-01 nginx-1.12.1]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4439/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 807/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1049/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 4439/nginx: master
tcp6 0 0 :::22 :::* LISTEN 807/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1049/master
tcp6 0 0 :::3306 :::* LISTEN 1029/mysqld
[root@tianqi-01 nginx-1.12.1]#
11.切换目录路径,并建立一个测试文件
[root@tianqi-01 nginx-1.12.1]# cd /data/wwwroot/aming.com/
[root@tianqi-01 aming.com]# ls
[root@tianqi-01 aming.com]# vim index.html
This is ssl.
保存退出
12.测试,如果直接访问会报400,这种状况不对的
[root@tianqi-01 aming.com]# curl -x127.0.0.1:443 https://aming.com/
curl: (56) Received HTTP code 400 from proxy after CONNECT
[root@tianqi-01 aming.com]#
13.要直接访问,在虚拟机中 /etc/写hosts
[root@tianqi-01 aming.com]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.123.com www.0000000.com www.8888.com aming.com
192.168.11.136 www.123.com
14.测试,不指定-x访问
[root@tianqi-01 aming.com]# curl https://aming.com/
curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
[root@tianqi-01 vhost]#
15.在windows中的host文件添加,并保存
192.168.11.136 aming.com
16.浏览器访问aming.com,会看到以下画面
17.这时查看虚拟机防火墙iptables -nvL,如果防火墙存在,能够直接ipbables -F清空全部规则,若不想清空全部规则能够增长443端口的规则
[root@tianqi-01 aming.com]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1057 81940 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
2 120 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
1 52 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
224 33866 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 1113 packets, 174K bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-01 aming.com]# iptables -F
[root@tianqi-01 aming.com]#
18.这时再来访问https://aming.com,依然会提醒不安全,此时点击高级,继续访问,会出现 如下画面
19.这个就是本身颁发证书,浏览器不被信任的时候,会显示红色 不安全 ,而不是绿色
20.之后若想正常的访问https,能够去沃通买证书
友情连接:阿铭linux