收藏文章 楼主

nginx 环境下配置 ssl 证书

版块:linux   类型:普通   作者:小绿叶技术博客   查看:985   回复:0   获赞:3   时间:2019-10-23 13:51:06

#说明:申请ssl 时候需要先关闭转跳到443,默认到80端口,否则验证证书过慢或者无法通过

1. nginx 配置https 和转跳。2. php 转跳到https


[root@eisc_cn ~]# cat /www/server/panel/vhost/nginx/www.eisc.cn.conf



###############################  简单配置案例 ##########################################


server{
    listen 80;
    listen 443 ssl;
    server_name www.eisc.cn eisc.cn www.ddoss.cn ddoss.cn;

    #-------- 证书配置 ---------#
    ssl_certificate     /etc/letsencrypt/live/eisc.cn/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/eisc.cn/privkey.pem;
    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 ~ \.php {

        # fastcgi_pass   unix:/var/run/php-fpm811.sock; 
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    #--- nginx 代理 ---#
    # location / {
    #     proxy_pass http://10.1.1.11:62010;
    #     #---- 记录 IP 地址 ----#
    #     proxy_redirect off;
    #     proxy_set_header Host $http_host;
    #     proxy_set_header X-Real-IP $remote_addr;	    
    #     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # }
}




#---- 完整配置案例 ---#


server
{
    listen 443 ssl;
    listen 80;
    server_name eisc.cn www.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/eisc.pem;
ssl_certificate_key /www/wwwroot/www/ssl443/eisc.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;
#------- ssl 301 转跳 -----#
    if ($server_port = 80){
          rewrite ^(/.*)$ https://$host$1 permanent;
       }
#---------------------------#
location /ccb/ {
    return 301 http://work.eisc.cn;
    proxy_pass https://www.eisc.cn;
    index  index.html index.php;
}
    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]


#-----------  php 环境转跳到https -------------#


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 或者转跳到指定域名



相关连接:

301 转跳到ssl证书 https 或者转跳到指定域名

提供企业建站服务,免费网防系统,提交信息登录 http://yundun.ddoss.cn 邮箱: proposal@ddoss.cn 
回复列表
默认   热门   正序   倒序

回复:nginx 环境下配置 ssl 证书

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息