Blog

Blog

PHODAL

网站 https 创建

在听闻: Google调整搜索引擎算法:HTTPS网站排名更高 这样一个消息后,便自觉地打算给自己的博客加上HTTPS。虽然没有多大作用,而且只有1%的网站会受影响,但是还是加了。这里的SSL证书只是自己生成的,暂时没有第三方机构认证,所以会有证书问题。只有等过些时候买一个认证的。

Nginx SSL证书创建

前提: 需要安装好Nginx。

1.创建证书存放目录

sudo mkdir /etc/nginx/ssl
cd /etc/nginx/ssl

2.创建服务器密钥

 sudo openssl genrsa -des3 -out server.key 1024

过程中会要求输入短语,相当于某种意义上的密码,在下一步会用到。

3.创建证书签名请求

 sudo openssl req -new -key server.key -out server.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) [AU]:CN
State or Province Name (full name) [Some-State]:SHAANXI
Locality Name (eg, city) []:XIAN
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Mokcy Design
Organizational Unit Name (eg, section) []:MQ
Common Name (e.g. server FQDN or YOUR name) []:www.phodal.com
Email Address []:h@phodal.com

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

需要注意的是:

  • Comman Name是网站的域名或者IP。
  • 最后两个challenge password和company name为空

4.删除短语

 sudo cp server.key server.key.org
 sudo openssl rsa -in server.key.org -out server.key

5.签名证书

  sudo openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

365指的是有效期

修改Nginx配置

为了保证原来的域名还是可以工作的,因为现在的证书还没有经过第三方机构认证,所以保留一下原来的80端口,ssl端口默认的是443。So,我们需要添加的是SSL那三行,还有,listen 443。

server {
        listen 80;
        server_name phodal.com;

        root /usr/share/nginx/www;
        index index.html index.htm;
}
server {
        listen 443;
        server_name phodal.com;

        root /usr/share/nginx/www;
        index index.html index.htm;

        ssl on;
        ssl_certificate /etc/nginx/ssl/server.crt;
        ssl_certificate_key /etc/nginx/ssl/server.key; 
}

需要注意的是

  • 可能还需要网站程序的支持(未验证过),Mezzanine可以直接用SSL。
  • 确保你的网站开启了443端口

重新载入Nginx配置

这里用的是CentOS,SO

 sudo /etc/init.d/nginx reload

接着打开https://www.phodal.com就可以看到效果了。

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签