騰訊雲免費SSL證書配置

基於ngnix的https配置

1.證書上傳

1)申請證書請參照官方文檔,而後把已經頒發下來的證書下載下來。php

2)解壓文件,而後把ngnix文件夾下的1_xxx.com_bundle.crt和2_xxx.com.key上傳到服務器的nginx配置文件目錄(上傳到同一目錄),如:/usr/local/nginx/conf,我上傳的目錄是/usr/local/nginx/conf/ssl/www。個人服務器上的nginx安裝在/usr/local/nginx目錄。html

2.證書安裝

修改/usr/local/nginx/conf目錄下nginx.conf配置文件。nginx

server {
      listen         443 ssl; #監聽443端口
      server_name    xxx.com;#域名,換成本身域名
      ssl_certificate      ssl/www/1_xxx.com_bundle.crt;#證書路徑,換成本身證書名
      ssl_certificate_key  ssl/www/2_xxx.com.key;#key路徑,換成本身key
                      
      ssl_session_timeout  5m; #會話過時時間                                               

      ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #爲創建安全鏈接,服務器所容許的密碼格式列表                                          
      ssl_prefer_server_ciphers  on; #依賴SSLv3和TLSv1協議的服務器密碼將優先於客戶端密碼

      location /     {
              root     /usr/share/nginx/html; #域名訪問的根目錄,換成本身根目錄
              index    index.html index.htm index.php;
      }

      location ~ \.php$ {
          root           /usr/share/nginx/html; #域名訪問的根目錄,換成本身根目錄
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include        fastcgi_params;
      }
 }

配置完成後,先用nginx下的./sbin/nginx –t來測試下配置是否有誤,正確無誤的話,重啓nginx(./sbin/nginx –s reload)。就能夠使 https://xxx.com (本身域名)來訪問了。 註釋:瀏覽器

配置文件參數 說明
listen 443 SSL訪問端口號爲443
ssl on 啓用SSL功能
ssl_certificate 證書文件
ssl_certificate_key 私鑰文件
ssl_protocols 使用的協議
ssl_ciphers 配置加密套件,寫法遵循openssl標準

3 使用全站加密,http自動跳轉https

對於用戶不知道網站能夠進行https訪問的狀況下,讓服務器自動把http的請求重定向到https。 在nginx.conf配置文件的http的監聽80端口的server中,找到安全

location / {
            #root   html;
            rewrite ^(.*) https://$host$1 permanent; #重定向到https
            index  index.php index.html index.htm;
        }

配置,在裏面增長重定向配置 rewrite ^(.*) https://$host$1 permanent; 配置完成後,先用nginx下的./sbin/nginx –t來測試下配置是否有誤,正確無誤的話,重啓nginx(./sbin/nginx –s reload)。就能夠使 http://xxx.com (本身域名)來訪問了,看看是否在瀏覽器中自動變成https連接。服務器

4.遇到的問題

server {
        listen 80;
        server_name liuhangs.com;
        #return 301 https://$server_name$request_uri;
        rewrite ^(.*)$ https://$server_name$1 permanent;
}
相關文章
相關標籤/搜索