五十、Nginx負載均衡、SSL原理、生成SSL密鑰對、Nginx配置SSL
一、Nginx負載均衡
代理一臺機器叫代理,代理兩臺機器就可以叫負載均衡。
代理服務器後有多個web服務器提供服務的時候,就可以實現負載均衡的功能。
dig命令:解析域名的IP。常用的域名查詢工具,可以用來測試域名系統工作是否正常,可以反饋多個IP。
需要安裝這個包:# yum install -y bind-utils
# 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: 15958
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;qq.com. IN A
;; ANSWER SECTION:
qq.com. 176 IN A 14.17.32.211
;; Query time: 26 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 六 4月 28 02:32:44 CST 2018
;; MSG SIZE rcvd: 51
# ping qq.com
PING qq.com (14.17.32.211) 56(84) bytes of data.
# cd /usr/local/nginx/conf/vhost/
[root@MRX vhost]# vim load.conf
upstream qq_com //這個名字可以自定義,這個名字代表著下面的兩歌IP。
{
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; 做代理時是指定它的IP,此處不能寫IP就只能寫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;
}
}
測試:
# curl -x127.0.0.1:80 www.qq.com 正常情況下,會訪問默認虛擬主機。
This is the defaule site.
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 www.qq.com 再次訪問時,訪問的就是qq的主頁。
反饋回來的是網頁的源碼。
知識點:
Nginx不支持代理https,就是server 125.39.240.113:80冒號後面不能寫443,只能代理httpd,新版本能代理tcp。
如果一定要代理https,訪問web服務器的80端口,但不支持443。
web服務器提供https服務,只能是後端訪問的時候是80,代理服務器可以監聽443,Nginx訪問後端服務器的時候,訪問它的80端口,但是不支持訪問它的443。
二、SSL原理
https的通信是加密的,如果中間被黑客截取,黑客獲取到的數據包是不能解密的,看到的是亂碼。
HTTPS:一種加密的HTTP協議,HTTP通信的數據包傳輸過程中被截獲,可以破譯數據包裏面的信息;而使用HTTPS通信,即使被截獲,也無法破譯裏面的內容。
SSL工作流程:
1.瀏覽器發送一個https的請求給服務器;
2.服務器要有一套數字證書,可以自己制作(後面的操作就是阿銘自己制作的證書),也可以向組織申請,區別就是自己頒發的證書需要客戶端驗證通過,才可以繼續訪問,而使用受信任的公司申請的證書則不會彈出>提示頁面,這套證書其實就是一對公鑰和私鑰;
3.服務器會把公鑰傳輸給客戶端;
4.客戶端(瀏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數,並用收到的公鑰加密;
5.客戶端把加密後的隨機字符串傳輸給服務器;
6.服務器收到加密隨機字符串後,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數後,再用這串隨機字符串加密傳輸的數據(該加密為對稱加密,所謂對稱加密,就是將數據和私鑰也就是這個隨機字符串>通過某種算法混合在一起,這樣除非知道私鑰,否則無法獲取數據內容);
7.服務器把加密後的數據傳輸給客戶端;
8.客戶端收到數據後,再用自己的私鑰也就是那個隨機字符串解密;
三、生成SSL密鑰對
# cd /usr/local/nginx/conf
# rpm -qf `which openssl` 查要這個openssl的包的名字,默認應該安裝了
openssl-1.0.2k-8.el7.x86_64
# 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:
//key文件為私鑰。
//genrsa:生成rsa形式的私鑰;2048:長度;名字是tmp.key。
# openssl rsa -in tmp.key -out mrx.key
Enter pass phrase for tmp.key:
writing RSA key
//轉換key,取消密碼 。
//-in:指定要被轉換的密鑰,-out:指定要輸出的密鑰。
//此時tmp.key和mrx.key實際上是一樣的,只是tmp.key有密碼,mrx.key沒密碼
# rm -f tmp.key //然後刪除有密碼的。
# openssl req -new -key mrx.key -out mrx.csr(請求文件)
//生成證書請求文件,需要拿這個文件和私鑰一起生成公鑰文件
Country Name (2 letter code) [XX]:11
State or Province Name (full name) []:changsha
Locality Name (eg, city) [Default City]:changsha
Organization Name (eg, company) [Default Company Ltd]:MRX
Organizational Unit Name (eg, section) []:MRX
Common Name (eg, your name or your server's hostname) []:MRX111
Email Address []:[email protected]
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:12345
An optional company name []:MRX
# openssl x509 -req -days 365 -in mrx.csr -signkey mrx.key -out mrx.crt
//這裏的mrx.crt為公鑰
Signature ok
subject=/C=11/ST=changsha/L=changsha/O=MRX/OU=MRX/CN=MRX111/[email protected]
Getting Private key
//這樣最終才生成了CRT證書文件。
四、Nginx配置SSL
# cd /usr/local/nginx/conf/vhost/
[root@MRX vhost]# vim ssl.conf
server
{
listen 443;
server_name mrx.com;
index index.html index.php;
root /data/wwwroot/mrx.com;
ssl on; //開啟ssl,支持https。
ssl_certificate mrx.crt; //指定公鑰
ssl_certificate_key mrx.key; //指定私鑰
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; //協議,一般三種都配置上
}
# mkdir /data/wwwroot/mrx.com
# /usr/local/nginx/sbin/nginx -t //報錯,不知道ssl的配置,因為之前編譯Nginx時沒有指定支持ssl
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
# cd /usr/local/src/nginx-1.8.0/
# ./configure --help|grep -i ssl
--with-http_ssl_module enable ngx_http_ssl_module //需要加上標紅的這個參數
--with-mail_ssl_module enable ngx_mail_ssl_module
--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenSSL
# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
# make && make install
# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.8.0
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@MRX nginx-1.8.0]# /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
# /etc/init.d/nginx restart
Restarting nginx (via systemctl): [ 確定 ]
# 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 3952/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 946/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1301/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 3952/nginx: master
tcp6 0 0 :::22 :::* LISTEN 946/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1301/master
tcp6 0 0 :::3306 :::* LISTEN 1261/mysqld
多了一個443。
# cd /data/wwwroot/mrx.com/
# vim index.html //做一個測試文件
This is ssl.
# curl -x 127.0.0.1:443 https://mrx.com
curl: (56) Received HTTP code 400 from proxy after CONNECT
# vim /etc/hosts
127.0.0.1 mrx.com
# curl https://mrx.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下面還有內容沒有貼出來。
編輯Windows中c盤的hosts文件,加上192.168.93.130 mrx.com
再瀏覽器訪問https://mrx.com,如果訪問不到,查看有無防火墻。
有的話,簡單的方法是直接iptables -F,不想清空就加一條443端口的規則,再次瀏覽器訪問,就會彈出來不安全,還想繼續訪問,就點高級,繼續前往。
證書不被信任的時候就會報不安全。
沃通:購買SSL證書的網站。
五十、Nginx負載均衡、SSL原理、生成SSL密鑰對、Nginx配置SSL