Nginx 配置 HTTPS

18 篇文章 2 订阅

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

解决办法看我博客https://blog.csdn.net/qq_36850813/article/details/102383540 

报错一:

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;
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值