你好,游客 登录 注册 搜索
背景:
阅读新闻

使用Nginx搭建HTTPS服务器

[日期:2017-08-15] 来源:Linux社区  作者:liqingbiao [字体: ]

(一)简介
HTTPS(Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单来讲就是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。

它是一个URI scheme(抽象标识符体系),句法类同http:体系,用于安全的http数据传输。https使用的默认端口是443.
HTTPS协议的主要作用可以分为两种:一种是建立一个信息安全通道,来保证数据传输的安全;另一种就是确认网站的真实性。

SSL证书类型简介:
要设置安全服务器,使用公共钥创建一对公私钥对。大多数情况下,发送证书请求(包括自己的公钥),你的公司证明材料以及费用到一个证书颁发机构(CA).CA验证证书请求及您的身份,然后将证书返回给您的安全服务器。
但是内网实现一个服务器端和客户端传输内容的加密,可以自己给自己颁发证书,只需要忽略掉浏览器不信任的警报即可!
由CA签署的证书为您的服务器提供两个重要的功能:

浏览器会自动识别证书并且在不提示用户的情况下允许创建一个安全连接

当一个CA生成一个签署过的证书,它为提供网页给浏览器的组织提供身份担保。

多数支持ssl的web服务器都有一个CA列表,它们的证书会被自动接受。当一个浏览器遇到一个其授权CA并不在列表中的证书,浏览器将询问用户是否接受或拒绝连接

(二)工作原理
HTTPS能够加密信息,以免敏感信息被第三方获取,所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议。
客户端在使用HTTPS方式与Web服务器通信时有以下几个步骤,如图所示。
(1)客户使用https的URL访问Web服务器,要求与Web服务器建立SSL连接。
(2)Web服务器收到客户端请求后,会将网站的证书信息(证书中包含公钥)传送一份给客户端。
(3)客户端的浏览器与Web服务器开始协商SSL连接的安全等级,也就是信息加密的等级。
(4)客户端的浏览器根据双方同意的安全等级,建立会话密钥,然后利用网站的公钥将会话密钥加密,并传送给网站。
(5)Web服务器利用自己的私钥解密出会话密钥。
(6)Web服务器利用会话密钥加密与客户端之间的通信。

(三)https的优缺点:
      1,优点:
(1)使用HTTPS协议可认证用户和服务器,确保数据发送到正确的客户机和服务器;
(2)HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,要比http协议安全,可防止数据在传输过程中不被窃取、改变,确保数据的完整性。
(3)HTTPS是现行架构下最安全的解决方案,虽然不是绝对安全,但它大幅增加了中间人攻击的成本。
(4)搜索排名更靠前。比起同等HTTP网站,采用HTTPS加密的网站在搜索结果中的排名将会更高。

    2,缺点:

1)HTTPS协议握手阶段比较费时,会使页面的加载时间延长近50%,增加10%到20%的耗电;
(2)HTTPS连接缓存不如HTTP高效,会增加数据开销和功耗,甚至已有的安全措施也会因此而受到影响;
(3)SSL证书需要钱,功能越强大的证书费用越高,个人网站、小网站没有必要一般不会用。
(4)SSL证书通常需要绑定IP,不能在同一IP上绑定多个域名,IPv4资源不可能支撑这个消耗。
(5)HTTPS协议的加密范围也比较有限,在黑客攻击、拒绝服务攻击、服务器劫持等方面几乎起不到什么作用。最关键的,SSL证书的信用链体系并不安全,特别是在某些国家可以控制CA根证书的情况下,中间人攻击一样可行。

 (四)nginx搭建https服务器
1,创建SSL证书
  1.1生产私钥,openssl genrsa -des3 -out xn2.lqb.com.key 2048。此命令将生成2048位的RSA私钥,使用DES3算法,私钥文件名可任意命名,在Nginx配置中指定文件路径即可,会提示设定私钥密码,请设置密码,并牢记。
[root@Monitor ssl]# openssl genrsa -des3 -out xn2.lqb.com 2048
Generating RSA private key, 2048 bit long modulus
..................................+++
.......................................................+++
e is 65537 (0x010001)
Enter pass phrase for xn2.lqb.com:
Verifying - Enter pass phrase for xn2.lqb.com:

1.2以上生产的key是有密码的,如果把密码去除,执行如下命令 openssl rsa -in xn2.lqb.com -out xn2.lqb.com_nopwd.key
[root@Monitor ssl]# ls
xn2.lqb.com
[root@Monitor ssl]# openssl rsa -in xn2.lqb.com -out xn2.lqb.com_nopwd.key
Enter pass phrase for xn2.lqb.com:
writing RSA key

1.3由已生产的私钥生成证书请求文件CSR。 openssl rsa -in xn2.lqb.com -out xn2.lqb.com_nopwd.key
[root@Monitor ssl]# openssl rsa -in xn2.lqb.com -out xn2.lqb.com_nopwd.key
Enter pass phrase for xn2.lqb.com:
writing RSA key
[root@Monitor ssl]# openssl req -new -key xn2.lqb.com -out xn2.lqb.com.csr
Enter pass phrase for xn2.lqb.com:
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) [AU]:CN
State or Province Name (full name) [Some-State]:shanghai
Locality Name (eg, city) []:shanghai
Organization Name (eg, company) [Internet Widgits Pty Ltd]:xn2.lqb.com
Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:xn2.lqb.com
Email Address []:2223344@qq.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@Monitor ssl]# ls
xn2.lqb.com  xn2.lqb.com.csr  xn2.lqb.com_nopwd.key

1.4.证书请求文件CSR文件必须有CA的签名才能形成证书,可以将此CSR发给StartSSL(可免费)、verisign(一大笔钱)等地方由他来验证。也可以自己做CA,自己给自己颁发证书。创建一个自己签署的CA证书。openssl req -new -x509 -days 3650 -key xn2.lqb.com -out xn2.lqb.com.crt
[root@Monitor ssl]# openssl req -new -x509 -days 3650 -key xn2.lqb.com -out xn2.lqb.com.crt     
xn2.lqb.com            xn2.lqb.com.csr        xn2.lqb.com_nopwd.key 
[root@Monitor ssl]# openssl req -new -x509 -days 3650 -key xn2.lqb.com_nopwd.key -out xn2.lqb.com.crt
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) [AU]:CN
State or Province Name (full name) [Some-State]:Shanghai 
Locality Name (eg, city) []:shanghai
Organization Name (eg, company) [Internet Widgits Pty Ltd]:lqb.com
Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:xn2.lqb.com
Email Address []:
[root@Monitor ssl]# ls
xn2.lqb.com  xn2.lqb.com.crt  xn2.lqb.com.csr  xn2.lqb.com_nopwd.key

2.查看nginx是否安装SSL模块
[root@Monitor ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.10.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre --with-http_realip_module --with-http_image_filter_module --with-debug

3.配置nginx虚拟主机文件
[root@Monitor ssl]#vim  ../server.conf
server {
        listen    80; 
        server_name  xn2.lqb.com;
        root /html/xn2;
    #    rewrite ^/(.*)$ https:xn3.lqb.com/$1 permanent;
    location / {
      index index.html;
    #    proxy_cache mycache;
    #    proxy_cache_valid 200 3h;
    #    proxy_cache_valid 301 302 10m;
    #    proxy_cache_valid all 1m;
    #    proxy_cache_use_stale error timeout http_500 http_502 http_503;
    #   
    #    proxy_pass http://192.168.180.9;
    #    proxy_set_header Host    $host;
    #    proxy_set_header X-Real-IP  $remote_addr;
                    }
                   
      location  /images/
          {
            index index.html;
            }
                    }
server {
     
        listen      *:443;
        server_name  xn2.lqb.com;
        ssl            on;                                ###位虚拟主机开启ssl支持
        ssl_certificate /usr/local/nginx/conf/server/ssl/xn2.lqb.com.crt;    ###为虚拟主机指定签名证书文件
        ssl_certificate_key    /usr/local/nginx/conf/server/ssl/xn2.lqb.com_nopwd.key;    ###为虚拟主机指定私钥文件
#      #ssl_session_timeout  5m;        ####客户端能够重复使用存储在缓存中的会话参数时间
        root  /html/xn3;
        location  /images/ {
                index index.html;
              }
        location / {
        proxy_pass http://192.168.180.23;
        proxy_set_header Host    $host;
        proxy_set_header X-Real-IP  $remote_addr;
                  }
          }

4,在浏览器查看访问记录

nginx服务器访问日志:
192.168.181.231 - - [11/Aug/2017:11:22:35 +0800] "GET / HTTP/1.1" 200 65 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
192.168.181.231 - - [11/Aug/2017:11:22:37 +0800] "GET /images/ HTTP/1.1" 200 47 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"

Nginx 403 forbidden的解决办法  http://www.linuxidc.com/Linux/2017-08/146084.htm

CentOS 7下Nginx服务器的安装配置  http://www.linuxidc.com/Linux/2017-04/142986.htm

CentOS上安装Nginx服务器实现虚拟主机和域名重定向  http://www.linuxidc.com/Linux/2017-04/142642.htm

CentOS 6.8 安装LNMP环境(Linux+Nginx+MySQL+PHP)  http://www.linuxidc.com/Linux/2017-04/142880.htm

Linux下安装PHP环境并配置Nginx支持php-fpm模块  http://www.linuxidc.com/Linux/2017-05/144333.htm

Nginx服务的SSL认证和htpasswd认证  http://www.linuxidc.com/Linux/2017-04/142478.htm

Ubuntu 16.04上启用加密安全的Nginx Web服务器  http://www.linuxidc.com/Linux/2017-07/145522.htm

Linux中安装配置Nginx及参数详解  http://www.linuxidc.com/Linux/2017-05/143853.htm

Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

CentOS 7.2下Nginx+PHP+MySQL+Memcache缓存服务器安装配置  http://www.linuxidc.com/Linux/2017-03/142168.htm

CentOS6.9编译安装Nginx1.4.7  http://www.linuxidc.com/Linux/2017-06/144473.htm

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-08/146268.htm

linux
相关资讯       Nginx搭建HTTPS服务器  HTTPS服务器 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款