1. 程式人生 > >2018-04-27 Linux學習

2018-04-27 Linux學習

Linux學習

12.17 Nginx負載均衡

只能代理http

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

dig工具
yum -y install bind-utils

[root@linux-01 ~]# curl -x127.0.0.1:80 www.qq.com
This is default site.

[root@linux-01 ~]# vim /usr/local/nginx/conf/vhost/load.conf

upstream qq
{
ip_hash;
server 14.17.32.211:80;
server 14.17.42.40:80;
}
server
{
listen 80;
server_name www.qq.com;
location /
{
proxy_pass http://qq;

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@linux-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@linux-01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@linux-01 ~]# curl -x127.0.0.1:80 www.qq.com    //顯示www.qq.com的網頁內容

12.18 ssl原理

SSL工作流程

瀏覽器發送一個https的請求給服務器;
 服務器要有一套數字證書,可以自己制作(後面的操作就是阿銘自己制作的證書),也可以向組織申請,區別就是自己頒發的證書需要客戶端驗證通過,才可以繼續訪問,而使用受信任的公司申請的證書則不會彈出>提示頁面,這套證書其實就是一對公鑰和私鑰;

 服務器會把公鑰傳輸給客戶端;

 客戶端(瀏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數,並用收到的公鑰加密;

 客戶端把加密後的隨機字符串傳輸給服務器;

 服務器收到加密隨機字符串後,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數後,再用這串隨機字符串加密傳輸的數據(該加密為對稱加密,所謂對稱加密,就是將數據和私鑰也就是這個隨機字符串>通過某種算法混合在一起,這樣除非知道私鑰,否則無法獲取數據內容);

 服務器把加密後的數據傳輸給客戶端;

 客戶端收到數據後,再用自己的私鑰也就是那個隨機字符串解密;

12.19 生產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為公鑰

生成過程

[root@linux-01 ~]# cd /usr/local/nginx/conf/
[root@linux-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:
Verifying - Enter pass phrase for tmp.key:

[root@linux-01 conf]# openssl rsa -in tmp.key -out aminglinux.key
Enter pass phrase for tmp.key:
writing RSA key

[root@linux-01 conf]# rm -rf tmp.key

[root@linux-01 conf]# openssl req -new -key aminglinux.key -out aminglinux.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]:11
State or Province Name (full name) []:www
Locality Name (eg, city) [Default City]:sz
Organization Name (eg, company) [Default Company Ltd]:www
Organizational Unit Name (eg, section) []:www
Common Name (eg, your name or your server‘s hostname) []:wwwhost
Email Address []:[email protected]

Please enter the following ‘extra‘ attributes
to be sent with your certificate request
A challenge password []:www123456
An optional company name []:wwwchina

[root@linux-01 conf]# openssl x509 -req -days 365 -in aminglinux.csr -signkey aminglinux.key -out aminglinux.crt
Signature ok
subject=/C=11/ST=www/L=sz/O=www/OU=www/CN=wwwhost/[email protected]
Getting Private 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/

操作過程

[root@linux-01 conf]# cd vhost/

[root@linux-01 vhost]# mkdir /data/wwwroot/aming.com

[root@linux-01 vhost]# vim ssl.conf
添加上面的配置內容

[root@linux-01 vhost]# /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@linux-01 vhost]# cd /usr/local/src/nginx-1.12.2/
[root@linux-01 nginx-1.12.2]# ./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@linux-01 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module

[root@linux-01 nginx-1.12.2]# make
[root@linux-01 nginx-1.12.2]# make install

[root@linux-01 nginx-1.12.2]# cd /data/wwwroot/aming.com/
[root@linux-01 aming.com]# vim 1.txt
This is default site.

[root@linux-01 aming.com]# /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@linux-01 aming.com]# /usr/local/nginx/sbin/nginx -s reload

[root@linux-01 aming.com]# curl -x127.0.0.1:443 https://aming.com/
curl: (7) Failed connect to 127.0.0.1:443; 拒絕連接

[root@linux-01 aming.com]# vim /etc/hosts
127.0.0.1   aming.com

[root@linux-01 aming.com]# curl https://aming.com/
curl: (7) Failed connect to aming.com:443; 拒絕連接

電腦端直接顯示  This is default site.  有不安全提示

2018-04-27 Linux學習