日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个命令,让你的网站支持https

發(fā)布時間:2025/3/16 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个命令,让你的网站支持https 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

介紹

最近寫小程序,但是小程序的接口得通過https的形式訪問,所以用nginx轉(zhuǎn)發(fā)了一下。想到Google搜索引擎會提高https網(wǎng)站的權(quán)重,索性把我的網(wǎng)站改造成同時支持http和https了。看效果

http形式訪問


https形式訪問

我是用LET'S ENCRYPT來生成證書的,這個應(yīng)該是用的最多的一個工具,也很方便。

我直接參考了官方文檔上關(guān)于在CentOS 7上用nginx來讓網(wǎng)站支持https的教程

1.下載

sudo?yum?install?certbot?python2-certbot-nginx

2.執(zhí)行命令

certbot?--nginx?-d?www.erlie.cc

如果不在這個目錄可以用--nginx-server-root這個參數(shù)來指定

certbot?--nginx?--nginx-server-root=/yourpath?-d?www.erlie.cc????

如果要為多個域名生成的話

-d?域名1?-d?域名2

或者每次指定一個,執(zhí)行多次

如果選 1,則通過 HTTP 和 HTTPS 都可以訪問。

中途中可能會遇到各種環(huán)境問題,Google解決即可。

我說一個我遇到的坑,我原來編譯的時候沒有選擇SSL module,所以我得重新編譯一下,編譯完成之后我是用如下命令重啟的

nginx?-s?reload

結(jié)果https訪問一直有問題,后來我才意識到。sbin目錄下重新生成的nginx得重啟才能生效。

此時http://www.erlie.cc和https://www.erlie.cc就都可以訪問了

nginx.conf配置

server?{listen???????80;server_name??www.erlie.cc;#charset?koi8-r;access_log??/usr/local/nginx/logs/access.log?combined;location?=?/?{root???/product/new-blog-fe/dist/view;index??index.html;}location?~?.*\.html$?{root???/product/new-blog-fe/dist/view;index??index.html;}location?/?{proxy_pass??http://127.0.0.1:8080/;}location?~?.*\.(gif|jpeg|png|bmp|swf|flv|ico)$?{root???/product/new-blog-fe;if?(-f?$request_filename)?{expires?1d;break;}}location?~?.*\.(js|css)?$?{root???/product/new-blog-fe;if?(-f?$request_filename)?{expires?1d;break;}}#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;}listen?443?ssl;?#?managed?by?Certbotssl_certificate?/etc/letsencrypt/live/www.erlie.cc/fullchain.pem;?#?managed?by?Certbotssl_certificate_key?/etc/letsencrypt/live/www.erlie.cc/privkey.pem;?#?managed?by?Certbotinclude?/etc/letsencrypt/options-ssl-nginx.conf;?#?managed?by?Certbotssl_dhparam?/etc/letsencrypt/ssl-dhparams.pem;?#?managed?by?Certbot }

最后面的5行就是前面的命令自動幫你生成的,非常方便,都不用自己配置。

前端怎么寫?

為了讓前端在訪客通過http方式訪問的時候調(diào)用http接口。通過https訪問的時候調(diào)用https接口,我是這樣做的

nb.js

var?conf?=?{serverHot?:?window.location.origin }; var?_nb?=?{request:?function?(param)?{var?_this?=?this;$.ajax({type???????:?param.method?||?'get',url????????:?param.url?||?'',dataType???:?param.type?||?'json',data???????:?param.data?||?'',xhrFields??:?{withCredentials:?true},crossDomain:?true,contentType:?param.contentType?||?'application/x-www-form-urlencoded;charset=UTF-8',success:?function?(res)?{typeof?param.success?===?'function'?&&?param.success(res.data,?res.msg);},error:?function?(err)?{typeof?param.error?===?'function'?&&?param.error(err.statusText);}})},//?獲取服務(wù)器地址getServerUrl?:?function(path)?{return?conf.serverHot?+?path;} }module.exports?=?_nb;

window.location.origin為拿到https://www.erlie.cc類似這樣的請求地址,可以在瀏覽器自己試一下


user-service.js
請求用戶相關(guān)的接口
var?_nb?=?require('util/nb.js');var?_user?=?{//?用戶登錄login?:?function(userInfo,?resolve,?reject){_nb.request({url?????:?_nb.getServerUrl('/user/login'),data????:?userInfo,method??:?'POST',success?:?resolve,error???:?reject});} } module.exports?=?_user;

userInfo為請求參數(shù)

有道無術(shù),術(shù)可成;有術(shù)無道,止于術(shù)

歡迎大家關(guān)注Java之道公眾號

好文章,我在看??

新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎!定制產(chǎn)品紅包拿不停!

總結(jié)

以上是生活随笔為你收集整理的一个命令,让你的网站支持https的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。