網站通訊協議升級到HTTPS&HTTP2

爲什麼要升級到HTTPS和HTTP2?

http://baijiahao.baidu.com/s?id=1602041305989767011&wfr=spider&for=pcjavascript

https://www.jianshu.com/p/67c541a421f9php

http://www.javashuo.com/article/p-zmrjubpn-g.htmlcss

HTTPS

1、所需條件

  • 域名
  • Web服務器(Nginx,Apache,IIS都行)
  • SSL證書(可經過騰訊雲、阿里雲等申請)
  • 須要Nginx支持SSL(命令nginx -V中,若是出現 ‘-with-http_ssl_module’ 字樣便可)

2、操做步驟

  1. 登陸阿里雲購買免費SSL證書並申請
  2. 下載證書,上傳至服務器
  3. Nginx配置HTTPS服務
  4. 重定向
  5. 重啓Nginx服務
  6. 將html中全部的外鏈資源(如img,css,js,媒體標籤等)url置爲https

3、具體操做

 1.登陸阿里雲購買免費SSL證書(https://www.aliyun.com/product/cas?utm_content=se_1001656059

  阿里雲免費型DV SSL有效期1年,購買後填寫一些信息,提交申請,便可等待簽發。在這裏表揚一下阿里雲,秒速簽發。html

  在這裏講一下,域名驗證類型可選擇自動DNS、手動DNS和文件驗證,在這裏我選擇的是自動的;CSR生成方式選擇系統生成。前端

  驗證階段,根據提示,要將域名添加一條DNS解析記錄,這裏要注意,要去申請域名的網站去操做。好比個人服務器買的阿里雲,域名在新網買的,因此要去新網去設置,而不是在阿里雲。 java

 2.下載SSL證書並上傳至服務器

  選擇Nginx版本證書,在nginx安裝目錄(通常在/etc/nginx)中新建文件夾(cert),放置證書python

 3.Nginx配置HTTPS服務

server { # 這個server標識我要配置了
    listen 80 default_server;       # 我要監聽那個端口
    listen [::]:80 default_server;
    server_name xxx.cn ;      # 你訪問的路徑前面的url名稱
    access_log /var/log/nginx/access.log main; # Nginx日誌配置
    charset utf-8; # Nginx編碼
    gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json   image/jpeg image/gif image/png application/octet-stream; # 支持壓縮的類型
  
    # 注意添加下面這行代碼,用於重定向
    return 301 https://$server_name$request_uri;

    error_page 404 /404.html; # 錯誤頁面
    error_page 500 502 503 504 /50x.html; # 錯誤頁面
  
    # 指定項目路徑uwsgi
    location / { # 這個location就和我們Django的url(r'^admin/', admin.site.urls),
        include uwsgi_params; # 導入一個Nginx模塊他是用來和uWSGI進行通信的
        uwsgi_connect_timeout 30; # 設置鏈接uWSGI超時時間
        uwsgi_pass unix:/data/wwwroot/script/uwsgi.sock; # 指定uwsgi的sock>文件全部動態請求就會直接丟給他
    }
    # 指定靜態文件路徑
    location /static/ {
        alias /data/wwwroot/maci_proj/static/;
        #index index.html index.htm;
    }
}

        
server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;    
    server_name www.xxxxx.cn ;    
    access_log /var/log/nginx/access.log main; # Nginx日誌配置
    charset utf-8; # Nginx編碼
    gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json   image/jpeg image/gif image/png application/octet-stream; # 支持壓縮的類型

    ssl_certificate /etc/nginx/cert/2050124_xxx.pem;   # pem文件
    ssl_certificate_key /etc/nginx/cert/2050124_xxx.key; # key文件
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    error_page 404 /404.html; # 錯誤頁面
    error_page 500 502 503 504 /50x.html; # 錯誤頁面

    # 指定項目路徑uwsgi
    location / { # 這個location就和我們Django的url(r'^admin/', admin.site.urls),
        include uwsgi_params; # 導入一個Nginx模塊他是用來和uWSGI進行通信的
        uwsgi_connect_timeout 30; # 設置鏈接uWSGI超時時間
        uwsgi_pass unix:/data/wwwroot/script/uwsgi.sock; # 指定uwsgi的sock>文件全部動態請求就會直接丟給他
    }
    # 指定靜態文件路徑
    location /static/ {
        alias /data/wwwroot/maci_proj/static/;
        #index index.html index.htm;
    }
}

 4.重定向

   將全部http請求重定向爲https,如上面配置所述。nginx

   有三種重定向方式:chrome

  • rewrite ^/(.*)$ https://example.com/$1;
  • rewrite ^ https://example.com$request_uri? permanent;
  • return 301 https://example.com$request_uri;

   惟一的區別:正則匹配的性能。第三種性能最優,第一種差一些。然而對於業餘的我,徹底體會不到。json

 5.重啓Nginx服務

nginx -t
nginx -s reload

 6.前端頁面外鏈資源url所有改爲https協議

  不然瀏覽器仍會提示不安全的鏈接

  

   對於這些資源,可以使用https的則使用,若是不能使用能夠將其下載到本身的服務器上。

HTTP2

1、所需條件

 一、openssl 1.0.2+

openssl version  //查看版本的命令

  二、升級HTTPS

· 3.Nginx 1.9.5+ 且 支持

nginx -V  # 查看版本

2、修改Nginx配置文件

 本來的https的listen爲

listen 443 ssl;

 如今在後面加上http2:

listen 443 ssl http2 default_server;

 重啓Nginx

nginx -t
nginx -s reload

 在瀏覽器中能夠查看到協議

 FireFox

   

 chrome

   

相關文章
相關標籤/搜索