1. 程式人生 > >Nginx SSL 證書部署 並配置http重定向到https

Nginx SSL 證書部署 並配置http重定向到https

取自騰訊雲文件結合自己站點,總結如下:

1.獲取證書

證書申請下來之後 ,下載證書,解壓之後圖片:
在這裡插入圖片描述
這裡我們使用 nginx 伺服器 ,開啟資料夾如下:
在這裡插入圖片描述

2.部署證書

將域名 www.bug404.club 的
證書檔案 1_www.bug404.club_bundle.crt 、
私鑰檔案 2_www.bug404.club.key
儲存到同一個目錄,例如 /usr/local/nginx/conf 目錄下(隨便一個目錄,後面配置nginx需要引入)

3.nginx配置

server {
        listen 443;
        server_name www.bug404.club; #填寫繫結證書的域名
        ssl on;
        ssl_certificate 1_www.bug404.club_bundle.crt;
        ssl_certificate_key 2_www.bug404.club.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照這個套件配置
        ssl_prefer_server_ciphers on;
        location / {
            root   html; #站點目錄
            index  index.html index.htm;
        }
    }

配置完成後,開放443埠,詳見CentOS7 防火牆 firewalld使用方法,重啟 nginx。就可以使用 https://www.bug404.club 來訪問。

配置檔案引數 說明
listen 443 SSL 訪問埠號為 443
ssl on 啟用 SSL 功能
ssl_certificate 證書檔案
ssl_certificate_key 私鑰檔案
ssl_protocols 使用的協議
ssl_ciphers 配置加密套件,寫法遵循 openssl 標準

4.使用全站加密,http 自動跳轉 https(可選)

對於使用者不知道網站可以進行 https 訪問的情況下,讓伺服器自動把 http 的請求重定向到 https。
在伺服器這邊的話配置的話,可以在頁面里加 js 指令碼,也可以在後端程式裡寫重定向,當然也可以在 web 伺服器來實現跳轉。Nginx 是支援 rewrite 的(只要在編譯的時候沒有去掉 pcre)
httpserver (另外寫一個server)裡增加 rewrite ^(.*) https://$host$1 permanent;


這樣就可以實現 80 進來的請求,重定向為 https 了。

5.我的配置

#
# The default server
#
server {
    listen 443;
    server_name www.bug404.club; #填寫繫結證書的域名
    ssl on;
    ssl_certificate 1_www.bug404.club_bundle.crt;
    ssl_certificate_key 2_www.bug404.club.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照這個套件配置
    ssl_prefer_server_ciphers on;

    charset utf8;

    access_log  logs/host.access.log  main;

    location / {
        root   /var/www/html/index/;
        index  index.html index.php index.htm;
    # example
        #ModSecurityEnabled on;
        #ModSecurityConfig /etc/nginx/modsecurity.conf;
        autoindex on;                            #開啟目錄瀏覽功能;   
        autoindex_exact_size off;            #關閉詳細檔案大小統計,讓檔案大小顯示MB,GB單位,預設為b;   
        autoindex_localtime on;              #開啟以伺服器本地時區顯示檔案修改日期!
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
server{
	listen 80;
    server_name www.bug404.club;
    rewrite ^(.*) https://$host$1 permanent;
}