申请并下载证书

首先,前往https://freessl.cn/注册账号,根据指示填写域名,点击创建免费的SSL证书

CSR生成选择离线生成

点击生成后,会提示先安装KeyManager,根据指示安装即可

根据指示一直走,可以看见证书已经出现在了KeyManager里,点击导出证书

选择Nginx并导出

打开压缩包,可以看见有两个格式的文件:.crt.key

配置服务器

现在需要将证书上传至服务器,位置任意,例如我这里放在了/root/ssl

接下来编辑Nginx的配置文件

1
sudo nano /etc/nginx/sites-available/default

server把80端口的那部分改成下面几行

1
2
3
4
5
#监听 443 端口 (HTTPS用的是443)
listen 443 ssl;
#添加证书
ssl_certificate /root/ssl/www-nickxu-top-0223222924_chain.crt;
ssl_certificate_key /root/ssl/www-nickxu-top-0223222924_key.key;

然后再开一个server来处理80端口

1
2
3
4
5
6
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.nickxu.top;
rewrite ^(.*) https://$server_name$1 permanent; #重定向至https协议
}

完整的配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.nickxu.top;
rewrite ^(.*) https://$server_name$1 permanent;
}

server {

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

listen 443 ssl;
ssl_certificate /root/ssl/www-nickxu-top-0223222924_chain.crt;
ssl_certificate_key /root/ssl/www-nickxu-top-0223222924_key.key;


root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name www.nickxu.top;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

重启,可以看见已经成功启用了SSL证书