十二周五次课
2019獨角獸企業重金招聘Python工程師標準>>>
十二周五次課
12.17 Nginx負載均衡
12.18 ssl原理
12.19 生成ssl密鑰對
12.20 Nginx配置ssl
12.17 Nginx負載均衡
Nginx負載均衡目錄概要
- vim /usr/local/nginx/conf/vhost/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;
??? }
}
- upstream來指定多個web server
Nginx負載均衡
- 代理一臺機器稱為代理 ,代理兩臺機器就可以稱為負載均衡
- 代理服務器后面可以有多個web服務器,多個web服務器去提供服務的時候,就可以實現一個負載均衡的功能
- 正常情況下,用戶訪問web服務器,是一臺一臺去請求;要么就是指定一個IP,把這域名解析到多臺服務器上
- 案例
- 用戶1 –> web1服務器
- 用戶2 –> web2服務器
- 假設這時web1服務器掛掉了(宕機),用戶1因為解析到了web1,但web1宕機了,所以就無法正常訪問;
- 這時候若是用nginx的負載均衡,在web1宕機后,代理服務器就不會把請求發送給web1,這就是代理的一個優點,負載均衡的優點。
1.配置負載均衡,負載均衡的配置借助了upstream 模塊
2.這里將qq.com作為演示對象
- dig命令查看解析的IP——>yum install -y bind-utils
[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
- 當有多個服務器同時對一個域名提供服務的時候,長時間訪問一個域名,在一定的時效內,會出現需要重新登錄或者是說跳轉到另外一個地址的服務器上;ip_hash,就是使通過這個代理訪問的同一個域名的多個IP的服務器時,始終保持在一個IP上對這個域名進行訪問。
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]#?
nginx代理和負載均衡的知識點
- nginx不支持去代理https,也就是在配置文件中的server后不能寫:443,是不支持的,只能代理http、tcp
- 若想要實現代理https,nginx監聽443端口,但web服務必須是80端口
12.18ssl原理
ssl原理
- https的相關知識點
- 要配置nginx和https,就需要首先去了解https是什么?
- 在訪問一些網站的時候,會自動加上了https前標
- http和https的區別
- https通信是加密的,如果不加密,中間傳輸數據包的有時候會被截到,就會導致信息泄露,https就是對這個通信的數據包進行加密
- SSL工作流程
- 瀏覽器發送一個https的請求給服務器;
- 服務器要有一套數字證書,可以自己制作(后面的操作就是阿銘自己制作的證書),也可以向組織申請,區別就是自己頒發的證書需要客戶端驗證通過,才可以繼續訪問,而使用受信任的公司申請的證書則不會彈出提示頁面,這套證書其實就是一對公鑰(加密)和私鑰(解密);
- 服務器會把公鑰傳輸給客戶端;
- 客戶端(瀏覽器)收到公鑰后,(這個過程是瀏覽器判斷的)會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數,并用收到的公鑰加密;
- 客戶端把加密后的隨機字符串傳輸給服務器;
- 服務器收到加密隨機字符串后,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數后,再用這串隨機字符串加密傳輸的數據(該加密為對稱加密,所謂對稱加密,就是將數據和私鑰也就是這個隨機字符串通過某種算法混合在一起,這樣除非知道私鑰,否則無法獲取數據內容);
- 服務器把加密后的數據傳輸給客戶端;
- 客戶端收到數據后,再用自己的私鑰也就是那個隨機字符串解密;
12.19 生成ssl密鑰對
生成ssl密鑰對目錄概要
? 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密鑰對
在自己的虛擬機生成ssl 需要用到openssl工具
- 在虛擬上頒發一套證書,生成ssl
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]#?
- openssl genrsa -des3 -out tmp.key 2048
- genrsa ,表示生成rsa的私鑰
- 2048 ,2048長度
- 名字為 tmp.key
- 生成這個秘鑰必須要有密碼
6.在生成這個秘鑰后比較麻煩,在nginx的配置文件里指定密碼,每次訪問瀏覽器,在https這個網址輸入這個密碼會很不方便,所以還需要去除這個密碼
7.轉換key,取消密碼,命令 openssl rsa -in tmp.key -out gurui.key
- -in 表示指定哪一個秘鑰要被轉換
- -out 表示指定輸出的
[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是屬于同一個
- 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]#?
- -days 365 證書的日期是一年
13.gui.crt是公鑰,gurui.key是私鑰
12.20 Nginx配置ssl
- 在有了公鑰和私鑰之后,配置nginx
?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 的協議
}
保存退出
- 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 ~]#?
報錯:
- 因為不知道這個ssl 配置,先前在編譯nginx的時候,并沒有額外配置支持SSL的參數
[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 ~]#?
解決辦法
- 重新編譯nginx
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]#?
- 編譯的時候需要加上--with-http_ssl_module
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
轉載于:https://my.oschina.net/u/3744518/blog/1636127
總結
- 上一篇: 和平精英全陀螺仪好处 下载2022年最新
- 下一篇: 淘宝app如何删除评价(淘宝海外全球站首