Nginx 启用 https

时间:2023-01-08 17:06:46

在nginx.conf中增加新server配置

    server {
listen ;
server_name www.some.com;
ssl on;
ssl_certificate sslkey/some.com.crt;
ssl_certificate_key sslkey/some.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1. TLSv1.;
ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:-LOW:!aNULL:!eNULL;
ssl_prefer_server_ciphers on; location / {
proxy_pass http://tomcat_www;
}
access_log logs/www-ssl.access.log main;
}

对于需要强制跳转的80端口访问, 使用

    server {
listen ;
server_name www.some.com;
location / {
root /var/www/html;
index index.html; # meta jump to https
}
access_log logs/www.access.log main;
}

index.html使用

<html>
<meta http-equiv="refresh" content="0;url=https://www.some.com/">
</html>

其他的跳转方案一:

    server {
listen 192.168.1.111:;
server_name test.com; rewrite ^(.*)$ https://$host$1 permanent;
}

方案二

    server {
listen 192.168.1.11:; #ssl端口
listen 192.168.1.11:; #用户习惯用http访问,加上80,后面通过497状态码让它自动跳到443端口
server_name test.com;
#为一个server{......}开启ssl支持
ssl on;
#指定PEM格式的证书文件
ssl_certificate /etc/nginx/test.pem;
#指定PEM格式的私钥文件
ssl_certificate_key /etc/nginx/test.key; #让http请求重定向到https请求
error_page https://$host$uri?$args;
}

使用openssl 给nginx生成证书的shell脚本

#!/bin/sh

# Preparing directories and files
mkdir -p demoCA/private
mkdir -p demoCA/newcerts
touch demoCA/index.txt
echo -e "01\n" >> demoCA/serial read -p "Enter your Organization [RockBB]: " ORGANIZATION
read -p "Enter your Organization Unit [Board]: " ORGANIZATION_UNIT
read -p "Enter your domain [www.example.com]: " DOMAIN
read -p "Enter your client name [client]: " CLIENT_NAME
read -p "Enter your p12 password [111111]: " PASSWORD
SUBJECT="/C=CN/ST=Beijing/L=Chaoyang/O=$ORGANIZATION/OU=$ORGANIZATION_UNIT/CN=$DOMAIN"
echo ""
echo "create self-signed certificate:"
# create private server key
openssl genrsa -out demoCA/private/cakey.pem
# self-signed certificate
openssl req -new -subj $SUBJECT -x509 -key demoCA/private/cakey.pem -out demoCA/cacert.pem -days echo ""
echo "create server certificate:"
openssl genrsa -out $DOMAIN.key
openssl req -new -subj $SUBJECT -key $DOMAIN.key -out $DOMAIN.csr
openssl ca -in $DOMAIN.csr -out $DOMAIN.crt -days= echo ""
echo "create client certificate"
SUBJECT_CLIENT="/C=CN/ST=Beijing/L=Chaoyang/O=$ORGANIZATION/OU=$ORGANIZATION_UNIT/CN=$CLIENT_NAME"
openssl genrsa -out $DOMAIN.client.key
openssl req -new -subj $SUBJECT_CLIENT -key $DOMAIN.client.key -out $DOMAIN.client.csr
openssl ca -batch -in $DOMAIN.client.csr -out $DOMAIN.client.crt -days=
openssl pkcs12 -export -clcerts -in $DOMAIN.client.crt -inkey $DOMAIN.client.key -out $DOMAIN.client.p12 -pasword pass:$PASSWORD echo ""
echo "Update the Nginx configuration:" : <<'END'
upstream tomcat_admin {
server 10.1.1.3:;
}
server {
listen ;
server_name www.rockbb.com;
location / {
rewrite ^(.*)$ https://$host$1 permanent;
}
access_log logs/www.access.log main;
}
server {
listen ;
server_name www.rockbb.com;
ssl on;
ssl_certificate sslkey/www.rockbb.com.crt;
ssl_certificate_key sslkey/www.rockbb.com.key;
ssl_client_certificate sslkey/www.rockbb.com.cacert.pem;
ssl_session_timeout 5m;
ssl_verify_client on;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1. TLSv1.;
ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:-LOW:!aNULL:!eNULL;
ssl_prefer_server_ciphers on; location / {
proxy_pass http://tomcat_admin;
proxy_redirect http:// $scheme://;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log logs/www-ssl.access.log main;
}
END

制作的过程中, 如果需要重新制作, 删除demoCA目录以及同级目录下的其他文件即可.

如果在浏览器上重复安装同参数但是第二次生成的证书, 会出现这样的错误

An error occurred during a connection to internal.yihuicai.cn. You have received an invalid certificate. Please contact the server administrator or email correspondent and give them the following information: Your certificate contains the same serial number as another certificate issued by the certificate authority. Please get a new certificate containing a unique serial number. Error code: SEC_ERROR_REUSED_ISSUER_AND_SERIAL

这是因为浏览器的旧证书没有清除干净导致的, 除了我的/个人部分外, 服务器证书下, 也需要清理.

其中 proxy_redirect http:// $scheme://; 用于让上游的tomcat知道访问者使用的是https协议, 避免java应用在request中得到错误的schema而使用http进行跳转

Update 2017-02-03 在同一服务器上同时使用商业证书和自签发证书时, 安卓客户端访问出现 java.security.cert.CertPathValidatorException: Trust anchor for certification path not found 错误的解决:

在命令行下, 检查证书是否正确

#
openssl s_client -connect app.somedomain.cn: | openssl x509 -noout -subject -issuer
# 明细
openssl s_client -debug -connect app.somedomain.cn:

如果上面的结果中, 出现的证书是自签发证书或者 verify error:num=21:unable to verify the first certificate , 就说明商业证书未生效. 解决的办法, 是在nginx中将对应IP的证书也设置为商业证书, 而原来直接用IP访问的应用, 新建一个二级域名来访问.

Update 2019-06-28 对于通过upstrean来映射到公网的Jenkins服务器, 来源端口是8080, 公网端口是6443, 虽然已经在Jenkins配置中, 将带端口的URL配置上, 但是在登录跳转和登出跳转时, 还是会跳到不带端口的URL上, 经过各种尝试, 发现是nginx配置中, 未将来源端口信息带给upstrean, 应当在 proxy_set_header Host 时, 加上端口信息 $host:$server_port, 如下

  location /jenkins/ {
proxy_pass http://jenkins_main;
#proxy_set_header Host $host;
proxy_set_header Host $host:$server_port;
proxy_redirect http:// $scheme://;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_intercept_errors on;
}

Nginx启用 HTTP/2

参考 https://www.nginx.com/blog/nginx-1-9-5/

在nginx中启用http2必须先启用ssl, 然后只需要在listen中增加 http2 参数, reload就可以了.

server {
listen 443 ssl http2;
server_name www.aa.cn;
ssl_certificate sslkey/aa.cn_bundle.crt.201806;
ssl_certificate_key sslkey/aa.cn.key.201806;
ssl_session_timeout 5m;
...

.这里有一个问题, 如果你配置了多个virtual server, 对应的都是同一个IP+Port, 那么在其中一个server上启用http2会同时在这个IP+Port对应的其他Virtual Server上也启用http2. 这里是这么解释的 https://*.com/questions/40987592/can-i-enable-http-2-for-specific-server-blocks-virtual-hosts-only-on-nginx

When starting, nginx first creates a separate process for every group of virtual hosts that listen on the same IP:port combination, and then sets the capabilities of that process to be the sum of all capabilities of every virtual host in that group handled by said process.

In your case, there's only one process that handles all the virtual hosts bound to *:443, so the process includes the http2 capability.

In order to achieve what you want, you need to make nginx spawn a different process that doesn't have the http2 capability on a separate IP:port combination.

For the virtual hosts you want to be accessed via http2, you must either:

use a different port - trivial, just use another port for them (e.g. listen 8443 ssl http2;) and remove http2 from all the others (e.g. `listen 443 ssl;)
use a different IP - you need to add another IP to the same NIC that uses your current IP and modify your virtual hosts accordingly (e.g. listen new_ip:443 ssl http2; and listen current_ip:443 ssl; respectively)

Nginx 启用 https的更多相关文章

  1. nginx启用https访问

    什么是https? https 全称:Hyper Text Transfer Protocol over Secure Socket Layer,是http的安全版.即http下加入SSL协议层,因此 ...

  2. 关于启用 HTTPS 的一些经验分享(二)

    转载: 关于启用 HTTPS 的一些经验分享(二) 几天前,一位朋友问我:都说推荐用 Qualys SSL Labs 这个工具测试 SSL 安全性,为什么有些安全实力很强的大厂家评分也很低?我认为这个 ...

  3. Startssl 现在就启用 HTTPS,免费的!

    为什么要使用HTTPS 主要是为了安全,虽然没有100%的安全,但是我们可以尽量提高安全级别,目前大型网站都已经使用HTTPS了 注册StartSSL 注册页面  选择国家 和 输入 邮箱 他们会通过 ...

  4. 我是如何将网站全站启用Https的?-记录博客安装配置SSL证书全过程

    评论»   文章目录 为什么要Https 如何选择Https 安装部署SSL证书 平滑过渡Https 搜索引擎的响应 启用Https小结 正如大家所看到的,部落全站已经启用了Https访问了,连续几天 ...

  5. tomcat 安装配置部署到nginx&plus;tomcat&plus;https

    目录 1 Tomcat简介 2.下载并安装Tomcat服务 2.2 部署java环境 2.3 安装Tomcat 2.4 Tomcat目录介绍 (关注点 bin conf logs webapps) 2 ...

  6. 记一次免费让网站启用HTTPS的过程

    写在前面 个人网站运行将近2个月了,期间根据酷壳的一篇教程如何免费的让网站启用HTTPS做了一次,中间遇到问题就放下了.昨天孙三苗问我网站地址说要添加友链,出于好奇想看他网站长什么样,顺道也加一下友链 ...

  7. nginx通过https方式反向代理多实例tomcat

    案例说明:前面一层nginx+Keepalived部署的LB,后端两台web服务器部署了多实例的tomcat,通过https方式部署nginx反向代理tomcat请求.配置一如下: 1)LB层的ngi ...

  8. 如何免费的让网站启用https

    本文源自酷壳:如何免费的让网站启用HTTPS 今天,我把CoolShell变成https的安全访问了.我承认这件事有点晚了,因为之前的HTTP的问题也有网友告诉我,被国内的电信运营商在访问我的网站时加 ...

  9. &lbrack;web&rsqb;&lbrack;nginx&rsqb; 初识nginx -- 使用nginx搭建https DPI解码测试环境

    环境 CentOS 7 X86 文档: https://nginx.org/en/docs/ 安装: [root@dpdk ~]# cat /etc/yum.repos.d/nginx.repo [n ...

随机推荐

  1. ASP&period;NET Core MVC&sol;WebAPi 模型绑定探索

    前言 相信一直关注我的园友都知道,我写的博文都没有特别枯燥理论性的东西,主要是当每开启一门新的技术之旅时,刚开始就直接去看底层实现原理,第一会感觉索然无味,第二也不明白到底为何要这样做,所以只有当你用 ...

  2. cherrypy应用探究

    1. cherrypy是什么? cheerypy是一个有pythonic特性的面向对象的http服务框架. 玩python的人都应该知道pythonic这个单词.python大神给我们的建议 : &g ...

  3. Jackson序列化和反序列化Json数据完整示例

    Jackson序列化和反序列化Json数据 Web技术发展的今天,Json和XML已经成为了web数据的事实标准,然而这种格式化的数据手工解析又非常麻烦,软件工程界永远不缺少工具,每当有需求的时候就会 ...

  4. 卸载WIN10自带功能

    要卸载OneNote,在里面输入 Get-AppxPackage *OneNote* | Remove-AppxPackage 可以复制,回车执行 4 要卸载3D,输入 Get-AppxPackage ...

  5. python的print&lpar;转&rpar;

    转载:http://www.pythonclub.org/python-basic/print   使用print输出各型的 字符串 整数 浮点数 出度及精度控制 strHello = 'Hello ...

  6. C&num;Linq的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. Javascript多线程引擎&lpar;五&rpar;

    Javascript多线程引擎(五)之异常处理 C语言没有提供一个像Java一样的异常处理机制, 这就带来了一个问题, 对于一个子函数中发生异常后, 需要在父函数调用子函数的位置进行Check, 如果 ...

  8. linux 集群及lvs

    集群及LVS 集群: 一组通过高速网络互联的计算机组,并以单一系统的模式加以管理 价格很多服务器集中起来,提供同一种服务,在客户端看起来就像只有一个服务器 可以在付出较低成本的情况下获得在性能,可靠性 ...

  9. ViewPager 几个状态详解

    ViewPager.SCROLL_STATE_DRAGGING 当用户按下ViewPager视图并且需要滑动第一下时; ViewPager.SCROLL_STATE_SETTLING: 当用户滑动的放 ...

  10. typeScript 学习

    最近看了下typescript, 虽然说已经有很多人已经用到它了,但是我还是写写自己的feel咯:这里推荐学习链接 https://ts.xcatliu.com. 这个入门学习,我不好做评价,但是我自 ...