准备工作

基于 Hugo 搭建的静态博客网页代码托管在 Github 上,我们可以直接使用 Github 的地址访问。但是为了提高加载速度,我们也可以选择将博客部署到各大厂商的云服务器上。

在此之前,需要完成包括但不限于以下准备工作:

  • 购买云服务器;
  • 购买域名;
  • 域名解析和 ICP 备案;
  • 防火墙/安全组设置。

安装宝塔面板

该步骤不是必选项,但是为了方便后续使用,建议安装宝塔面板,安装后可以使用图形化界面操作云服务器。

参考链接:宝塔面板安装教程


安装SSL证书

该步骤不是必选项,但是安装 SSL 证书后我们的博客网站支持 https 访问,推荐安装。

一般我们购买的阿里云/腾讯云等平台的服务器,都可以使用免费的 SSL 证书。证书的有效期一般为一年,到期时可以提前免费续约。以阿里云为例,参考教程:在Nginx或Tengine服务器上安装证书


配置Nginx

在步骤2中,我们安装宝塔面板的同时一般已经安装了 Nginx,输入nginx -v查看。

输入ps -ef | grep nginx可以查看正在运行的 Nginx 进程,我们可以看到宝塔面板安装的 Nginx 配置文件是:/www/server/nginx/conf/nginx.conf,个人配置如下:

代码
 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
user  www www;
worker_processes auto;
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

stream {
    log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';

    access_log /www/wwwlogs/tcp-access.log tcp_format;
    error_log /www/wwwlogs/tcp-error.log;
    include /www/server/panel/vhost/nginx/tcp/*.conf;
}

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }

http
    {
        include       mime.types;
		include proxy.conf;
        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        limit_conn_zone $binary_remote_addr zone=perip:10m;
		limit_conn_zone $server_name zone=perserver:10m;

        server_tokens off;
        access_log off;

server
    {
        listen 443 ssl;
        server_name kdjlyy.cn;
		root html;
        index index.html index.htm index.php;

    	#填写证书文件名称
    	ssl_certificate cert/xxxxxxx_kdjlyy.cn.pem;
  		#填写证书私钥文件名称
   		ssl_certificate_key cert/xxxxxxx_kdjlyy.cn.key;
    	ssl_session_timeout 5m;
    	#表示使用的加密套件的类型
    	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    	#表示使用的TLS协议的类型,您需要自行评估是否配置TLSv1.1协议。
    	ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    	ssl_prefer_server_ciphers on;
    	location / {
        	root /www/wwwroot/public/ ;  # 站点目录设置为之前Hugo生成的public路径即可
       		index index.html;
    	}
        access_log  /www/wwwlogs/access.log;
    }
	# 设置HTTP请求自动跳转HTTPS
	server {
   		listen 80;
   	 	server_name kdjlyy.cn; #需要将yourdomain替换成证书绑定的域名。
  	  	rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
  	 	location / {
   	   		index index.html index.htm;
   	 	}
	}
include /www/server/panel/vhost/nginx/*.conf;
}

需要更改的地方为第68、73、75、83、91行,配置完 Nginx 后把博客的public目录上传到云服务器上,对应 Nginx 配置的 location 中的 root 字段。

然后输入命令重启 Nginx 即可通过域名访问博客。

1
/www/server/nginx/sbin/nginx -s reload