前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx 环境下配置 ssl 证书 并301转跳https

nginx 环境下配置 ssl 证书 并301转跳https

原创
作者头像
eisc
修改2021-04-30 15:30:37
1.5K0
修改2021-04-30 15:30:37
举报
文章被收录于专栏:linux 自动化运维
代码语言:javascript
复制
linux 服务器安装ssl证书
[root@eisc_cn ~]# cat /www/server/panel/vhost/nginx/www.eisc.cn.conf
server
{
    listen 443 ssl;
    listen 80;
# 需要同时监听 80 和 443端口,否则在转跳的时候出现无法访问
    server_name www.eisc.cn eisc.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/www;
    include enable-php-72.conf;
    include /www/server/panel/vhost/rewrite/www.eisc.cn.conf;
#-------- 证书配置 ---------#
ssl_certificate /www/wwwroot/www/ssl443/4862920_eisc.cn.pem;
ssl_certificate_key /www/wwwroot/www/ssl443/4862920_eisc.cn.key;
# 证书的绝对路径 也可以 ./ssl443/   当前目录下的文件
    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;
    
#------- ssl 301 转跳 -----#
    if ($server_port = 80){
          rewrite ^(/.*)$ https://$host$1 permanent;
                           }

#----------------------------#
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    location ~ \.well-known{
        allow all;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log /dev/null;
    }
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log /dev/null;
    }
    access_log  /www/wwwlogs/www.eisc.cn.log;
    error_log  /www/wwwlogs/www.eisc.cn.error.log;
}
#################################
# vi index.php   写入首页文件转跳到ssl 443端口
// https转跳
if(!((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on')||(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])&&$_SERVER['HTTP_X_FORWARDED_PROTO']=='https'))){
    Header("HTTP/1.1 301 Moved Permanently");
    header('Location: https://www.eisc.cn'.$_SERVER['REQUEST_URI']);
}

###############################################################
虚拟主机安装第三方 ssl 证书教程:
在 腾讯云ssl  申请证书后,有几种方式的证书。这里是Apache方式,进去Apache文件夹有三个文件:
1_root_bundle.crt                               # 这是主证书
2_ssh.gs.crt                                    # 密匙,key在前,crt 在后
3_ssh.gs.key
第一步:
当前域名 允许 使用 SSL 你可以在这里关闭 点击【设置】勾选:SSL加密;PHP 访问;使用一个 符号链接从private_html到public_html - 允许同样的数据在http和https中 ;  然后保存
第二步:
点击:   【粘贴事前准备好的证书和密匙】
以文本打开这两个文件: 3_ssh.gs.key     2_ssh.gs.crt    组合粘贴到此输入框,key在前,crt 在后。【保存】
第三步:
在第二步界面的左下角点击{【点击这里】 来粘贴一个CA根证书}勾选【使用CA证书】 粘贴主密匙文件:1_root_bundle.crt  后保存。
完成
2.系统安装ssl 证书:
find / | grep nginx.conf                                                # 查找  nginx 的安装目录
#/www/server/nginx/conf/nginx.conf.default      # 这是ssl 证书配置文件
rm -rf /www/server/nginx/conf/cert
mkdir /www/server/nginx/conf/cert
cp -rf /root/www.eisc.cn.crt /root/www.eisc.cn.key /www/server/nginx/conf/cert
                                                                                # 创建并将下载的ssl证书文件放入cert文件夹下
vi  nginx.conf.default  # 修改nginx 文件配置ssl
 HTTPS server

    server {
        listen       443 ssl;
        server_name  localhost;
        ssl_certificate      cert/www.eisc.cn.crt;              # 证书地址
        ssl_certificate_key  cert/www.eisc.cn.key;      # 证书地址
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
重启后可以使用https访问,如果开启了云加速等工具,将无法访问手动配置SSl
==============  自动转跳https  ==================
1.若证书已下载,上传到网站
在网站根目录下创建 .htaccess 文件,如果目录下已经有 .htaccess 文件,则用记事本或其他编辑器打开,在最下面添加写入如下语句即可:
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
2. php 环境,在index.php 中增加以下代码
// https转跳
if(!((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on')||(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])&&$_SERVER['HTTP_X_FORWARDED_PROTO']=='https'))){
    Header("HTTP/1.1 301 Moved Permanently");
    header('Location: https://www.eisc.cn'.$_SERVER['REQUEST_URI']);
}
// 将  Location: https://www.eisc.cn'  换成   Location: https://'.$_SERVER['SERVER_NAME'  表示所有域名都转跳https,并随机访问域名
为你提供几个ssl 证书注册地址:
https://buy.cloud.tencent.com/ssl?fromSource=ssl
https://myssl.com/csr_create.html
https://www.aliyun.com/product/cas?spm=5176.10695662.1171680.2.5358481aknwEuK&aly_as=YjLCpm-w
http://aq.chinaz.com/SSL/Index?code=TrustAsiaDVG5#product
相关连接:
301 转跳到ssl证书 https 或者转跳到指定域名

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
轻量应用服务器
轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门软件打包实现一键构建应用,提供极简上云体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档