日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nginx实战五

發布時間:2023/12/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx实战五 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?nginx用戶認證

https://coding.net/u/aminglinux/p/nginx/git/blob/master/access/auth.md

當訪問一些私密資源時,最好配置用戶認證,增加安全性。

安裝httpd

yum install -y httpd

使用htpasswd生產密碼文件

htpasswd -c /usr/local/nginx/conf/htpasswd aming

配置nginx用戶認證

location /admin/{auth_basic "Auth";auth_basic_user_file /usr/local/nginx/conf/htpasswd;}

測試

curl -uaming:passwd www.aminglinux.com/admin/1.html 

實驗:

1.配置1.conf文件

[root@centos-03 vhost]# vim 1.conf [root@centos-03 vhost]# cat 1.conf server {listen 80;server_name www.1.com;index index.html;root /data/wwwroot/www.1.com;access_log /tmp/1.log;location /admin/{auth_basic "Auth";auth_basic_user_file /usr/local/nginx/conf/htpasswd;} } [root@centos-03 vhost]#

2.查看是否有htpasswd命令,沒有需要安裝httpd

3.創建user1

[root@centos-03 vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd user1 New password: Re-type new password: Adding password for user user1 [root@centos-03 vhost]# cat /usr/local/nginx/conf/htpasswd user1:$apr1$uxh3ptes$Yd7avMFbzdq6sDHTw2i8a0 [root@centos-03 vhost]#

4.創建user2

[root@centos-03 vhost]# htpasswd /usr/local/nginx/conf/htpasswd user2 New password: Re-type new password: Adding password for user user2 [root@centos-03 vhost]# cat /usr/local/nginx/conf/htpasswd user1:$apr1$uxh3ptes$Yd7avMFbzdq6sDHTw2i8a0 user2:$apr1$5sdndSVS$Cm2qbv2SUeHMjD1U9TgPO/ [root@centos-03 vhost]#

測試:

[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com/admin/adf <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.14.0</center> </body> </html> [root@centos-03 vhost]#

5.curl 輸入用戶名和密碼

[root@centos-03 vhost]# curl -x127.0.0.1:80 -uuser1:user1 www.1.com/admin/adf <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.14.0</center> </body> </html> [root@centos-03 vhost]#

什么是ca證書

https://coding.net/u/aminglinux/p/nginx/git/blob/master/ssl/ca.md

先來一個例子
A公司的小明被派到B公司辦事情。B公司如何信任小明是A公司派來的呢?

普通介紹信

為了讓B公司信任小明,A公司特意給小明開了一封介紹信,在信件中詳細說明了小明的特征以及小明過來的目的, 并且聲明這個小明確實是A公司派來的,除此之外還要有一個A公司的公章。 這樣B公司前臺小姐姐拿到介紹信后,通過信件內容和A公司公章就能判斷出小明確實是A公司派來的員工。 那萬一A公司公章是假的呢?畢竟公章偽造太容易了,這樣豈不是會存在問題。咱們就暫且認為公章這種東西很難偽造, 否則故事無法繼續嘍。

引入第三方中介公司

好,回到剛才的話題。如果和B公司有業務往來的公司很多,每個公司的公章都不同,那B公司的前臺小姐姐就要懂得分辨各種公章, 非常滴麻煩。所以,有某個中介公司C,發現了這個商機。C公司專門開設了一項“代理公章”的業務。于是今后,A公司的業務員去B公司,需要帶2個介紹信: 介紹信1(含有C公司的公章及A公司的公章。并且特地注明:C公司信任A公司。) 介紹信2(僅含有A公司的公章,然后寫上:茲有xxx先生/女士前往貴公司辦理業務,請給予接洽......。)這樣不是增加麻煩了嗎?有啥好處呢? 主要的好處在于,對于接待公司的前臺,就不需要記住各個公司的公章分別是啥樣子的,她只要記住中介公司C的公章即可。 當她拿到兩份介紹信之后,先對介紹信1的C公章驗明正身,確認無誤之后,再比對“介紹信1”和“介紹信2”的兩個A公章是否一致。 如果是一樣的,那就可以證明“介紹信2”是可以信任的了。
相關專業術語的解釋
下面就著上面的例子,把相關的名詞,作一些解釋。

什么是證書?

證書,洋文也叫“digital certificate”或“public key certificate”。 它是用來證明某某東西確實是某某的東西,通俗地說,證書就好比例子里面的公章。通過公章, 可以證明該介紹信確實是對應的公司發出的。 理論上,人人都可以找個證書工具,自己做一個證書。那如何防止壞人自己制作證書出來騙人呢?

什么是CA?

CA 是“Certificate Authority”的縮寫,也叫“證書授權中心”。它是負責管理和簽發證書的第三方機構, 就好比例子里面的中介C公司。 一般來說,CA必須是所有行業和所有公眾都信任的、認可的。因此它必須具有足夠的權威性。 就好比A、B兩公司都必須信任C公司,才會找C公司作為公章的中介。

什么是CA證書?

CA證書,顧名思義,就是CA頒發的證書。前面已經說了,人人都可以找工具制作證書。但是你一個小破孩制作出來的證書是沒啥用處的。 因為你不是權威的CA機關,你自己搞的證書不具有權威性。 這就好比上述的例子里,某個壞人自己刻了一個公章,蓋到介紹信上。但是別人一看, 不是受信任的中介公司的公章,就不予理睬。

什么是證書之間的信任關系?

在開篇的例子里談到,引入中介后,業務員要同時帶兩個介紹信。第一個介紹信包含了兩個公章,并注明,公章C信任公章A。 證書間的信任關系,就和這個類似。就是用一個證書來證明另一個證書是真實可信滴。

什么是證書信任鏈?

實際上,證書之間的信任關系,是可以嵌套的。 比如,C信任A1,A1信任A2,A2信任A3......這個叫做證書的信任鏈。 只要你信任鏈上的頭一個證書,那后續的證書,都是可以信任滴。

什么是根證書?

根證書的洋文叫“root certificate”,為了說清楚根證書是咋回事,再來看個稍微復雜點的例子。 假設C證書信任A和B;然后A信任A1和A2;B信任B1和B2。則它們之間,構成如下的一個樹形關系(一個倒立的樹)。

處于最頂上的樹根位置的那個證書,就是“根證書”。除了根證書,其它證書都要依靠上一級的證書,來證明自己。 那誰來證明“根證書”可靠呢? 實際上,根證書自己證明自己是可靠滴(或者換句話說,根證書是不需要被證明滴)。 聰明的同學此刻應該意識到了:根證書是整個證書體系安全的根本。 所以,如果某個證書體系中,根證書出了問題(不再可信了),那么所有被根證書所信任的其它證書,也就不再可信了。
證書有啥用?
CA證書的作用有很多,只列出常用的幾個。

驗證網站是否可信(針對HTTPS)

通常,我們如果訪問某些敏感的網頁(比如用戶登錄的頁面),其協議都會使用HTTPS而不是HTTP,因為HTTP協議是明文的, 一旦有壞人在偷窺你的網絡通訊,他/她就可以看到網絡通訊的內容(比如你的密碼、銀行帳號、等)。 而 HTTPS 是加密的協議,可以保證你的傳輸過程中,壞蛋無法偷窺。 但是,千萬不要以為,HTTPS協議有了加密,就可高枕無憂了。 假設有一個壞人,搞了一個假的網銀的站點,然后誘騙你上這個站點。 假設你又比較單純,一不留神,就把你的帳號,口令都輸入進去了。那這個壞蛋的陰謀就得逞了。 為了防止壞人這么干,HTTPS 協議除了有加密的機制,還有一套證書的機制。通過證書來確保,某個站點確實就是某個站點。 有了證書之后,當你的瀏覽器在訪問某個HTTPS網站時,會驗證該站點上的CA證書(類似于驗證介紹信的公章)。 如果瀏覽器發現該證書沒有問題(證書被某個根證書信任、證書上綁定的域名和該網站的域名一致、證書沒有過期), 那么頁面就直接打開,否則的話,瀏覽器會給出一個警告,告訴你該網站的證書存在某某問題,是否繼續訪問該站點。

驗證文件是否可信

本文參考于?https://program-think.blogspot.com/2010/02/introduce-digital-certificate-and-ca.html

SSL原理

要想弄明白SSL認證原理,首先要對CA有有所了解,它在SSL認證過程中有非常重要的作用。 說白了,CA就是一個組織,專門為網絡服務器頒發證書的,國際知名的CA機構有VeriSign、Symantec,國內的有GlobalSign。 每一家CA都有自己的根證書,用來對它所簽發過的服務器端證書進行驗證。如果服務器提供方想為自己的服務器申請證書,它就需要向CA機構提出申請。 服務器提供方向CA提供自己的身份信息,CA判明申請者的身份后,就為它分配一個公鑰, 并且CA將該公鑰和服務器身份綁定在一起,并為之簽字,這就形成了一個服務器端證書。如果一個用戶想鑒別另一個證書的真偽,他就用CA的公鑰對那個證書上的簽字進行驗證,一旦驗證通過,該證書就被認為是有效的。 證書實際是由證書簽證機關(CA)簽發的對用戶的公鑰的認證。證書的內容包括:電子簽證機關的信息、公鑰用戶信息、公鑰、權威機構的簽字和有效期等等。 目前,證書的格式和驗證方法普遍遵循X.509國際標準。

申請證書過程

首先要有一個CA根證書,然后用CA根證書來簽發用戶證書。 用戶進行證書申請: 1. 先生成一個私鑰 2. 用私鑰生成證書請求(證書請求里應含有公鑰信息) 3. 利用證書服務器的CA根證書來簽發證書這樣最終拿到一個由CA根證書簽發的證書,其實證書里僅有公鑰,而私鑰是在用戶手里的。

?SSL工作流程(單向)

1.客戶端say hello 服務端 2.服務端將證書、公鑰等發給客戶端 3.客戶端CA驗證證書,成功繼續、不成功彈出選擇頁面 4.客戶端告知服務端所支持的加密算法 5.服務端選擇最高級別加密算法明文通知客戶端 6.客戶端生成隨機對稱密鑰key,使用服務端公鑰加密發送給服務端 7.服務端使用私鑰解密,獲取對稱密鑰key 8.后續客戶端與服務端使用該密鑰key進行加密通信
SSL工作流程(雙向)

單向認證,僅僅是客戶端需要檢驗服務端證書是否是正確的,而服務端不會檢驗客戶端證書是否是正確的。 雙向認證,指客戶端驗證服務器端證書,而服務器也需要通過CA的公鑰證書來驗證客戶端證書。

雙向驗證的過程:

1.客戶端say hello 服務端 2.服務端將證書、公鑰等發給客戶端 3.客戶端CA驗證證書,成功繼續、不成功彈出選擇頁面 4.客戶端將自己的證書和公鑰發送給服務端 5.服務端驗證客戶端證書,如不通過直接斷開連接 6.客戶端告知服務端所支持的加密算法 7.服務端選擇最高級別加密算法使用客戶端公鑰加密后發送給客戶端 8.客戶端收到后使用私鑰解密并生成隨機對稱密鑰key,使用服務端公鑰加密發送給服務端 9.服務端使用私鑰解密,獲取對稱密鑰key 10.后續客戶端與服務端使用該密鑰key進行加密通信

?自制CA證書

https://coding.net/u/aminglinux/p/nginx/git/blob/master/ssl/key.md

生成CA根證書
# mkdir /etc/pki/ca_test //創建CA更證書的目錄# cd /etc/pki/ca_test# mkdir root server client newcerts //創建幾個相關的目錄# echo 01 > serial //定義序列號為01# echo 01 > crlnumber //定義crl號為01# touch index.txt //創建index.txt# cd ..# vi tls/openssl.cnf //改配置文件 default_ca = CA_default 改為 default_ca = CA_test [ CA_default ] 改為 [ CA_test ] dir = /etc/pki/CA 改為 dir = /etc/pki/ca_test certificate = $dir/cacert.pem 改為 certificate = $dir/root/ca.crt private_key = $dir/private/cakey.pe 改為 private_key = $dir/root/ca.key# openssl genrsa -out /etc/pki/ca_test/root/ca.key //生成私鑰# openssl req -new -key /etc/pki/ca_test/root/ca.key -out /etc/pki/ca_test/root/ca.csr //生成請求文件,會讓我們填寫一些指標,這里要注意:如果在這一步填寫了相應的指標, 比如Country Name、State or Province Name、hostname。# openssl x509 -req -days 3650 -in /etc/pki/ca_test/root/ca.csr -signkey /etc/pki/ca_test/root/ca.key -out /etc/pki/ca_test/root/ca.crt //生成crt文件
生成server端證書
# cd /etc/pki/ca_test/server# openssl genrsa -out server.key //生成私鑰文件# openssl req -new -key server.key -out server.csr//生成證書請求文件,填寫信息需要和ca.csr中的Organization Name保持一致# openssl ca -in server.csr -cert /etc/pki/ca_test/root/ca.crt -keyfile /etc/pki/ca_test/root/ca.key -out server.crt -days 3650 //用根證書簽名server.csr,最后生成公鑰文件server.crt,此步驟會有兩個地方需要輸入y Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y

?生成客戶端證書

如果做ssl的雙向認證,還需要給客戶端生成一個證書,步驟和上面的基本一致 # cd /etc/pki/ca_test/client# openssl genrsa -out client.key //生成私鑰文件# openssl req -new -key client.key -out client.csr //生成請求文件,填寫信息需要和ca.csr中的Organization Name保持一致# openssl ca -in client.csr -cert /etc/pki/ca_test/root/ca.crt -keyfile /etc/pki/ca_test/root/ca.key -out client.crt -days 3650 //簽名client.csr, 生成client.crt,此步如果出現 failed to update database TXT_DB error number 2需執行: # sed -i 's/unique_subject = yes/unique_subject = no/' /etc/pki/ca_test/index.txt.attr執行完,再次重復執行簽名client.csr那個操作 

?實驗:

1.創建目錄

[root@centos-03 ~]# mkdir /etc/pki/ca_test [root@centos-03 ~]# cd /etc/pki/ca_test/ [root@centos-03 ca_test]# mkdir root server client newcerts [root@centos-03 ca_test]# [root@centos-03 ca_test]# echo 01 > serial [root@centos-03 ca_test]# echo 01 > crlnumber [root@centos-03 ca_test]# touch index.txt [root@centos-03 ca_test]# [root@centos-03 pki]# vim tls/openssl.cnf [ ca ] #default_ca = CA_default # The default ca section default_ca = CA_test # The default ca section #################################################################### [ CA_test ]dir = /etc/pki/ca_test # Where everything is kept certs = $dir/certs # Where the issued certs are kept crl_dir = $dir/crl # Where the issued crl are kept database = $dir/index.txt # database index file. #unique_subject = no # Set to 'no' to allow creation of# several ctificates with same subject. new_certs_dir = $dir/newcerts # default place for new certs.certificate = $dir/root/ca.crt # The CA certificate serial = $dir/serial # The current serial number crlnumber = $dir/crlnumber # the current crl number# must be commented out to leave a V1 CR L crl = $dir/crl.pem # The current CRL private_key = $dir/root/ca.key # The private key RANDFILE = $dir/private/.rand # private random number file

2.生成私鑰

[root@centos-03 pki]# openssl genrsa -out /etc/pki/ca_test/root/ca.key Generating RSA private key, 2048 bit long modulus ..................+++ ..........................+++ e is 65537 (0x10001) [root@centos-03 pki]#

3.生成請求文件,會讓我們填寫一些指標,這里要注意:如果在這一步填寫了相應的指標, 比如Country Name、State or Province Name、hostname。

[root@centos-03 pki]# openssl req -new -key /etc/pki/ca_test/root/ca.key -out /etc/pki/ca_test/root/ca.csr 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) [XX]: State or Province Name (full name) []:bj Locality Name (eg, city) [Default City]:bj Organization Name (eg, company) [Default Company Ltd]:aming Organizational Unit Name (eg, section) []:test Common Name (eg, your name or your server's hostname) []:aming.com Email Address []:aming@aming.comPlease enter the following 'extra' attributes to be sent with your certificate request A challenge password []:123456 An optional company name []:aminglinux.com [root@centos-03 pki]#

4.生成crt文件

[root@centos-03 pki]# openssl x509 -req -days 3650 -in /etc/pki/ca_test/root/ca.csr -signkey /etc/pki/ca_test/root/ca.key -out /etc/pki/ca_test/root/ca.crt Signature ok subject=/C=XX/ST=bj/L=bj/O=aming/OU=test/CN=aming.com/emailAddress=aming@aming.com Getting Private key [root@centos-03 pki]#

5.我們在root下生成了三個文件

[root@centos-03 pki]# ls -l ca_test/root/ 總用量 12 -rw-r--r-- 1 root root 1253 7月 28 11:19 ca.crt -rw-r--r-- 1 root root 1102 7月 28 11:17 ca.csr -rw-r--r-- 1 root root 1679 7月 28 11:14 ca.key [root@centos-03 pki]#
生成server端證書
[root@centos-03 pki]# cd /etc/pki/ca_test/server [root@centos-03 server]#

1.生成私鑰文件

[root@centos-03 server]# openssl genrsa -out server.key Generating RSA private key, 2048 bit long modulus ...........+++ ....+++ e is 65537 (0x10001) [root@centos-03 server]#

2.生成證書請求文件,填寫信息需要和ca.csr中的Organization Name保持一致

[root@centos-03 server]# openssl req -new -key server.key -out server.csr 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) [XX]: State or Province Name (full name) []:bj Locality Name (eg, city) [Default City]:bj Organization Name (eg, company) [Default Company Ltd]:aming Organizational Unit Name (eg, section) []:test2 Common Name (eg, your name or your server's hostname) []:123.com Email Address []:admin@123.comPlease enter the following 'extra' attributes to be sent with your certificate request A challenge password []:123456 An optional company name []:123.com [root@centos-03 server]#

3.用根證書簽名server.csr,最后生成公鑰文件server.crt,此步驟會有兩個地方需要輸入y

[root@centos-03 server]# openssl ca -in server.csr -cert /etc/pki/ca_test/root/ca.crt -keyfile /etc/pki/ca_test/root/ca.key -out server.crt -days 3650 Using configuration from /etc/pki/tls/openssl.cnf Check that the request matches the signature Signature ok Certificate Details:Serial Number: 1 (0x1)ValidityNot Before: Jul 28 03:27:13 2018 GMTNot After : Jul 25 03:27:13 2028 GMTSubject:countryName = XXstateOrProvinceName = bjorganizationName = amingorganizationalUnitName = test2commonName = 123.comemailAddress = admin@123.comX509v3 extensions:X509v3 Basic Constraints: CA:FALSENetscape Comment: OpenSSL Generated CertificateX509v3 Subject Key Identifier: C6:56:E8:E3:35:40:FA:F3:46:A7:EE:37:F6:02:E1:F5:29:6E:32:8BX509v3 Authority Key Identifier: DirName:/C=XX/ST=bj/L=bj/O=aming/OU=test/CN=aming.com/emailAddress=aming@aming.comserial:A9:31:0D:12:47:56:CE:81Certificate is to be certified until Jul 25 03:27:13 2028 GMT (3650 days) Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated [root@centos-03 server]#

4.crt文件生成成功

[root@centos-03 server]# ls -l 總用量 16 -rw-r--r-- 1 root root 4769 7月 28 11:27 server.crt -rw-r--r-- 1 root root 1090 7月 28 11:26 server.csr -rw-r--r-- 1 root root 1679 7月 28 11:23 server.key [root@centos-03 server]#
生成客戶端證書

如果做ssl的雙向認證,還需要給客戶端生成一個證書,步驟和上面的基本一致

[root@centos-03 server]# cd /etc/pki/ca_test/client [root@centos-03 client]#

1.生成私鑰文件

[root@centos-03 client]# openssl genrsa -out client.key Generating RSA private key, 2048 bit long modulus .................................................................................+++ ........+++ e is 65537 (0x10001) [root@centos-03 client]#

2.生成請求文件,填寫信息需要和ca.csr中的Organization Name保持一致

[root@centos-03 client]# openssl req -new -key client.key -out client.csr 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) [XX]: State or Province Name (full name) []:bj Locality Name (eg, city) [Default City]:bj Organization Name (eg, company) [Default Company Ltd]:aming Organizational Unit Name (eg, section) []:test3 Common Name (eg, your name or your server's hostname) []:aaa.com Email Address []:ad@aaa.comPlease enter the following 'extra' attributes to be sent with your certificate request A challenge password []:123456 An optional company name []:aaa.com [root@centos-03 client]#

3.簽名client.csr, 生成client.crt

[root@centos-03 client]# openssl ca -in client.csr -cert /etc/pki/ca_test/root/ca.crt -keyfile /etc/pki/ca_test/root/ca.key -out client.crt -days 3650 Using configuration from /etc/pki/tls/openssl.cnf Check that the request matches the signature Signature ok Certificate Details:Serial Number: 2 (0x2)ValidityNot Before: Jul 28 03:42:18 2018 GMTNot After : Jul 25 03:42:18 2028 GMTSubject:countryName = XXstateOrProvinceName = bjorganizationName = amingorganizationalUnitName = test3commonName = aaa.comemailAddress = ad@aaa.comX509v3 extensions:X509v3 Basic Constraints: CA:FALSENetscape Comment: OpenSSL Generated CertificateX509v3 Subject Key Identifier: C0:B7:1E:6A:D2:AE:2D:95:C0:D8:D4:AE:49:07:C5:81:0D:98:C9:FFX509v3 Authority Key Identifier: DirName:/C=XX/ST=bj/L=bj/O=aming/OU=test/CN=aming.com/emailAddress=aming@aming.comserial:A9:31:0D:12:47:56:CE:81Certificate is to be certified until Jul 25 03:42:18 2028 GMT (3650 days) Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated [root@centos-03 client]#

4.生成了crt和key

[root@centos-03 client]# ls -l 總用量 16 -rw-r--r-- 1 root root 4762 7月 28 11:42 client.crt -rw-r--r-- 1 root root 1086 7月 28 11:40 client.csr -rw-r--r-- 1 root root 1675 7月 28 11:38 client.key [root@centos-03 client]#

配置Nginx單向SSL

https://coding.net/u/aminglinux/p/nginx/git/blob/master/ssl/nginx.md

Nginx配置示例(單向)
cp /etc/pki/ca_test/server/server.* /usr/local/nginx/conf/ {listen 443 ssl;server_name www.aminglinux.com;index index.html index.php;root /data/wwwroot/aminglinux.com;ssl on;ssl_certificate server.crt;ssl_certificate_key server.key;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!eNULL;ssl_prefer_server_ciphers on;... }
配置說明
1. 443端口為ssl監聽端口。 2. ssl on表示打開ssl支持。 3. ssl_certificate指定crt文件所在路徑,如果寫相對路徑,必須把該文件和nginx.conf文件放到一個目錄下。 4. ssl_certificate_key指定key文件所在路徑。 5. ssl_protocols指定SSL協議。 6. ssl_ciphers配置ssl加密算法,多個算法用:分隔,ALL表示全部算法,!表示不啟用該算法,+表示將該算法排到最后面去。 7. ssl_prefer_server_ciphers 如果不指定默認為off,當為on時,在使用SSLv3和TLS協議時,服務器加密算法將優于客戶端加密算法。
Nginx配置雙向認證
cp /etc/pki/ca_test/root/ca.crt /usr/local/nginx/conf/ 配置示例: {listen 443 ssl;server_name www.aminglinux.com;index index.html index.php;root /data/wwwroot/aminglinux.com;ssl on;ssl_certificate server.crt;ssl_certificate_key server.key;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!eNULL;ssl_prefer_server_ciphers on;ssl_client_certificate ca.crt; //這里的ca.crt是根證書公鑰文件ssl_verify_client on;... }
客戶端(瀏覽器)操作
如果不進行以下操作,瀏覽器會出現400錯誤。400 Bad Request(No required SSL certificate was sent) 首先需要將client.key轉換為pfx(p12)格式# cd /etc/pki/ca_test/client # openssl pkcs12 -export -inkey client.key -in client.crt -out client.pfx //這一步需要輸入一個自定義密碼,一會在windows上安裝的時候要用到,需要記一下。然后將client.pfx拷貝到windows下,雙擊即可安裝。也可以直接curl測試: curl -k --cert /etc/pki/ca_test/client/client.crt --key /etc/pki/ca_test/client/client.key https://www.aminglinux.com/index.html

實驗

1.查看是否安裝了http_ssl_module模塊

[root@centos-03 client]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.14.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) configure arguments: --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-module/ [root@centos-03 client]# [root@centos-03 nginx-1.14.0]# ./configure --help |grep ssl--with-http_ssl_module enable ngx_http_ssl_module--with-mail_ssl_module enable ngx_mail_ssl_module--with-stream_ssl_module enable ngx_stream_ssl_module--with-stream_ssl_preread_module enable ngx_stream_ssl_preread_module--with-openssl=DIR set path to OpenSSL library sources--with-openssl-opt=OPTIONS set additional build options for OpenSSL [root@centos-03 nginx-1.14.0]# [root@centos-03 nginx-1.14.0]# ./configure --with-http_ssl_module --prefix=/usr/local/nginx

2.沒有openssl的需要安裝openssl

[root@centos-03 nginx-1.14.0]# yum install openssl openssl-devel [root@centos-03 nginx-1.14.0]# make && make install oot@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V ▽ginx version: nginx/1.14.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --with-http_ssl_module --prefix=/usr/local/nginx [root@centos-03 nginx-1.14.0]# killall nginx [root@centos-03 nginx-1.14.0]# killall nginx nginx: no process found [root@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx [root@centos-03 nginx-1.14.0]# ps aux|grep nginx root 29709 0.0 0.1 77268 1204 ? Ss 12:22 0:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 29710 0.0 0.1 77268 1968 ? S 12:22 0:00 nginx: worker process nobody 29711 0.0 0.1 77268 1724 ? S 12:22 0:00 nginx: cache manager process nobody 29712 0.0 0.1 77268 1724 ? S 12:22 0:00 nginx: cache loader process root 29715 0.0 0.0 112724 972 pts/1 S+ 12:23 0:00 grep --color=auto nginx [root@centos-03 nginx-1.14.0]#

3.配置ssl

[root@centos-03 nginx-1.14.0]# cd /usr/local/nginx/conf/vhost/ [root@centos-03 vhost]# ls 1.conf default_8080.conf fp.conf zp.conf 2.conf default.conf.bak test.com.conf [root@centos-03 vhost]# vim 1.conf [root@centos-03 vhost]# cat 1.conf server {listen 443 ssl;server_name www.123.com;index index.html;root /data/wwwroot/www.1.com;ssl on;ssl_certificate /etc/pki/ca_test/server/server.crt;ssl_certificate_key /etc/pki/ca_test/server/server.key;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!eNULL;ssl_prefer_server_ciphers on; } [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload

4.Windows做下host綁定1.com ,服務器也綁定host

192.168.242.133 www.1.com www.123.com

?

?配置Nginx雙向SSL

1.在單向ssl基礎之上再增加兩行

[root@centos-03 vhost]# vim 1.conf ^C [root@centos-03 vhost]# cat 1.conf server {listen 443 ssl;server_name www.123.com;index index.html;root /data/wwwroot/www.1.com;ssl on;ssl_certificate /etc/pki/ca_test/server/server.crt;ssl_certificate_key /etc/pki/ca_test/server/server.key;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!eNULL;ssl_prefer_server_ciphers on;ssl_client_certificate /etc/pki/ca_test/root/ca.crt;ssl_verify_client on; } [root@centos-03 vhost]#

?

[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]#

2.瀏覽器訪問報400錯誤,這是因為雙向驗證瀏覽器需要有一個key文件(公鑰文件)

3.轉換瀏覽器能用的key文件格式

[root@centos-03 vhost]# cd /etc/pki/ca_test/client [root@centos-03 client]# openssl pkcs12 -export -inkey client.key -in client.crt -out client.pfx Enter Export Password: Verifying - Enter Export Password: [root@centos-03 client]#

4.下載文件到Windows下

[root@centos-03 client]# ls client.crt client.csr client.key client.pfx [root@centos-03 client]# sz client.pfx rz Starting zmodem transfer. Press Ctrl+C to cancel.100% 2 KB 2 KB/s 00:00:01 0 Errors[root@centos-03 client]#

5.下載完成后雙擊文件導入

6.再次訪問

7.用curl訪問測試

[root@centos-03 client]# curl -k --cert /etc/pki/ca_test/client/client.crt --key /etc/pki/ca_test/client/client.key https://www.123.com www.1.com [root@centos-03 client]#

?

轉載于:https://www.cnblogs.com/sunyujun/p/9443867.html

總結

以上是生活随笔為你收集整理的nginx实战五的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。