---
title: "nginx配置https服务器"
author: "ciaos"
date: "2015-09-04T15:23:16Z"
tags: []
categories: ["开源软件"]
slug: "nginx-https"
draft: false
lastmod: "2015-09-04T15:23:16Z"
id: 2240601
url: "https://ciaos.iteye.com/blog/2240601"
---

＃ 生成CA的key

&gt; openssl genrsa -des3 -out ca.key 4096

 

\# 生成CA的证书

&gt; openssl req -new -x509 -days 365 -key ca.key -out ca.crt

 

\# 生成我们的key和CSR这两步与上面Self Signed中是一样的

&gt; openssl genrsa -des3 -out myserver.key 4096

&gt; openssl req -new -key myserver.key -out myserver.csr

 

\# 使用ca的证书和key，生成我们的证书

\# 这里的set\_serial指明了证书的序号，如果证书过期了(365天后)，

\# 或者证书key泄漏了，需要重新发证的时候，就要加1

&gt; openssl x509 -req -days 365 -in myserver.csr -CA ca.crt -CAkey ca.key -set\_serial 01 -out server.crt

 

获取免密码版本

&gt; openssl rsa -in myserver.key -out server.key

 

注意：提示Common Name (eg, your name or your server's hostname)时设置用于ssl的hostname

 

nginx配置文件添加ssl server配置

server {

        listen 443;

        server\_name www.ciaos.com;

 

        ssl on;

        ssl\_certificate ssl/myserver.crt;

        ssl\_certificate\_key ssl/myserver.key;

 

        location /api{

                root html/api;

        }

 }

 

测试如下：

wget --ca-certificate=server.crt https://test.ciaos.com/

--2015-09-04 15:18:11--  https://test.ciaos.com/

正在解析主机 test.ciaos.com (download.smartecs.net)... 127.0.0.1

正在连接 test.ciaos.com (test.ciaos.com)|127.0.0.1|:443... 已连接。

    错误: 证书通用名 www.ciaos.com” 与所要求的主机名 test.ciaos.com” 不符。

要以不安全的方式连接至 test.ciaos.com，使用“--no-check-certificate”。

 

这种方法获得的证书只能严格匹配的域名才能使用，无法用于其他域或者子域。