使用nginx配置从http到https

由于想做下小程序接口且因为小程序发起请求的API只能是Https形式的域名,所以为了能执行预开发的工作,逐步逐步的去注册购买以及配置。

从域名注册 – 实名认证 – 域名解析(DNS解析DNSPod,解析后的可以通过 http://***.com访问)

以下是域名解析配置

nginx

官网下载

下载后解压

注:不能解压到含中文的目录下

配置 nginx.conf

    server {
        listen       80;
        server_name  www.****.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
        
            proxy_set_header X-Real-IP $remote_addr;
            
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            
            proxy_set_header Host  $http_host;
            
            proxy_set_header X-Nginx-Proxy true;
            
            proxy_set_header Connection "";
        
            proxy_pass http://127.0.0.1:3000;    // 对应 node
        
            proxy_redirect default;

            #root   html;
            #index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

从腾讯云中搜索 SSL证书 ,然后点击 申请免费证书

确定 等待处理

等到处理完成时,即可 下载证书

nginx 证书安装

腾讯云地址

从腾讯下载的证书文件夹中找到 Nginx 文件夹,然后拷贝以下文件:

1_www.****.com_bundle.crt 证书文件和 2_www.****.com.key 私钥文件

在这里插入图片描述

拷贝的文件复制到nginx文件夹中 - conf 文件夹中

conf 文件夹中 - 打开 nginx.conf 然后用以下代码为例进行配置:

server {
     #SSL 访问端口号为 443
     listen 443 ssl; 
     #填写绑定证书的域名
     server_name www.****.com; 
     #证书文件名称
     ssl_certificate 1_www.****.com_bundle.crt; 
     #私钥文件名称
     ssl_certificate_key 2_www.****.com.key; 
     ssl_session_timeout 5m;
     #请按照以下协议配置
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
     #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
     ssl_prefer_server_ciphers on;
     location / {
        #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
         root /var/www/www.****.com; 
         index  index.html index.htm;
     }
 }

server {
listen 80;
#填写绑定证书的域名
server_name www.****.com; 
#把http的域名请求转成https
return 301 https://$host$request_uri; 
}

重启 nginx.exe 即可

最后可以看到成功示例:

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值