引子
? ? ? ? 為了提高web應(yīng)用的安全性,現(xiàn)在基本上都需要支持https訪(fǎng)問(wèn)姆坚。在此記錄一下自己在nginx下的配置過(guò)程屯掖。
準(zhǔn)備安裝包
? ? ? ? ?將nginx-1.12.1.tar.gz、openssl-1.0.0e.tar.gz拷貝到 /root/ 下(如果文件已經(jīng)存在纬傲,則不需要上傳)满败。
? ? ? 安裝NGINX+ssl模塊
進(jìn)入到root目錄下:cd/root/nginx-1.12.1
配置NGINX參數(shù) ?:./configure ?--prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module ?--with-openssl=/root/openssl-1.0.0
make (耐心等待)
make install
補(bǔ)充:若已安裝nginx但沒(méi)有安裝ssl模塊,按一下步驟安裝
進(jìn)入安裝nginx/sbin,使用命令:./nginx -V 查看已配置的參數(shù)叹括,例如
configure arguments: --prefix=/usr/local/nginx ?--add-module=/root/soft/ngx_devel_kit-0.3.0 --add-module=/root/soft/lua-nginx-module-0.10.9rc7
記下這里的配置參數(shù)葫录,在下面的配置中需要用到。同時(shí)備份nginx ? ? mv nginx nginx.bak
進(jìn)入到nginx源碼目錄下领猾,配置nginx米同,需要把步驟1中的配置參數(shù)加上(若不加會(huì)影響原有的功能)
./configure ?--prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module ?--with-openssl=/root/openssl-1.0.0 ?--add-module=/root/soft/ngx_devel_kit-0.3.0 --add-module=/root/soft/lua-nginx-module-0.10.9rc7
make
進(jìn)入到objs下:cd objs,拷貝nginx到安裝目錄下
cp nginx /usr/local/nginx/sbin
啟動(dòng)nginx摔竿,查看nginx模塊面粮,發(fā)現(xiàn)已經(jīng)添加
/usr/local/nginx/sbin/nginx -V
? ? 至此,nginx支持ssl模塊安裝完畢继低!
?安裝證書(shū)
? ? ?nginx支持https協(xié)議需要服務(wù)器證書(shū)熬苍,此證書(shū)使用openssl命令生成(確保openssl命令可用)
? ? ?證書(shū)生成步驟如下:?
? ? ? ?1.進(jìn)入到/usr/local/nginx/conf/下,新建目錄crt(mkdir crt)
? ? ? ?2.進(jìn)入到crt(cd crt)
3.開(kāi)始生成證書(shū),使用命令:openssl genrsa -des3 -out server.key 1024 生成key柴底,會(huì)出現(xiàn)以下提示? ? ? ? ?
? ? ? ? ? ? ? ? ? Generating RSA private key, 1024 bit long modulus
? ? ? ? ? ? ? ? ? ......................................................++++++
? ? ? ? ? ? ? ? ? .................++++++
? ? ? ? ? ? ? ? ? e is 65537 (0x10001)
Enter pass phrase for server.key:(此處隨意輸入證書(shū)密碼開(kāi)心就行婿脸,比如123456)
Verifying - Enter pass phrase for server.key:(重復(fù)輸入一次)
? ? ? ??4.使用命令openssl req -new -key server.key -out server.csr 生成csr,(注:此步驟生成證書(shū)柄驻,需要輸入國(guó)家/地區(qū)/公司/個(gè)人相關(guān)信息狐树,不需要真實(shí),內(nèi)容差不多就行鸿脓,可參考下面的加粗部分)
? ? ? ? ? ? ? ? Enter pass phrase for server.key:
? ? ? ? ? ? ? ? You are about to be asked to enter information that will be incorporated
? ? ? ? ? ? ? ? into your certificate request.
? ? ? ? ? ? ? ? What you are about to enter is what is called a Distinguished Name or a DN.
? ? ? ? ? ? ? ? There are quite a few fields but you can leave some blank
? ? ? ? ? ? ? ? For some fields there will be a default value,
? ? ? ? ? ? ? ? If you enter '.', the field will be left blank.
? ? ? ? ? ? ? ? -----
Country Name (2 letter code) [GB]:CN
? ? ? ? ? ? ? ? State or Province Name (full name) [Berkshire]:Shandong
Locality Name (eg, city) [Newbury]:liangshang
Organization Name (eg, company) [My Company Ltd]:hahah
Organizational Unit Name (eg, section) []:biubiu
Common Name (eg, your name or your server's hostname) []:nanxiaoliu
Email Address []:nanxiaoliu@channelsoft.com
? ? ? ? ? ? ? ? Please enter the following 'extra' attributes
? ? ? ? ? ? ? ? to be sent with your certificate request
A challenge password []:123456
? ? ? ? ? ? ? ? An optional company name []:?(敲回車(chē))
? ? ? ??5.cp server.keyserver.key.org
? ? ? ??6.openssl rsa -inserver.key.org-out server.key
? ? ? ? ? ? ?Enter pass phrase forserver.key.org:123456
? ? ? ? ? ? ?writing RSA key
7.openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
? ? ? ? ? ? Signature ok
? ? ? ? 到此抑钟,證書(shū)創(chuàng)建完畢。
?修改nginx配置https
進(jìn)入到/usr/local/nginx/conf
vim nginx.conf 新增server節(jié)點(diǎn)野哭,配置如下:
server {
server_name localhost;
listen 443 ssl;
#ssl on;
ssl_certificate /usr/local/nginx/conf/crt/server.crt;
ssl_certificate_key /usr/local/nginx/conf/crt/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
#其它的一些配置
}
重啟nginx驗(yàn)證:/usr/local/nginx/sbin/nginx -s reload
打開(kāi)瀏覽器驗(yàn)證:https://10.130.29.7/index.html?
在驗(yàn)證原有的http是否支持:http://10.130.29.7/index.html
若都能訪(fǎng)問(wèn)在塔,配置完收工。