詳解nginx如何配置HTTPS

使用ssl模塊配置同時支持http和https并存

寧縣網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)自2013年創(chuàng)立以來到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

一,生成證書

# 1、首先,進(jìn)入你想創(chuàng)建證書和私鑰的目錄,例如:
cd /etc/nginx/

# 2、創(chuàng)建服務(wù)器私鑰,命令會讓你輸入一個口令:
openssl genrsa -des3 -out server.key 1024

# 3、創(chuàng)建簽名請求的證書(CSR):
openssl req -new -key server.key -out server.csr

# 4、在加載SSL支持的Nginx并使用上述私鑰時除去必須的口令:
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key


# 5、最后標(biāo)記證書使用上述私鑰和CSR:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

二,配置nginx

cd /etc/nginx
vim nginx.conf
#
# HTTPS server configuration
#
server {
  listen    443;
  server_name 本機(jī)的IP地址;

  ssl         on;
  ssl_certificate   /etc/nginx/server.crt;
  ssl_certificate_key /etc/nginx/server.key;

  ssl_session_timeout 5m;

#  ssl_protocols SSLv2 SSLv3 TLSv1;
#  ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#  ssl_prefer_server_ciphers  on;

  location / {
    #root  html;
    #index testssl.html index.html index.htm;
   proxy_redirect off;
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_pass http://IP地址/ssl/;
  }
}

重啟nginx,在瀏覽器輸入:https://IP地址 會跳轉(zhuǎn)到 http://IP地址/ssl/ 這個地址(后期調(diào)整為webservice接口地址即可)

三、受瀏覽器信任的StartSSL免費(fèi)SSL證書

跟VeriSign一樣,StartSSL(網(wǎng)址:http://www.startssl.com,公司名:StartCom)也是一家CA機(jī)構(gòu),

它的根證書很 久之前就被一些具有開源背景的瀏覽器支持(Firefox瀏覽器、谷歌Chrome瀏覽器、蘋果Safari瀏覽器等)。

四、項(xiàng)目需要,將訪問目錄 \services\ 由http訪問 重定向到 https (解決方法:nginx rewrite 加上 location 方式實(shí)現(xiàn))

location ~ /services/.*$ {
    if ($server_port ~ "^80$"){
      set $rule_0 1$rule_0;
    }
    if ($rule_0 = "1"){
      rewrite /(.*) https://IP地址/$1 permanent;            break;
    }
  }

五,配置結(jié)束上傳以后用nginx -t 測試下配置無誤 就reload一下nginx服務(wù) 檢查443端口是否在監(jiān)聽

/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful (顯示表示配置文件沒有錯誤)

service nginx reload (重新加載nginx服務(wù)) 
netstat -lan | grep 443 (查看443端口) 
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN (有看到這一行 就表示HTTPS已經(jīng)在工作了) 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

分享題目:詳解nginx如何配置HTTPS
本文網(wǎng)址:http://muchs.cn/article48/iheshp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、網(wǎng)站營銷靜態(tài)網(wǎng)站、Google、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)