您的位置:首页 > 运维架构 > Nginx

Nginx负载均衡,ssl原理

2018-01-12 04:33 906 查看

Nginx负载均衡

Nginx负载均衡是通过代理服务器让后面的web服务器能更快更稳定,还可以避免单点设备的故障造成的服务不可用。

dig命令:常用的域名解析工具

通过安装bind-utils这个包。

[root@shuai-01 ~]# yum install -y bind-utils


语法: dig 域名

[root@shuai-01 ~]# dig www.qq.com


做负载均衡,先写一个新的配置文件(ld.conf)

[root@shuai-01 vhost]# vim ld.conf

upstream qq_com
##自定义名字
{
ip_hash;
##保证同一个用户始终在同一台机器上,当域名指向多个IP时,保证用户始终解析到同一IP
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;
}
}


检测:

使用代理前:在本机访问www.qq.com会直接访问到默认虚拟主机的默认页

[root@shuai-01 vhost]# curl -x127.0.0.1:80 www.qq.com
this is a default site


代理后:

[root@shuai-01 vhost]# /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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload


会变成qq.com的主页

Nginx不支持代理https 只能代理http,新版本Nginx能代理tcp

HTTP超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。

HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议要比http协议安全。

HTTP默认的端口号为80,HTTPS的端口号为443。

TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议,由IETF的RFC 793定义。默认监听80端口。

ssl原理

HTTPS它是一种加密的HTTPS协议,如果HTTPS通信的数据包在传输过程中被截获,我们可以破译这些数据包里面的信息,这里面不乏一些用户名、密码、手机号等敏感的信息,而如果使用HTTPS通信,即使数据包被截获,我们也无法破译里面的内容。



解读SSL的工作流程

浏览器发送一个https的请求给服务器;

服务器要有一套数字证书,可以自己制作(后面的操作就是阿铭自己制作的证书),也可以向组织申请,区别就是自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出>提示页面,这套证书其实就是一对公钥和私钥;

服务器会把公钥传输给客户端;

客户端(浏览器)收到公钥后,会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机数,并用收到的公钥加密;

客户端把加密后的随机字符串传输给服务器;

服务器收到加密随机字符串后,先用私钥解密(公钥加密,私钥解密),获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,所谓对称加密,就是将数据和私钥也就是这个随机字符串>通过某种算法混合在一起,这样除非知道私钥,否则无法获取数据内容);

服务器把加密后的数据传输给客户端;

客户端收到数据后,再用自己的私钥(也就是那个随机字符串)解密;

生成SSL密钥对

理解了ssl原理后,现在我们可以在虚拟机上去生成ssl密钥对,也就是自己制作证书。我们需要使用一个工具来生成密钥对,把密钥对放在nginx的conf目录下。

进入nginx的conf目录:

[root@shuai-01 ~]# cd /usr/local/nginx/conf/


我们需要使用到的工具是openssl,如果你虚拟机没有此命令,需要自己安装,安装命令:

yum -y install openssl


准备完成后,第一步是生成一个私钥,命令如下:

[root@shuai-01 conf]# openssl genrsa -des3 -out tmp.key 2048   //key文件为私钥


第二步,是把密码取消掉,如果不取消的话,会每次都要求客户端输入此密码,命令如下:

[root@shuai-01 conf]# openssl rsa -in tmp.key -out shuailinux.key


//转换key,取消密码

实际上这时候shuailinux.key和tmp.key是同一个文件,只不过前者有密码,后者没密码。

这时候就可以把tmp.key给删掉了:

[root@shuai-01 conf]# rm -f tmp.key


第三步就是去生成一个请求的文件,生成这个请求文件的目的是为了让这个请求文件和私钥一起去生成一个公钥,命令如下,会要求你输入一些信息,因为是自己制作的证书所以随便输入也是可以的,如果是正式的证书就不可以随便写了:

[root@shuai-01 conf]# openssl req -new -key shuailinux.key -out shuailinux.csr

Country Name (2 letter code) [XX]:11
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:shuai
Organizational Unit Name (eg, section) []:shuai
Common Name (eg, your name or your server's hostname) []:shuai
Email Address []:shuai@linux.com

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


第四步就是生成公钥了,命令如下:

[root@shuai-01 conf]# openssl x509 -req -days 365 -in shuailinux.csr -si


gnkey shuailinux.key -out shuailinux.crt

[root@shuai-01 conf]# ls
fastcgi.conf            mime.types           shuailinux.crt
fastcgi.conf.default    mime.types.default   shuailinux.csr
fastcgi_params          nginx.conf           shuailinux.key
fastcgi_params.default  nginx.conf.1         uwsgi_params
htpasswd                nginx.conf.default   uwsgi_params.default
koi-utf                 scgi_params          vhost
koi-win                 scgi_params.default  win-utf


Nginx配置ssl

我们生成好密钥对也就是证书之后,就可以使用Nginx配置SSL了。

在虚拟配置文件目录下

创建一个文件:

[root@shuai-01 conf]# cd vhost/
[root@shuai-01 vhost]# vim ssl.conf


加入以下内容:

server
{
listen 443;
server_name shuai.com;
index index.html index.php;
root /data/wwwroot/shuai.com;
ssl on;  //定义开启ssl
ssl_certificate shuailinux.crt;  //指定公钥
ssl_certificate_key shuailinux.key;  //指定私钥
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  //定义协议
}


然后保存退出,测试一下配置文件

[root@shuai-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




查看Nginx的编译参数:

[root@shuai-01 nginx-1.12.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
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


最早编译nginx的 并没有指定支持ssl ,需要重新编译下,让大家不要去删除源码包,后期有可能还要进一步编译

查看将ssl模块编译进Nginx:

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

[root@shuai-01 nginx-1.12.2]# make

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


检查配置文件语法:

[root@shuai-01 nginx-1.12.2]# /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@shuai-01 nginx-1.12.2]# /etc/init.d/nginx restart


查监听端口:

[root@shuai-01 nginx-1.12.2]# 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      5665/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1968/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2806/master
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      5665/nginx: master
tcp6       0      0 :::3306                 :::*                    LISTEN      2754/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      1968/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      2806/master




在/data/wwwroot/下创键shuai.com目录:

[root@shuai-01 nginx-1.12.2]# cd /data/wwwroot/
[root@shuai-01 wwwroot]# mkdir shuai.com
[root@shuai-01 wwwroot]# cd shuai.com/
[root@shuai-01 shuai.com]# vim index.html


测试:

用curl :

[root@shuai-01 shuai.com]# curl -x127.0.0.1:443 https://shuai.com curl: (56) Received HTTP code 400 from proxy after CONNECT


要想用curl访问,写host

[root@shuai-01 shuai.com]# vi /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.172.3 www.baidu.com
127.0.0.1 shuai.com

[root@shuai-01 shuai.com]# curl  https://shuai.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.


证书不可信任,实际上已经访问成功。证书是自己颁发的。

在windows端访问:

先在hosts添加一下



打开浏览器:



看防火墙规则:

全部清空或者添加一个443端口开放

[root@shuai-01 shuai.com]# iptables -F




证书不被信任

信任证书要买,沃通。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ssl配置