nginx配置https访问

一、https简介

HTTPS其实是有两部分组成:HTTP + SSL/TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据

二、https协议原理

首先,客户端与服务器建立连接,各自生成私钥和公钥,是不同的。服务器返给客户端一个公钥,然后客户端拿着这个公钥把要搜索的东西加密,称之为密文,并连并自己的公钥一起返回给服务器,服务器拿着自己的私钥解密密文,然后把响应到的数据用客户端的公钥加密,返回给客户端,客户端拿着自己的私钥解密密文,把数据呈现出来。

三、SSL证书和私钥的生成

1、创建一个保存私钥和证书的目录并进入

[[email protected] ~]# mkdir /usr/local/nginx-1.12.1/key

[[email protected] ~]# cd /usr/local/nginx-1.12.1/key

2、生成私钥文件

[[email protected] key]# openssl genrsa -out server.key 1024

3、生成csr文件

[[email protected] key]# openssl req -new -key server.key -out certreq.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    所在国家的ISO标准代号

State or Province Name (full name) []:beijing   单位所在地省/自治区/直辖市

Locality Name (eg, city) [Default City]:beijing    单位所在地的市/县/区

Organization Name (eg, company) [Default Company Ltd]:lvdian   单位/机构/企业合法的名称

Organizational Unit Name (eg, section) []:yunwei  部门名称

Common Name (eg, your name or your server's hostname) []:www.long.com

主机名,此项必须与您访问提供SSL服务的服务器时所应用的域名完全匹配

Email Address []:[email protected]   邮件地址,不必输入,直接回车跳过

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:            以下信息不必输入,回车跳过直到命令执行完毕

An optional company name []:

生成类型为X509的自签名证书。有效期设置3650天,即有效期为10年

[[email protected] key]# openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

四、重新编译nginx添加ssl模块(编译过ssl跳过此步骤)

1、查看nginx版本及编译参数

[[email protected] ~]# /usr/local/nginx-1.12.1/sbin/nginx -V

nginx version: nginx/1.12.1

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module

可以看到,我们上面已经编译了ssl模块,如果没有编译ssl模块,我们需要安装以下方法进行编译安装。

2、编译ssl模块

(1)备份原有的nginx执行文件

[[email protected] ~]# cp /usr/local/nginx-1.12.1/sbin/nginx /usr/local/nginx-1.12.1/sbin/nginx.bak

(2)进入源码解压的nginx目录编译ssl模块

提示:一定要把之前编译过的参数都加进去重新编译,并加入ssl模块

[[email protected] ~]# cd nginx-1.12.1

[[email protected] nginx-1.12.1]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module

(3)执行make,千万别执行make install,否则就覆盖安装

[[email protected] nginx-1.12.1]# make

3、停止nginx,将新生成的nginx文件覆盖原有的nginx文件

make完之后在nginx-1.12.1/objs目录下就多了个nginx,这个就是新版本的nginx执行文件了,将这个文件复制到/usr/local/nginx-1.12.1/sbin/目录覆盖原来的nginx执行文件。

[[email protected] nginx-1.12.1]# cp objs/nginx /usr/local/nginx-1.12.1/sbin/

cp:是否覆盖"/usr/local/nginx-1.12.1/sbin/nginx"? y

4、测试新的nginx程序是否正确并查看编译参数

[[email protected] ~]# /usr/local/nginx-1.12.1/sbin/nginx -t

nginx: the configuration file /usr/local/nginx-1.12.1/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx-1.12.1/conf/nginx.conf test is successful

[[email protected] ~]# /usr/local/nginx-1.12.1/sbin/nginx -v

nginx version: nginx/1.12.1

[[email protected] ~]# /usr/local/nginx-1.12.1/sbin/nginx -V

nginx version: nginx/1.12.1

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module

五、nginx配置ssl加密

这里的配置是最重要的,可以根据自己的需求进行配置。想要https就要监听443端口,nginx.conf已经预留出了server,只要我们放开权限,简单修改即可。

[[email protected] ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf

server {

listen       443 ssl;

server_name  localhost;

ssl_certificate      /usr/local/nginx-1.12.1/key/certreq.csr;

ssl_certificate_key  /usr/local/nginx-1.12.1/key/server.key;

ssl_session_cache    shared:SSL:1m;

ssl_session_timeout  5m;

ssl_ciphers  HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers  on;

location / {

root   html;

index  index.html index.htm;

}

}

#配置端口转发

server {

listen 80;

server_name www.long.com;

rewrite ^(.*) https://$server_name$1 permanent;

}

[[email protected] ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload

ssl_certificate证书其实是个公钥,它会被发送到连接服务器的每个客户端,ssl_certificate_key私钥是用来解密的,所以它的权限要得到保护但nginx的主进程能够读取。当然私钥和证书可以放在一个证书文件中,这种方式也只有公钥证书才发送到client。

ssl_session_timeout 客户端可以重用会话缓存中ssl参数的过期时间,内网系统默认5分钟太短了,可以设成30m即30分钟甚至4h。

ssl_ciphers选择加密套件,不同的浏览器所支持的套件(和顺序)可能会不同。这里指定的是OpenSSL库能够识别的写法,你可以通过 openssl -v cipher ‘RC4:HIGH:!aNULL:!MD5’(后面是你所指定的套件加密算法) 来看所支持算法。

ssl_prefer_server_ciphers on设置协商加密算法时,优先使用我们服务端的加密套件,而不是客户端浏览器的加密套件。

六、Windows浏览器测试能否跳转https成功

1、Windows中添加域名解析

打开windows的C:\Windows\System32\drivers\etc\hosts文件,添加下面的域名解析

192.168.10.10     www.long.com

2、打开浏览器,输入域名测试

可以看到,我们的https已经跳转成功

上面的是自己生成的证书,不受各个浏览器信任的,要让各个浏览器信任就得在相关官方网站申请一个免费证书,一般免费证书有效期是几个月至一年。

原文地址:http://blog.51cto.com/longlei/2158471

时间: 2024-08-30 01:48:26

nginx配置https访问的相关文章

nginx配置https访问安装ssl证书

开发小程序,接口需要换成https(lnmp环境.阿里云服务器) 证书在阿里云上免费购买下载的(解析到你要绑定的域名上) 修改nginx的配置nignx.conf 如下图配置(不做详细的讲解了,这块根据自己服务器不同的目录会有不同的配置) 重启nginx就可以了. 原文地址:https://www.cnblogs.com/blange/p/11497277.html

nginx配置https及Android客户端访问自签名证书

前一篇随笔通过keytool生成keystore并为tomcat配置https,这篇随笔记录如何给nginx配置https.如果nginx已配置https,则tomcat就不需要再配置https了.通过以下三步生成自签名证书# 生成一个key,你的私钥,openssl会提示你输入一个密码,可以输入,也可以不输,# 输入的话,以后每次使用这个key的时候都要输入密码,安全起见,还是应该有一个密码保护> openssl genrsa -des3 -out selfsign.key 4096 # 使用

【转】Linux下nginx配置https协议访问的方法

一.配置nginx支持https协议访问,需要在编译安装nginx的时候添加相应的模块--with-http_ssl_module 查看nginx编译参数:/usr/local/nginx/sbin/nginx -V 如下所示: configure arguments: --prefix=/usr/local/nginx --with-google_perftools_module --without-http_memcached_module --user=www --group=www --

certbot在Centos7上配置合法签名证书,实现nginx的https访问

咖菲猫-李常明笔记 公司因之前使用的openssh创建的自签名证书,有一个弊端,就是在某些客户端上不能使用此证书,无法使用https连接,所以,研究了一下certbot 做签名证书! certbot的官网地址: https://certbot.eff.org/ 1.制作证书前的准备: 你需要有一个公网地址,并绑定合法域名 2.开始制作: (1).下载Certbot客户端: wget https://dl.eff.org/certbot-auto (2).下载后,进入下载的目录,添加执行权限 ch

Nginx配置https服务器

配置HTTPS主机,必须在server配置块中打开SSL协议,还需要指定服务器端证书和密钥文件的位置: server { listen 443;  #要加密的域名 server_name www.test.com; ssl on; #证书所在位置,本例默认放在了nginx的conf目录下 ssl_certificate ssl.crt;  #密钥所在位置,本例默认放在了nginx的conf目录下 ssl_certificate_key server.key; ssl_session_timeou

https(ssl)免费证书申请及nginx配置https(ssl)

首先推荐免费证书的申请url:https://freessl.org/ 然后就是上面证书申请的流程,这里可以参考一篇阿里云里面的博文(我会补充一些细节): https://yq.aliyun.com/articles/225669?spm=5176.10695662.1996646101.searchclickresult.1da01cb9Hyk86J freessl免费证书申请补充: 一.补充一下配置DNS验证的流程,我这里用的是阿里云: 然后点击添加记录 确定 完成,全选,启用一下就可以了:

nginx配置https证书

连接错误,不安全 并不是hsts的锅 chrome://net-internals/#hsts, 大部分原因还是证书的问题 NET::ERR_CERT_COMMON_NAME_INVALID 证书域名和实际访问的域名要匹配:SAN 证书中增加SAN域名 open ssl openssl genrsa -out server.key 1024 openssl req -new -x509 -days 3650 -key server.key -out server.crt -subj "/C=CN

购买https证书以及nginx配置https

文章来源运维公会:购买https证书以及nginx配置https 1.https的作用https的全名是安全超文本传输协议,是在http的基础上增加了ssl加密协议.在信息传输的过程中,信息有可能被劫持,从而造成数据的丢失,而如果使用的是https的话,即使信息被劫持,因为在传输过程中,信息是被加密的,所以也能保证数据的安全性. 2.申请证书目前在阿里云或者腾讯云上都可以购买证书,现在以阿里云为例购买证书在阿里云的产品与服务上找到SSL证书点击购买证书,可以看到有免费型的SSL证书.如果是个人使

关于Nginx配置Https server后,乱跳的问题解决记录

大部分的服务器上,我们会在一个Nginx服务下配置多个vhost,以最大化运用服务器资源.然而,为其中一个vhost域名启用 HTTPS 之后,发现百度统计的实时访客或入口页中,存在一些来自其它域名的请求.即通过 https://some-other-domain.com/some-url 来访问对应的 https://www.domain.com/some-url 结果就是 Google 浏览器显示了一个安全警告页面,认为这是一个不安全的网页.因为我只配置了 www.domain.com 的