1. 程式人生 > 其它 ># Nginx 頒發自簽證書

# Nginx 頒發自簽證書

#1.Nginx 頒發自簽證書

對於CentOS-7 的系統,預設 nginx 的SSL配置路徑是 /etc/pki/nginx/
建議先進入這個目錄,然後再執行下文的生成操作,最後把 nginx.key 移動到 private 目錄中即可

首先,確保安裝了OpenSSL庫,並且安裝Nginx時使用了 –with-http_ssl_module引數。

1.1:進入生成證書的目錄

  cd  /etc/pki/nginx/

1.2:使用openssl建立建立伺服器私鑰,輸入相應提示的資訊

  openssl genrsa -des3 -out nginx.key 2048(nginx 是自己定義的,檔名而已)

輸入密碼後,再次重複輸入確認密碼。記住此密碼,後面會用到

1.3:建立證書籤名請求

  openssl req -new -key  nginx.key -out  nginx.csr

1.4:清除以SSL啟動Nginx時提示必須輸入金鑰

cp  nginx.key nginx.key.org
openssl rsa -in nginx.key.org -out nginx.key

1.5:使用剛生成的私鑰和CSR進行證書籤名

openssl x509 -req -days 3650 -in nginx.csr -signkey nginx.key -out nginx.crt

1.6:移動私鑰到 private
目錄(情況由 Nginx 配置檔案中的 ssl_certificate_key 決定)

# cd  /etc/pki/nginx/
\cp -rf  nginx.key  private/

1.7: 在nginx 的 server{} 中加入以下配置(如果沒有):

    listen       443 ssl http2 default_server;
    listen       [::]:443 ssl http2 default_server;

    ssl_certificate "/etc/pki/nginx/nginx.crt";
    ssl_certificate_key "/etc/pki/nginx/private/nginx.key";
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

#2. 常見問題處理

The plain HTTP request was sent to HTTPS port

Nginx監聽4441埠使用https協議,瀏覽器輸入http://hostname:4441時會報這樣的錯誤。在使用 http 訪問 https 時,就會報497錯誤
用error_page 處理一下即可:

server {
    listen       4441 ssl;
    ...
    ...
    error_page  497 https://$host:4441;
}

Blocked loading mixed active content


2、程式碼:

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

PS: 對於我司系統來說,只要在 fs_web/index.html 中的 header 中增加上述描述資訊即可,見下圖