Nginx安裝SSL認證時http_ssl_module相關報錯及解決方案

更新時間:
Copy as MD

Nginx伺服器需要通過http_ssl_module模組來載入SSL認證,因此安裝SSL認證前,需要保證當前Nginx已包含此模組。

常見報錯

報錯1:the "ssl" parameter requires ngx_http_ssl_module

原因:當前Nginx伺服器未編譯http_ssl_module模組。

報錯2:nginx: [emerg] unknown directive “ssl” in /usr/local/nginx/conf/nginx.conf

原因:修改設定檔後,執行nginx -s reload報上述錯誤,表明目前的版本的Nginx未編譯http_ssl_module模組。

報錯3:./configure: error: SSL modules require the OpenSSL library

原因:缺少OpenSSL依賴庫。

解決方案

確認伺服器中已安裝了openssl,若當前Nginx未編譯http_ssl_module,還需重新編譯Nginx使其包含http_ssl_module

樣本方案

說明

本文以CentOS 8.0 64位系統,Nginx 1.14.1版本,OpenSSL 1.1.1k版本為例。僅供參考,請根據Nginx的版本,選擇安裝相容的OpenSSL版本。

  1. 檢查目前的版本Nginx是否包含http_ssl_module模組。

    1. 執行以下命令,查看當前Nginx的編譯參數。

      nginx -V | grep http_ssl_module
    2. 如下所示,如果輸出中包含--with-http_ssl_module字樣,即說明已經包含http_ssl_module模組,否則表示當前Nginx安裝版本中不包含該模組,您需要手動添加。

      [root@xxx ~]# nginx -V
      nginx version: nginx/1.14.1
      built with gcc 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC)
      built with OpenSSL 1.1.1 FIPS  11 Sep 2018 (running with OpenSSL 1.1.1k  FIPS 25 Mar 2021)
      TLS SNI support enabled
      configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
  2. 安裝http_ssl_module

    # CentOS為例,openssl 1.1.1k版本,請根據Nginx選擇相容的openssl版本。
    sudo yum install openssl-devel-1.1.1k
  3. 配置http_ssl_module並編譯。

    # 前往Nginx安裝目錄(樣本目錄,請替換為實際的Nginx安裝目錄)
    cd /usr/local/nginx/
    # 配置http_ssl_module,其他參數可自訂
    ./configure --with-http_ssl_module
    # 編譯
    sudo make 
    重要

    注意不能make install,否則會覆蓋原有Nginx的配置。

  4. 再次檢查http_ssl_module模組是否成功安裝。如果未成功安裝,請重複步驟2-3。

    nginx -V | grep http_ssl_module
  5. 重啟Nginx服務。

    # 進入Nginx服務的可執行目錄(樣本目錄,請以實際路徑為準)。
    cd /usr/local/nginx/sbin
    # 重新載入設定檔。
    ./nginx -s reload