浏览 6.8k
作者:创客公元
原文链接:Nginx 配置 HTTPS 完整过程
配置站点使用 https,并且将 http 重定向至 https。
http_ssl_module 模块。$ /usr/local/nginx/sbin/nginx -V1
如果出现 configure arguments: --with-http_ssl_module, 则已安装(下面的步骤可以跳过,进入 nginx.conf 配置)。
# 下载安装包到 src 目录
$ cd /usr/local/src
$ wget http://nginx.org/download/nginx-1.14.1.tar.gz$ tar -zxvf nginx-1.14.1.tar.gz$ cd nginx-1.14.1
$ ./configure --prefix=/usr/local/nginx --with-http_ssl_modulemake 命令编译(使用make install会重新安装nginx),此时当前目录会出现 objs 文件夹。$ cp ./objs/nginx /usr/local/nginx/sbin/configure arguments: --with-http_ssl_module说明ssl模块已安装)。$ /usr/local/nginx/sbin/nginx -Vnginx version: nginx/1.14.1
…
configure arguments: –with-http_ssl_module
$ cd /usr/local/nginx
$ mkdir cert$ scp /Users/yourname/Downloads/ssl.pem root@xxx.xx.xxx.xx:/usr/local/nginx/cert/
$ scp /Users/yourname/Downloads/ssl.key root@xxx.xx.xxx.xx:/usr/local/nginx/cert/scp [本地文件路径,可以直接拖文件至终端里面] [<服务器登录名>@<服务器IP地址>:<服务器上的路径>]
nginx.conf 配置编辑 /usr/local/nginx/conf/nginx.conf 配置文件:
server {
# 服务器端口使用443,开启ssl, 这里ssl就是上面安装的ssl模块
listen 443 ssl;
# 域名,多个以空格分开
server_name baidu.com www.baidu.com;
<span class="hljs-comment"># ssl证书地址
<span class="hljs-attribute">ssl_certificate /usr/local/nginx/cert/ssl.pem; <span class="hljs-comment"># pem文件的路径
<span class="hljs-attribute">ssl_certificate_key /usr/local/nginx/cert/ssl.key; <span class="hljs-comment"># key文件的路径
<span class="hljs-comment"># ssl验证相关配置
<span class="hljs-attribute">ssl_session_timeout <span class="hljs-number">5m; <span class="hljs-comment">#缓存有效期
<span class="hljs-attribute">ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; <span class="hljs-comment">#加密算法
<span class="hljs-attribute">ssl_protocols TLSv1 TLSv1.<span class="hljs-number">1 TLSv1.<span class="hljs-number">2; <span class="hljs-comment">#安全链接可选的加密协议
<span class="hljs-attribute">ssl_prefer_server_ciphers <span class="hljs-literal">on; <span class="hljs-comment">#使用服务器端的首选算法
<span class="hljs-attribute">location / {
<span class="hljs-attribute">root html;
<span class="hljs-attribute">index index.html index.htm;
}server {
listen 80;
server_name baidu.com www.baidu.com;
return 301 https://$server_name$request_uri;
}$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf如果 80 端口被占用,用kill [id]来结束进程:
# 查看端口使用
$ netstat -lntpActive Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 21307/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0: LISTEN 3072/sshd
tcp 0 0 0.0.0.0:443 0.0.0.0😗 LISTEN 21307/nginx: master
# 结束 80 端口进程
$ kill 21307
再次重启 nginx :
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
按点赞数排序
按时间排序
微信公众号
加入微信群