nginx生成自簽名SSL證書配置HTTPS的實(shí)現(xiàn)
一、安裝nginx
nginx必須有"--with-http_ssl_module"模塊 查看nginx安裝的模塊: root@ecs-7398:/usr/local/nginx# cd /usr/local/nginx/ root@ecs-7398:/usr/local/nginx# ./sbin/nginx -V nginx version: nginx/1.20.2 built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2) built with OpenSSL 1.1.1f 31 Mar 2020 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_gzip_static_module --with-http_v2_module --with-http_realip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module
二、創(chuàng)建證書
1、生成私鑰
root@ecs-7398:~# cd /usr/local/nginx/ root@ecs-7398:/usr/local/nginx# mkdir key root@ecs-7398:/usr/local/nginx# cd key/ root@ecs-7398:/usr/local/nginx/key# openssl genrsa -des3 -out server.key 2048 #使用ssl生成私鑰名為 server.key Generating RSA private key, 2048 bit long modulus (2 primes) ................+++++ ......+++++ e is 65537 (0x010001) Enter pass phrase for server.key: #自定義密碼:123456 Verifying - Enter pass phrase for server.key: #確認(rèn)密碼:123456 root@ecs-7398:/usr/local/nginx# ls client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp server.key uwsgi_temp root@ecs-7398:/usr/local/nginx/key# cat server.key #查看私鑰內(nèi)容 -----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,4103533ED9B6ECD1 MAhsh46L3TsiCymB5pTA93lw/WZKzX/9iuzgM/OgG7U0cKHWdiLf907/52Ocp80e bY/FKTADBcrEv2uFuke28WjdN2aQddiJGsP0CDLVKfv/kqEgvYuy2sIcoXcHV8fL 70vfaFuTa5CwxyIbRvfHFSpj39oC76eitx120x+KCkgWDkIaVGG9cP0TfmDnDOSe fmpmZhqkkkP5dXuuPNItfumHHhZpjXqMr9oGxtENdMyNBrRywC8+NhRhO7iomZeP tHiQpjQCrD8xkKcYqfKVCOS8KCXeKF1EylbJ89e2ZqgaujuKyC90raHpwga9MUSO HNOT/U85zwsmqkh4/2Ox7AVLlNiG0+Rxt+IfWJb6xgT21SEfL/2vskNAkj2PN3J+ mpeSvpaKI1BsZ8LrpsqFNR0fDhIg+a5hzfSTlWouZcpePx7vB5qvKAvoSKrGmbDO GQp4H24cSPAaQI6Wih+AxB8stfTCsBatJ5RwXgYNskumHL8KzpC9/Yj7QrLx3m3I TBDlpOVU6tUYzMDVYDMGtTUhoPIdfVjaRz8BGWUFp0MM3Sx+rppPul1voSuVve5T 8uba4fqv+KIEQdR/PELB4N+ZgZiFP5HtoZN7mFWN6H/Ygm3GEgNeljiqypYQpZOd dUIC/vhRsCuylww7Rh8LUtgnVAkJbyuqjA38wypATLKQFI1rwFzI9gCWwyz0SCNQ tffBpZebLkG+H7GGfrTo+50TLDVetyQctbj2ibytpVKK4xE7oaMSZYqbfqg6OYCp k2LhlWkKsDf7XhLbo5kP2UUfB7LSzx3JdRmA0Fw3GqEevFJysyJO2w== -----END RSA PRIVATE KEY----- root@ecs-7398:/usr/local/nginx/key#
2、生成公鑰
root@ecs-7398:/usr/local/nginx/key# openssl req -new -key server.key -out server.csr #基于創(chuàng)建的server.key私鑰創(chuàng)建server.csr公鑰 Enter pass phrase for server.key: #輸入server.key的密碼:123456 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) [AU]:CN #國(guó)家 State or Province Name (full name) [Some-State]:shanghai #省市 Locality Name (eg, city) []:jiading #城市 Organization Name (eg, company) [Internet Widgits Pty Ltd]:bai #組織 Organizational Unit Name (eg, section) []:zr #單位 Common Name (e.g. server FQDN or YOUR name) []:byc #姓名 Email Address []:2123288207@qq.com #郵箱 Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:123456 #密碼 An optional company name []:zr #公司 root@ecs-7398:/usr/local/nginx/key#
3、簽名生成證書
root@ecs-7398:/usr/local/nginx/key# openssl rsa -in server.key -out server.key #去除server.key認(rèn)證,避免每次"nginx -t"時(shí)出現(xiàn)輸入密碼的情況 Enter pass phrase for server.key: #密碼:123456 writing RSA key root@ecs-7398:/usr/local/nginx/key# root@ecs-7398:/usr/local/nginx/key# openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt #使用私鑰和公鑰生成server.crt簽名證書,-days為3650天 -in指定公鑰,-signkey指定私鑰,生成的前面證書為server.crt Signature ok subject=C = CN, ST = shanghai, L = jiading, O = bai, OU = zr, CN = byc, emailAddress = 2123288207@qq.com Getting Private key root@ecs-7398:/usr/local/nginx/key#
三、配置證書并驗(yàn)證
root@ecs-7398:/usr/local/nginx/key# cd .. root@ecs-7398:/usr/local/nginx# systemctl start nginx #啟動(dòng)Nginx root@ecs-7398:/usr/local/nginx# vim conf/nginx.conf #編輯nginx主配置文件將后面server的注釋去掉
server { listen 443 ssl; server_name localhost; ssl_certificate /usr/local/nginx/key/server.crt; ##證書路徑 ssl_certificate_key /usr/local/nginx/key/server.key; ##證書路徑 ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root /usr/local/nginx/html/xiaomi; index index.html index.htm; } }
四、測(cè)試
root@ecs-7398:/usr/local/nginx# cd root@ecs-7398:~# ls nginx-1.20.2 nginx-1.20.2.tar.gz 小米官網(wǎng).zip root@ecs-7398:~# unzip 小米官網(wǎng).zip -d /usr/local/nginx/html/xiaomi root@ecs-7398:~# ls /usr/local/nginx/html/xiaomi/ css iconfont images index.html
在瀏覽器訪問https//xxx.xxx.xxx.xxx:443
到此這篇關(guān)于nginx生成自簽名SSL證書配置HTTPS的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)nginx生成自簽名SSL證書內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx?添加http_stub_status_module模塊
本文主要介紹了nginx?添加http_stub_status_module模塊,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Nginx隱藏server頭信息的實(shí)現(xiàn)
本文主要介紹了Nginx隱藏server頭信息的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01ConfigMap掛載與Subpath在Nginx容器中的應(yīng)用小結(jié)
configmap可以通過ENV環(huán)境變量和文件兩種方式掛載到容器中,修改configmap后容器中對(duì)應(yīng)的ENV環(huán)境變量不會(huì)更新,將配置文件nginx.conf以configmap文件的方式掛載到容器中,本文介紹ConfigMap掛載與Subpath在Nginx容器中的應(yīng)用小結(jié),感興趣的朋友一起看看吧2024-03-03Nginx實(shí)現(xiàn)if多重判斷配置方法示例
這篇文章主要介紹了Nginx實(shí)現(xiàn)if多重判斷配置方法示例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05利用nginx實(shí)現(xiàn)動(dòng)靜分離的負(fù)載均衡集群實(shí)戰(zhàn)教程
這篇文章介紹了利用nginx實(shí)現(xiàn)動(dòng)靜分離的負(fù)載均衡集群實(shí)戰(zhàn),本次用到的操作系統(tǒng)及服務(wù),本次實(shí)驗(yàn)一共需要3臺(tái)服務(wù)器,一臺(tái)nginx做為負(fù)載均衡分發(fā)器和動(dòng)靜分離的分發(fā)器,兩臺(tái)apache做為后端服務(wù)器,使用nginx實(shí)現(xiàn)兩臺(tái)apache服務(wù)器的負(fù)載均衡和動(dòng)靜分離,需要的朋友可以參考下2023-03-03