1. 基本思路
- 使用 certbot 来申请和维护证书(参见 Nginx on Ubuntu 18.04 LTS (bionic))
- 使用 Really Simple SSL 插件
2. 操作过程
2.1 安装 Certbot 与使用
# 1. 安装
apt-get update
apt-get install software-properties-common
add-apt-repository universe
add-apt-repository ppa:certbot/certbot
apt-get update
apt-get install certbot python3-certbot-nginx
# 2. 申请证书
certbot --nginx
# 3. 刷新证书
certbot renew
2.2 Nginx 配置
如果我们的 Nginx 服务器配置了多个域名就可能会把 Nginx 配置拆分到多个文件,比如 /etc/nginx/conf.d/note4code.com.conf
:
upstream wordpress {
server 127.0.0.1:8000;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/letsencrypt/live/note4code.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/note4code.com/privkey.pem;
server_name note4code.com www.note4code.com;
access_log /root/logs/ngxin/note4code.com.access.log;
location / {
proxy_pass http://wordpress/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
#return 301 https://$server_name$request_uri;
server_name www.note4code.com note4code.com;
access_log /root/logs/ngxin/note4code.com.access.log;
location / {
proxy_pass http://wordpress/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
3. 要解决的问题
即使我们配置了 HTTPS 证书和 Nginx 配置文件,在我们打开页面的时候地址栏左侧依然没有安全连接的标志。
这是因为 WordPress 的部分代码中依然引用了 HTTP 的非安全资源,尝试了网上给出的很多方法,比如修改 WordPress 里面的函数等效果都不太好。
但其实最简单的方法其实就是使用 Really Simple SSL 插件。不需要付费版,免费版就足够使用了。
