4-4 使用Nginx部署HTTPS服務(wù)

生成免費(fèi)ssl證書:

openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout localhost-privkey.pem -out localhost-cert.pem
生成的ssl證書

nginx配置

proxy_cache_path cache levels=1:2 keys_zone=my_cache:10m;


server {#由http跳轉(zhuǎn)到https
  listen       80 default_server;
  listen       [::]:80 default_server;
  server_name  test.com;
  return 302 https://$server_name$request_uri;
}

server {
  listen       443;
  server_name  test.com;

  ssl on;
  ssl_certificate_key /usr/local/etc/nginx/certs/localhost-privkey.pem;
  ssl_certificate /usr/local/etc/nginx/certs/localhost-cert.pem;

  location / {
    proxy_cache my_cache;
    proxy_pass http://127.0.0.1:8888;
    proxy_set_header Host $host; #nginx對(duì)http請(qǐng)求的host進(jìn)行轉(zhuǎn)發(fā)
  }
}

此時(shí),無論訪問http://test.com/還是https://test.com/都會(huì)跳轉(zhuǎn)到https

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容