`
wx1569466809
  • 浏览: 72230 次
文章分类
社区版块
存档分类
最新评论

nginx负载均衡、配置ssl

 
阅读更多

12.17 Nginx负载均衡

Nginx负载均衡即为当代理服务器将自定义的域名解析到多个指定IP时,通过upstream来保证用户可以通过代理服务器正常访问各个IP。

负载均衡配置

配置参数:

[root@adailinux ~]# vim /usr/local/nginx/conf/vhost/load.conf
upstream aq.com
#自定义域名
{
    ip_hash;
    #保证同一个用户始终保持在同一台机器上
    #即当域名指向多个IP时,保证每个用户始终解析到同一IP
    server 61.135.157.156:80;
    server 125.39.240.113:80;
    #指定web服务器的IP
}
server
{
    listen 80;
    server_name www.qq.com;
    location /
    {
        proxy_pass      http://aq.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@adailinux ~]# curl -x127.0.0.1:80 www.qq.com 
This is the default directory.

使用代理前,会直接解析到默认虚拟主机。

代理后

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

[root@adailinux ~]# curl -x127.0.0.1:80 www.qq.com

使用代理后会解析到代理服务器所指向的IP

[root@adailinux ~]# dig www.qq.com

;; ANSWER SECTION:
www.qq.com.		138	IN	A	61.135.157.156
www.qq.com.		138	IN	A	125.39.240.113

;; Query time: 13 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 二 8月 15 16:41:11 CST 2017
;; MSG SIZE  rcvd: 71

注意: Nginx不支持代理https,只能代理http,新版本的Nginx可以代理tcp。

dig命令

>dig命令是常用域名解析工具。

如果服务器中没有该命令,手动安装:

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

语法: dig [域名]

http、https、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端口。

12.18 SSL原理

>SSL(Secure Sockets Layer 安全套接层)协议,及其继任者TLS(Transport Layer Security传输层安全)协议,是为网络通信提供安全及数据完整性的一种安全协议。

SSL工作流程

如果虚拟机中没有此工具,手动安装:

[root@adailinux ~]# yum install -y openssl

SSL工作流程

mark

  • 浏览器发送一个https的请求给服务器;
  • 服务器要有一套数字证书,可以自己制作(后面的操作就是阿铭自己制作的证书),也可以向组织申请,区别就是自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出>提示页面,这套证书其实就是一对公钥和私钥;
  • 服务器会把公钥传输给客户端;
  • 客户端(浏览器)收到公钥后,会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机数,并用收到的公钥加密;
  • 客户端把加密后的随机字符串传输给服务器;
  • 服务器收到加密随机字符串后,先用私钥解密(公钥加密,私钥解密),获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,所谓对称加密,就是将数据和私钥也就是这个随机字符串>通过某种算法混合在一起,这样除非知道私钥,否则无法获取数据内容);
  • 服务器把加密后的数据传输给客户端;
  • 客户端收到数据后,再用自己的私钥也就是那个随机字符串解密;

12.19 生成SSL密钥对

SSL证书就是一对公钥和私钥。

创建私钥

[root@adailinux ~]# cd /usr/local/nginx/conf/

[root@adailinux conf]# openssl genrsa -des3 -out tmp.key 2048
#生成SSL密钥
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,取消密码:

[root@adailinux conf]# openssl rsa -in tmp.key -out adailinux.key 

Enter pass phrase for tmp.key:
writing RSA key

删除密钥文件:

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

生成证书请求文件

需要拿这个文件和私钥一起生产公钥文件:

[root@adailinux conf]# openssl req -new -key adailinux.key -out adailinux.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
State or Province Name (full name) []:adai
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:Beijing
Organizational Unit Name (eg, section) []:Beijing
Common Name (eg, your name or your server's hostname) []:adailinux
Email Address []:adai@adailinux.com

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

说明: 该部分内容如果不购买证书可以自定义;如果是正式应用在网站上,需要规范填写对应信息(购买)。

创建公钥:

[root@adailinux conf]# openssl x509 -req -days 365 -in adailinux.csr -signkey adailinux.key -out adailinux.crt

Signature ok
subject=/C=CN/ST=adai/L=Beijing/O=Beijing/OU=Beijing/CN=adailinux/emailAddress=adai@adailinux.com
Getting Private key

12.20 Nginx配置SSL

[root@adailinux conf]# cd vhost/

[root@adailinux vhost]# vim ssl.conf
server
{
    listen 443;
    server_name adai.com;
    index index.html index.php;
    root /data/wwwroot/adai.com;
    ssl on;
    #开启ssl
    ssl_certificate adailinux.crt;
    #配置公钥
    ssl_certificate_key adailinux.key;
    #配置私钥
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #配置协议
}

[root@adailinux vhost]# mkdir /data/wwwroot/adai.com

检测

报错:

[root@adailinux conf]# /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

未识别ssl配置,需要重新编译Nginx:

[root@adailinux conf]# cd /usr/local/src/nginx-1.12.1/

[root@adailinux nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module  

[root@adailinux conf]# make
[root@adailinux conf]# make install

[root@adailinux 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@adailinux nginx-1.12.1]# /etc/init.d/nginx restart
Restarting nginx (via systemctl):                          [  确定  ]

[root@adailinux 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      5991/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1735/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2040/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      5991/nginx: master  
tcp6       0      0 :::3306                 :::*                    LISTEN      1990/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      1735/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2040/master  

nginx监听80和443端口。

测试

[root@adailinux nginx-1.12.1]# cd /data/wwwroot/adai.com/

[root@adailinux adai.com]# vim index.html

This is ssl.

添加本地域名:

[root@adailinux adai.com]# vim /etc/hosts
127.0.0.1  adai.com

[root@adailinux vhost]# curl https://adai.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.

因为该证书是自己创建的,所以提示证书不被信任!!!

使用浏览器检测:

mark
注: 进行该测试之前需要更改Windows的hosts文件。

转载于:https://my.oschina.net/1995blog/blog/1594234

分享到:
评论

相关推荐

    nginx负载均衡配置-windows.docx

    本文档提供了一个完整的 Nginx 负载均衡配置示例,涵盖了基本配置、负载均衡配置、SSL 加密配置和 URL 路由规则配置等方面。通过阅读本文档,读者可以了解 Nginx 负载均衡的基本原理和配置方法,从而更好地应用 ...

    nginx负载均衡配置文件demo

    以上只是一些基本的Nginx负载均衡配置示例,实际使用中可能还需要结合其他高级特性,如URL重写、SSL/TLS终止、限速、限制访问等,来构建更复杂的Web服务架构。对于"conf"文件中的具体配置,可以根据需求进行详细解读...

    nginx和tomcat配置SSL和负载均衡配置

    ### Nginx 和 Tomcat 配置 SSL 与...通过以上步骤,可以实现 Tomcat 的 SSL 加密和负载均衡配置。需要注意的是,具体配置可能会因环境和需求的不同而有所变化。在实际操作过程中,应仔细检查官方文档以确保正确配置。

    nginx负载均衡ssl证书认证强制跳转https+keeplived+apache

    在构建高可用性和安全性的网络服务时,"nginx负载均衡ssl证书认证强制跳转https+keeplived+apache"是一个常见的架构模式。该模式结合了Nginx的反向代理和负载均衡能力、Keepalived的高可用性保证以及SSL证书来确保...

    nginx配置 +负载均衡+https协议

    通过生成SSL证书、重新编译Nginx以添加SSL模块,以及编辑Nginx配置文件实现HTTPS支持和负载均衡功能。此外,还提供了Nginx的基本操作命令,包括启动、停止和重启等。这些步骤和配置为构建安全可靠的Web服务器提供了...

    redis+tomcat+nginx负载均衡配置

    在构建高性能、高可用性的Web服务时,"redis+tomcat+nginx负载均衡配置"是一个常见且有效的架构模式。这个组合充分利用了各个组件的优势,确保系统稳定、响应快速,并能够处理大量的并发请求。以下是关于这个配置的...

    Nginx负载均衡/SSL配置的实现

    【负载均衡配置示例补充】 除了基础的`ip_hash`策略,Nginx还支持根据请求的URL或文件名进行负载均衡。在提供的示例中,有针对`.php`文件的两个不同组`aa`和`bb`,以及一个通用的`/`路径。`location`指令匹配URL...

    nginx负载均衡 nginx+tomcat tomcat实现负责均衡

    ### Nginx与Tomcat实现负载均衡的知识点详解 #### Nginx简介及特性 Nginx是一款由Igor Sysoev开发的高性能HTTP服务器和反向代理服务器,以其出色的稳定性和低系统资源消耗而著称。最初是为俄罗斯访问量排名第二的...

    nginx负载均衡配置文档.pdf

    **Nginx负载均衡配置详解** Nginx是一款高性能、轻量级的Web服务器和反向代理服务器,常被用于实现高并发的网络服务。它以其低内存占用、高效的处理能力以及灵活的配置机制,被众多大型互联网公司如百度、京东、...

    nginx负载均衡配置

    3. **定义负载均衡器**:在Nginx配置中,创建一个upstream块,指定后端Tomcat服务器的IP地址和端口。可以使用多种负载均衡策略,例如轮询(round-robin)、最少连接(least connections)或基于IP哈希(ip_hash)。 ...

    nginx 负载均衡与反向代理资源

    在Win7 64位系统上安装Nginx,可以利用其强大的负载均衡和反向代理功能,优化网站性能并提升可用性。** ### 一、Nginx的安装 1. **下载Nginx安装包**:首先,你需要从Nginx官方网站获取适用于Windows 64位系统的...

    Nginx + Tomcat 负载均衡配置详解

    **Nginx + Tomcat 负载均衡配置详解** 在现代互联网应用中,服务器的高可用性和性能优化是至关重要的。Nginx 和 Tomcat 的组合常常被用来实现这样的目标,其中Nginx作为反向代理和负载均衡器,而Tomcat作为Java应用...

    suse11下Nginx负载均衡安装流程

    在主服务器A上进行负载均衡配置,修改`nginx.conf`文件: ```nginx http { # 开启gzip压缩 # gzip on; upstream www.12.com { server 192.168.1.145:80; server 192.168.1.146:80; } server { listen 80;...

    nginx+tomcat+ssl实现负载均衡

    【Nginx+Tomcat+SSL 实现负载均衡】是一个常见的Web服务器架构,用于提高网站的可用性和响应速度。Nginx作为前端反向代理服务器,处理静态内容和客户端请求,将动态请求转发到后端的Tomcat应用服务器,同时通过SSL...

    nginx负载均衡搭建

    本文将详细介绍如何在Linux系统上利用Nginx搭建负载均衡环境,结合Tomcat7和JDK7进行配置。 首先,我们需要安装Nginx,这是一个高性能的HTTP和反向代理服务器,常用于提供负载均衡服务。在安装Nginx之前,需要先...

    Linux源码安装MySQL+MySQL主从+Nginx+Nginx负载均衡+redis+php+phpredis+tomcat

    1. 在Nginx配置文件中添加负载均衡配置,例如使用轮询策略: ```nginx upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; } location / { ...

    nginx应用部署和负载均衡应用

    在本文中,我们将探讨Nginx的安装、配置以及负载均衡的应用。 1. **安装Nginx** Nginx可以通过多种方式安装,如编译源码或使用包管理器。在Linux系统中,常用的是通过包管理器安装,例如使用`yum`或`apt-get`。...

    nginx负载均衡配置,宕机自动切换方式

    【Nginx负载均衡配置与宕机自动切换】 在Nginx中实现负载均衡和宕机自动切换,主要依赖于其内置的`ngx_http_proxy_module`和`ngx_http_upstream_module`模块。虽然Nginx自身并不直接提供健康检查功能,但通过合理...

    nginx 负载均衡

    二、Nginx 负载均衡配置 在 Nginx 配置文件中,我们通常在 http 或者 server 块中定义 upstream 模块,指定一组后端服务器。例如: ```nginx upstream backend { server backend1.example.com; server backend2....

Global site tag (gtag.js) - Google Analytics