TOP

怎样把http协议的网站改成https

作者: admin编辑
日期: 2020-04-25
<p>本网站已经可以使用https了,下面跟着记忆记录一下流程。</p><p>1. 去百度云下载证书 ,好像是在域名管理里面的(<span style="color: rgb(16, 140, 238);">TrustAsia 这个证书是免费的</span>)</p><p>2. 把下载的证书放到服务器上(本人是放在/etc/nginx/cert/目录下)</p><p>3. 在<span style="color: rgb(51, 51, 51);">&nbsp;/etc/nginx/conf.d/ 目录下</span>新建www-sunfengfeng.com.conf 文件</p><p>4. 内容如下</p><pre><code>server { &nbsp; &nbsp; listen 443 ssl; &nbsp; &nbsp; server_name www.sunfengfeng.com; &nbsp; &nbsp; ssl_certificate /etc/nginx/cert/www.sunfengfeng.com.crt; &nbsp; &nbsp; ssl_certificate_key /etc/nginx/cert/www.sunfengfeng.com.key; &nbsp; &nbsp; ssl_session_timeout 5m; &nbsp; &nbsp; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; &nbsp; &nbsp; ssl_protocols TLSv1 TLSv1.1 TLSv1.2;&nbsp; &nbsp;#使用该协议进行配置。 &nbsp; &nbsp; ssl_prefer_server_ciphers on; &nbsp; &nbsp; location / { &nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header X-Real-IP $remote_addr; &nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; &nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header Host $http_host; &nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header X-Nginx-Proxy true; &nbsp; &nbsp; &nbsp; &nbsp; proxy_pass http://main_nginx; &nbsp; &nbsp; &nbsp; &nbsp; proxy_redirect off; &nbsp; &nbsp; } } </code></pre><p>5. 重启nginx</p><pre><code>nginx -s reload</code></pre><p>到现在已经配置完成,现在打开<a href="https://www.sunfengfeng.com">https://www.sunfengfeng.com</a>已经可以正常打开了</p><p>提示: 需要http和https共存的话可以在新建一个.conf后缀的文件就可以了。</p><p>http自动跳转https:</p><pre><code># 在.conf后缀的文件写入下面内容 server { &nbsp; &nbsp; listen 80; &nbsp; &nbsp; server_name www.sunfengfeng.com; &nbsp; &nbsp; rewrite ^(.*)$&nbsp; https://$host$1 permanent;&nbsp;&nbsp;#这行是重点 实现跳转功能 &nbsp; &nbsp; location / { &nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header X-Real-IP $remote_addr; &nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; &nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header Host $http_host; &nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header X-Nginx-Proxy true; &nbsp; &nbsp; &nbsp; &nbsp; proxy_pass http://https_nginx; &nbsp; &nbsp; &nbsp; &nbsp; proxy_redirect off; &nbsp; &nbsp; } } <br></code></pre>