首先系統要已經安裝了openssl,以下是使用openssl安裝配置單向認證的執行步驟與腳本:
#--------------------------------------------------------
# 單向認證,就是傳輸的數據加密過了,但是不會校驗客戶端的來源
# 單項SSL連接,也就是只是客戶端驗證服務器證書
#--------------------------------------------------------
#創建存儲路徑
rm -rf /usr/local/nginx/ca.1way
mkdir -p /usr/local/nginx/ca.1way/
cd /usr/local/nginx/ca.1way/
#建立服務器私鑰(過程需要輸入密碼,請記住這個密碼) 生成RSA密鑰
/usr/local/openssl/bin/openssl genrsa -des3 -out server.key 2048
#------------------------------------------------------------------
Enter pass phrase for server.key: zhoulf123
Verifying - Enter pass phrase for server.key: zhoulf123
#------------------------------------------------------------------
#生成一個證書請求
/usr/local/openssl/bin/openssl req -new -key server.key -out server.csr
#---------------------------------------------------------------------------------------------------------------
Enter pass phrase for server.key: zhoulf123
Country Name (2 letter code) [XX]: CN #國家
State or Province Name (full name) []: BEIJING #區域或是省份
Locality Name (eg, city) [Default City]: BEIJING #地區局部名字
Organization Name (eg, company) [Default Company Ltd]: Navinfo Co.,Ltd #機構名稱:填寫公司名
Organizational Unit Name (eg, section) []: GIS #組織單位名稱:部門名稱
Common Name (eg, your name or your server's hostname) []: vw.test.zhoulf.com #網站域名
Email Address []: [email protected] #郵箱地址
A challenge password []: #輸入一個密碼
An optional company name []: #一個可選的公司名稱
#---------------------------------------------------------------------------------------------------------------
#輸入完這些內容,就會在當前目錄生成server.csr文件
cp server.key server.key.org
#對于使用上面的私鑰啟動具有SSL功能的NGINX
/usr/local/openssl/bin/openssl rsa -in server.key.org -out server.key
#---------------------------------
Enter pass phrase for server.key.org: zhoulf123
#---------------------------------
#使用上面的密鑰和CSR對證書進行簽名
/usr/local/openssl/bin/openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
修改nginx.conf文件,添加https配置內容:
#--------------------------------------------------------
# HTTPS server
server {
listen 443;
server_name localhost;
ssl on;
ssl_certificate /usr/local/nginx/ssl.ca.1way/server.crt;
ssl_certificate_key /usr/local/nginx/ssl.ca.1way/server.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
location / {
root /var/www/html;
index index.html index.htm;
}
}
#--------------------------------------------------------
#配置好后,重啟nginx,采用 https打開網站,瀏覽器會提示證書錯誤,點擊繼續瀏覽即可.