1.首先查看nginx是否安装 http_ssl_module
模块。
nginx -V
如果出现configure arguments: --with-http_ssl_module说明安装了。如果没有则看我之前博客地址如何安装nginxyum安装最新稳定版本nginx
2.ssl 证书部署
这里使用的是阿里云的免费证书,期限为1年,申请地址.
- 下载申请好的 ssl 证书文件压缩包到本地并解压(这里是用的 pem 与 key 文件,文件名可以更改,我这里用的ssl-bundle)。
- 在 nginx 目录新建 cert 文件夹存放证书文件。
- 将这两个文件上传至服务器的 cert 目录里。
3.nginx.conf 配置
编辑/etc/nginx/nginx.conf
server {
listen 443 ssl;
server_name www.aaaa.top;
ssl_certificate cert/ssl-bundle.pem;
ssl_certificate_key cert/ssl-bundle.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
测试访问ok
4.nginx配置http请求重定向到https
把http重定向到https使用了nginx的重定向命令。那么应该如何写重定向?之前老版本的nginx可能使用了以下类似的格式。
server {
listen 80;
server_name www.aaa.top;
rewrite ^(.*)$ https://${server_name}$1 permanent;
#rewrite ^(.*)$ https://$host$1 permanent;
}
或者
rewrite ^ http://domain.com$request_uri? permanent;
现在nginx新版本已经换了种写法,上面这些已经不再推荐。现在网上可能还有很多文章写的是第一种
server {
listen 80;
server_name my.domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name my.domain.com;
[....]
}
以下是我遇到的问题
警告:
[warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
解决办法看我博客javascript:void(0)
报错一:
nginx: [emerg] cannot load certificate "/etc/nginx/cert/ssl-bundle.pem": BIO_new_file() failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/etc/nginx/cert/ssl-bundle.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file)
因为我之前是没有把cert目录建仔仔/etc/nginx下面,必须建在根目录下。
Nginx 设置忽略favicon.ico文件的404错误日志(关闭favicon.ico不存在时记录日志)
在 server { … }内添加如下信息.
location = /favicon.ico {
log_not_found off;
access_log off;
}