在这个到处都https的情况下,加一个还是挺好的。废话不多说,开始搞吧!
初始化服务器!
安装nginx
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install -y nginx
反代一个易读程序,并且添加一个 /.well-known/ 的本地设置
server
{
listen 80;
server_name tbfensi.com www.tbfensi.com m.tbfensi.com;
root /www/wwwroot/tbfensi.com;
location /
{
proxy_pass http://192.187.xxx.xxx;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_cache cache_one;
proxy_cache_key $host$request_uri$is_args$args;
proxy_cache_valid 200 304 301 302 1h;
add_header X-Cache $upstream_cache_status;
expires 12h;
}
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.project|LICENSE|README.md)
{
return 404;
}
location ^~ /.well-known/ {
root /www/wwwroot/tbfensi.com;
}
access_log /www/wwwlogs/tbfensi.com.log;
}
开放80和443端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --reload
安装letsencrypt
yum -y install git
git clone https://github.com/letsencrypt/letsencrypt.git
安装证书
./letsencrypt-auto certonly --webroot --webroot-path /www/wwwroot/tbfensi.com -d tbfensi.com -d www.tbfensi.com -d m.tbfensi.com -m 1111@qq.com
说明:
--webroot-path 设置根目录,脚本会在这个目录下生成验证临时文件夹.well-known,下面有验证文件
-d 是域名,包括各种a记录,需要认证的,我们这里是www,m和@
-m 邮箱,填自己的邮箱就可以了。
安装证书的时候会出现两个问答,第一个A,第二个N就行了。具体你可以看英文
然后出现下面的提示就成功了!
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/tbfensi.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/tbfensi.com/privkey.pem
Your cert will expire on 2018-03-29. To obtain a new or tweaked
version of this certificate in the future, simply run
letsencrypt-auto again. To non-interactively renew *all* of your
certificates, run "letsencrypt-auto renew"
- If you like Certbot, please consider supporting our work by:
生成一个dhparam.pem文件,时间有点长,2048位的
openssl dhparam -out dhparam.pem 2048
到这里证书就做完了,下面我们部署到nginx里,修改配置文件。
#80端口301到https
server {
listen 80;
server_name tbfensi.com www.tbfensi.com m.tbfensi.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name tbfensi.com www.tbfensi.com m.tbfensi.com;
location /
{
proxy_pass http://192.187.xxx.xxx;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_cache cache_one;
proxy_cache_key $host$request_uri$is_args$args;
proxy_cache_valid 200 304 301 302 1h;
add_header X-Cache $upstream_cache_status;
expires 12h;
}
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.project|LICENSE|README.md)
{
return 404;
}
ssl on;
#ssl_prefer_server_ciphers on;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE;
#ssl_prefer_server_ciphers on;
ssl_session_cache shared:TLSSL:16m;
ssl_session_timeout 10m;
ssl_certificate /etc/letsencrypt/live/tbfensi.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/tbfensi.com/privkey.pem;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
#add_header X-Frame-Options SAMEORIGIN;
add_header Strict-Transport-Security max-age=16000000;
}
重启nginx,再打开网站应该是https啦。
systemctl restart nginx.service
恭喜!!!
另外,letsencrypt证书的有效期是3个月,做一个自动更新的脚本就可以不用管了
还有很重要的一步,如果没有显示绿色小标的话,应该是引用了非https资源,把这些资源都变成https就可以了