1. 程式人生 > 實用技巧 >原始碼安裝nginx開啟SSL功能

原始碼安裝nginx開啟SSL功能

編譯安裝nginx的環境

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

下載nginx安裝包

cd /usr/local/src
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar zxvf nginx-1.18.0.tar.gz

編譯安裝nginx

cd nginx-1.18.0
# 配置
./configure --prefix=/usr/local/nginx --with-http_ssl_module
# 編譯,安裝
make && make install
# 查詢是否安裝成功
/usr/local/nginx/sbin/nginx -V

# 出現這個安裝成功出現
nginx version: nginx/1.18.0
built by gcc 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module

配置nginx.conf

# http自動跳轉到https
server {
 listen 80;
 server_name localhost;   #將localhost修改為您證書繫結的域名,例如:www.example.com。
rewrite ^(.*)$ https://$host$1 permanent;   #將所有http請求通過rewrite重定向到https。
 location / {
    index index.html index.htm;
    }
}

# ssl配置
server {
    listen 443 ssl;
    server_name localhost;
    root html;
    index index.html index.htm;
    ssl_certificate cert/domain name.pem;   #將domain name.pem替換成您證書的檔名。
    ssl_certificate_key cert/domain name.key;   #將domain name.key替換成您證書的金鑰檔名。
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
    	index index.html index.htm;
    }
}

Test

# http自動跳轉到https
server {
 listen 80;
 server_name localhost;   #將localhost修改為您證書繫結的域名,例如:www.example.com。
rewrite ^(.*)$ https://$host$1 permanent;   #將所有http請求通過rewrite重定向到https。
 location / {
    index index.html index.htm;
    }
}

# ssl配置
server {
    listen 443 ssl;
    server_name localhost;
    root html;
    index index.html index.htm;
    ssl_certificate /usr/local/nginx/cert/4329893_qra.psmtech.com.cn.pem;   #將domain name.pem替換成您證書的檔名。
    ssl_certificate_key /usr/local/nginx/cert/4329893_qra.psmtech.com.cn.key;   #將domain name.key替換成您證書的金鑰檔名。
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
    	index index.html index.htm;
    }
}