前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx启用https访问

Nginx启用https访问

作者头像
天策
发布2018-06-22 14:33:52
6.7K2
发布2018-06-22 14:33:52
举报
文章被收录于专栏:行者悟空行者悟空

摘 要

nginx启用ssl加密访问。

由于本站全面开启https协议访问,小编决定将实践过程记录下来,方便后来人。

什么是https?

https 全称:Hyper Text Transfer Protocol over Secure Socket Layer,是http的安全版。即http下加入SSL协议层,因此https的安全基础就是SSL,所以加密内容需要SSL。

Nginx启用https访问
Nginx启用https访问
配置过程

首先需要申请一个证书,可以申请一个免费得。然后会得到nginx版本证书,一个公钥,一个私钥。将其上传到服务器目录。本次将讲解两种配置方式。

先确认nginx安装时已编译http_ssl模块,也就是执行nginx -V命令查看是否存在--with-http_ssl_module。如果没有,则需要重新编译nginx将该模块加入。

http与https共存方式

意思是指网站可以通过http请求访问,也可以通过https请求访问。注:http端口为80https端口为443。

代码语言:javascript
复制
server {
        listen       80;
        listen 443 ssl;
         ssl_certificate ssl/itunic.crt; #证书公钥文件路径
         ssl_certificate_key  ssl/itunic.key;   #证书私钥文件路径
         ssl_session_timeout  5m;  #5分钟session会话保持
         ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
         ssl_ciphers  HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
        root /www/web/itunic_com/public_html;
        server_name itunic.com www.itunic.com;
        index  index.html index.php index.htm;
        error_page  400 /errpage/400.html;
        error_page  403 /errpage/403.html;
        error_page  404 /errpage/404.html;
        error_page  503 /errpage/503.html;
        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                include fcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
                expires      1d;
        }
        location ~ .*\.(js|css|html|htm)?$ {
                expires      12h;
        }
        location ~ /\.ht {
                deny  all;
        }
        access_log  logs/itunic.com_access.log  wwwlogs;
        error_log  logs/itunic.com_error.log;
}
将http强制转https
代码语言:javascript
复制
server{
      listen 80;
      server_name itunic.com www.itunic.com;
      location / {
          rewrite (.*) https://itunic.com$1 permanent;
      }
   }
server {
        listen 443 ssl;
         ssl_certificate ssl/itunic.crt; #证书公钥文件路径
         ssl_certificate_key  ssl/itunic.key;   #证书私钥文件路径
         ssl_session_timeout  5m;  #5分钟session会话保持
         ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
         ssl_ciphers  HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
        root /www/web/itunic_com/public_html;
        server_name itunic.com www.itunic.com;
        index  index.html index.php index.htm;
        error_page  400 /errpage/400.html;
        error_page  403 /errpage/403.html;
        error_page  404 /errpage/404.html;
        error_page  503 /errpage/503.html;
        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                include fcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
                expires      1d;
        }
        location ~ .*\.(js|css|html|htm)?$ {
                expires      12h;
        }
        location ~ /\.ht {
                deny  all;
        }
        access_log  logs/itunic.com_access.log  wwwlogs;
        error_log  logs/itunic.com_error.log;
}

本站采用第二种也推荐第二种,即强制转https。原因有二:第一,全站加密,更安全。第二,有利于SEO,在搜索引擎方面,https权重高于http。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年02月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 什么是https?
  • 配置过程
    • http与https共存方式
      • 将http强制转https
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档