導航:首頁 > 源碼編譯 > ssl模塊編譯進nginx

ssl模塊編譯進nginx

發布時間:2023-08-17 02:02:22

A. 如何在Nginx中添加SSL證書以支持HTTPS協議訪問

獲得Nginx環境SSL證書

安裝流程:

  1. 環境檢測,檢測命令如下(測試nginx是否支持SSL)nginx-V

  2. 如果有輸入 –with-http_ssl_mole 表示已編譯openssl,支持安裝ssl

  3. 如果沒有安裝請下載nginx源碼重新編譯:./configure--with-http_stub_status_mole--with-http_ssl_mole
    make&&makeinstall

  4. 配置Nginx
    server{
    listen80;
    listen443ssl;
    server_namewww.gworg.cn;
    ssl_protocolsTLSv1.2TLSv1.1TLSv1;
    ssl_certificate/etc/ssl/yourdomain.com.crt;
    ssl_certificate_key/etc/ssl/yourdomain.com.key;
    ssl_prefer_server_cipherson;
    #自動跳轉到HTTPS(可選)
    if($server_port=80){
    rewrite^(.*)$https://$host$1permanent;
    }
    location/{
    root/home/web/;
    indexindex.php;
    }
    }

  5. 以上配置僅供參考,其他參數請根據生產環境需要添加。

  6. 安裝後重啟nginx使其生

解決辦法:SSL證書可以在Gworg申請,然後根據以上文檔安裝,Nginx證書分為:crt公鑰與key私鑰2個文件。

B. 怎麼編譯安裝nginx1.8.1

要編譯安裝Nginx,首先我們要安裝依賴包 pcre-devel 和 zlib-devel:
# yum install pcre-devel zlib-devel -y
程序默認是使用 nobody 身份運行的,我們建議使用 nginx 用戶來運行,首先添加Nginx組和用戶,不創建家目錄,不允許登陸系統
# groupadd nginx
# useradd -M -s /sbin/nologin -g nginx nginx

准備工作完成後就是下載編譯安裝Nginx了,可以從我提供的網盤下載,也可以去Nginx的官網下載。
首先解壓源碼包:
# tar xf nginx-1.4.4.tar.gz
然後 cd 到解壓後的目錄就可以執行 ./configure 了
# cd nginx-1.4.4
指定安裝目錄和運行時用的屬主和屬組,並啟用狀態監控模塊等
# ./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_mole \
--with-http_flv_mole \
--with-http_stub_status_mole \
--with-http_gzip_static_mole \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
等配置完成後就可以 make && make install 了
# make && make install
# mkdir /var/tmp/nginx/client/ -pv

等編譯安裝完成後在 /usr/local 下就會出現 Nginx 這個目錄了,進入這個目錄後發現目錄非常簡單。它的配置文件存放在 conf 目錄中,網頁文件存放在 html 中,日誌文件存放在 logs 中,sbin 目錄下只有一個可執行程序 "nginx"
接下來我們簡單的為它提供一個服務腳本吧!
# vim /etc/init.d/nginx
新建文件/etc/rc.d/init.d/nginx,內容如下:
#!/bin/bash
# chkconfig:235 85 15
# description: Nginx is an HTTP server
. /etc/rc.d/init.d/functions
start() {
echo "Start..."
/usr/local/nginx/sbin/nginx &> /dev/null
if [ $? -eq 0 ];then
echo "Start successful!"
else
echo "Start failed!"
fi
}

stop() {
if killproc nginx -QUIT ;then
echo "Stopping..."
fi
}

restart() {
stop
sleep 1
start
}

reload() {
killproc nginx -HUP
echo "Reloading..."
}

configtest() {
/usr/local/nginx/sbin/nginx -t
}

case $1 in
start)
start ;;
stop)
stop ;;
restart)
restart ;;
reload)
reload ;;
configtest)
configtest ;;
*)
echo "Usage: nginx {start|stop|restart|reload|configtest}"
;;
esac

之後給這個文件可執行許可權:
# chmod +x /etc/init.d/nginx
好了,現在可以使用 start,stop 這些參數控制Nginx服務了

由於腳本是我自己寫的,還有許多不盡人意的地方,歡迎大家修改和完善!
現在我們就試試啟動服務看看效果吧:
# service nginx start
記得關閉 SElinux 和 iptables 防火牆哦,
# service iptables stop
# setenforce 0
接下來就在瀏覽器中訪問你服務的IP看看效果吧!是不是出項了歡迎的字樣呢

接下來就研究下 Nginx 的配置文件吧!
# vim /usr/local/nginx/conf/nginx.conf
各項參數的意義如下:
worker_processes 1; 工作進程數量

error_log logs/error.log; 日誌文件位置
pid logs/nginx.pid; pid文件位置
worker_connections 1024; 沒進程的連接數
listen 80; 監聽埠
server_name localhost; 主機名
root html; 網站根目錄
index index.html index.htm; 網站索引頁
error_page 500 502 503 504 /50x.html; 訪問錯誤頁面
剩下的其他被注釋掉的代碼塊:
location ~ \.php$ { . . . . . . } 對PHP的支持,需要安裝PHP
server { . . . . . . } 添加server代碼塊能添加虛擬主機
剩下還有監聽443埠的超文本傳輸安全協議 HTTPS server 需要在編譯Nginx時添加ssl的支持

接下來我們試著添加一台虛擬主機吧,虛擬主機的添加可以基於埠,可以基於IP,也可以基於主機名,我們挨個來看看:
基於埠:
首先編輯配置文件,添加server代碼塊,記得要寫到http{ . . . . . . }這個大的代碼塊中。
server {
listen 8080;
server_name localhost;

location / {
root /var/www/html;
index index.html index.htm;
}
}
這樣就添加了一個監聽8080埠的服務,你也可以定義自己喜歡的埠哦。
接下來檢查下配置文件有沒有問題,如果最後一個單詞顯示successful就代表沒問題了,可以重新啟動Nginx了
# service nginx configtest
# service nginx restart
接下來就給第二個虛擬主機寫一個index吧!首先創建目錄
# mkdir -pv /var/www/html
# echo '<h1>Hi! This is 8080!</h1>' > /var/www/html/index.html
好了 接下來試著在瀏覽器中訪問訪問,記得第二個主機要加上埠訪問哦

現在試著用不同的IP建立虛擬主機吧!我們可以在一塊網卡上綁定多個IP地址的方式來實現
# ifconfig eth0:0 10.0.0.4/8
記得把IP換成你自己的哦!然後ifconfig看看是不是多出來一個網卡IP了呢
讓後繼續修改配置文件,這回要修改兩個地方,一個是原本自帶的站點的 listen 項,一個是自己添加的站點的 listen 項。
基於IP:
server {
listen 10.0.0.3:80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}

server {
listen 10.0.0.4:80;
server_name localhost;

location / {
root /var/www/html;
index index.html index.htm;
}
}
讓他們只監聽不同的IP,用相同的埠

接下來再瀏覽器上用不同的IP來訪問試試吧,及的還得重啟Nginx,先檢查一下,出現錯誤了看看哪裡配置的不對,然後就可以重啟了。
# service nginx congiftest
# service nginx restart
如果配置給網卡的第二個IP不想要了,把它停掉就可以了
# ifconfig eth0:0 down
再 ifconfig 看看是不是沒有了呢

現在試試用不同的主機名吧!也是企業用的最多的方式。我們把兩個站點的listen項都改為80,然後修改service_name項為定義的主機名
基於主機名:

server {
listen 80;
server_name ybmq.com;

location / {
root html;
index index.html index.htm;
}
}

server {
listen 80;
server_name zhzz.com;

location / {
root /var/www/html;
index index.html index.htm;
}
}

然後重啟Nginx吧!

可是我們在瀏覽器上怎麼通過域名訪問呢?要知道我們訪問 啊,qq 啊之類的是通過DNS伺服器的,難道我們還要配置一台DNS伺服器?其實不然,我們通過修改客戶機的 hosts 文件就可以了。hosts文件是一個本地的域名解析文件,我們要解析哪些域名只要把域名和對應的IP寫到一起就可以了。在Windows XP之後的系統中,這個文件位於:
C:\Windows\System32\drivers\etc\hosts
我們用文本編輯器打開,添加兩個相同的IP對應的兩個不同的主機名就可以了。
如下圖所示

如果你打開這個文件發現已經有很多IP地址了,可以直接在最後加入這兩行,也可以直接清空這個文件,不會有什麼問題的。這個文件的用途還可以屏蔽一些網站哦,只需要把網址對於的IP改為 127.0.0.1 也就是本地回環地址,瀏覽器查詢域名對應的IP時時先通過查詢這個文件的,如果查詢到了,不管對錯都不會訪問DNS伺服器了,所以我們給它一個錯誤的地址,那它一輩子也打不開被屏蔽掉的網站了。
好了 接下來就在瀏覽器中試試用用域名訪問你的兩個站點吧。
如果大家還用IP訪問會是什麼情況呢?我不說了,大家還是自己測試吧 哈哈o(^▽^)o

C. 如何在nginx伺服器部署ssl證書

1、創建SSL證書

1.1生產私鑰,openssl genrsa -des3 -out xn2.lqb.com.key 2048。此命令將生成2048位的RSA私鑰,使用DES3演算法,私鑰文件名可任意命名,在Nginx配置中指定文件路徑即可,會提示設定私鑰密碼,請設置密碼,並牢記。

[root@Monitorssl]#opensslgenrsa-des3-outxn2.lqb.com2048 GeneratingRSAprivatekey,2048bitlongmolus …………………………….+++ ……………………………………………….+++ eis65537(0x010001) Enterpassphraseforxn2.lqb.com: Verifying-Enterpassphraseforxn2.lqb.com:

1.2以上生產的key是有密碼的,如果把密碼去除,執行如下命令openssl rsa -in xn2.lqb.com -out xn2.lqb.com_nopwd.key

[root@Monitorssl]#ls xn2.lqb.com [root@Monitorssl]#opensslrsa-inxn2.lqb.com-outxn2.lqb.com_nopwd.key Enterpassphraseforxn2.lqb.com: writingRSAkey

1.3由已生產的私鑰生成證書請求文件CSR。openssl rsa -in xn2.lqb.com -out xn2.lqb.com_nopwd.key

[root@Monitorssl]#opensslrsa-inxn2.lqb.com-outxn2.lqb.com_nopwd.key Enterpassphraseforxn2.lqb.com: writingRSAkey [root@Monitorssl]#opensslreq-new-keyxn2.lqb.com-outxn2.lqb.com.csr Enterpassphraseforxn2.lqb.com: intoyourcertificaterequest. . , Ifyouenter』.』,thefieldwillbeleftblank. —– CountryName(2lettercode)[AU]:CN StateorProvinceName(fullname)[Some-State]:shanghai LocalityName(eg,city)[]:shanghai OrganizationName(eg,company)[InternetWidgitsPtyLtd]:xn2.lqb.com OrganizationalUnitName(eg,section)[]:IT CommonName(e.g.serverFQDNorYOURname)[]:xn2.lqb.com EmailAddress[]:2223344@qq.com Pleaseenterthefollowing』extra』attributes Achallengepassword[]: Anoptionalcompanyname[]: [root@Monitorssl]#ls xn2.lqb.comxn2.lqb.com.csrxn2.lqb.com_nopwd.key

1.4.證書請求文件CSR文件必須有CA的簽名才能形成證書,可以將此CSR發給StartSSL(可免費)、verisign(一大筆錢)等地方由他來驗證。也可以自己做CA,自己給自己頒發證書。創建一個自己簽署的CA證書。openssl req -new -x509 -days 3650 -key xn2.lqb.com -out xn2.lqb.com.crt

[root@Monitorssl]#opensslreq-new-x509-days3650-keyxn2.lqb.com-outxn2.lqb.com.crt xn2.lqb.comxn2.lqb.com.csrxn2.lqb.com_nopwd.key [root@Monitorssl]#opensslreq-new-x509-days3650-keyxn2.lqb.com_nopwd.key-outxn2.lqb.com.crt intoyourcertificaterequest. . , Ifyouenter』.』,thefieldwillbeleftblank. —– CountryName(2lettercode)[AU]:CN StateorProvinceName(fullname)[Some-State]:Shanghai LocalityName(eg,city)[]:shanghai OrganizationName(eg,company)[InternetWidgitsPtyLtd]:lqb.com OrganizationalUnitName(eg,section)[]:IT CommonName(e.g.serverFQDNorYOURname)[]:xn2.lqb.com EmailAddress[]: [root@Monitorssl]#ls xn2.lqb.comxn2.lqb.com.crtxn2.lqb.com.csrxn2.lqb.com_nopwd.key

2、配置nginx虛擬主機文件

[root@Monitorssl]#vim../server.conf server{ listen80; server_namexn2.lqb.com; root/html/xn2; #rewrite^/(.*)$https:xn3.lqb.com/$1permanent; location/{ indexindex.html; #proxy_cachemycache; #proxy_cache_valid2003h; #proxy_cache_valid30130210m; #proxy_cache_validall1m; #proxy_cache_use_staleerrortimeouthttp_500http_502http_503; # #proxy_passhttp://192.168.180.9; #proxy_set_headerHost$host; #proxy_set_headerX-Real-IP$remote_addr; } location/images/ { indexindex.html; } } server{ listen*:443; server_namexn2.lqb.com; sslon;###位虛擬主機開啟ssl支持 ssl_certificate/usr/local/nginx/conf/server/ssl/xn2.lqb.com.crt;###為虛擬主機指定簽名證書文件 ssl_certificate_key/usr/local/nginx/conf/server/ssl/xn2.lqb.com_nopwd.key;###為虛擬主機指定私鑰文件 ##ssl_session_timeout5m;####客戶端能夠重復使用存儲在緩存中的會話參數時間 root/html/xn3; location/images/{ indexindex.html; } location/{ proxy_passhttp://192.168.180.23; proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; } }

閱讀全文

與ssl模塊編譯進nginx相關的資料

熱點內容
編譯target 瀏覽:910
java圖片縮略圖 瀏覽:883
初中學生編程的最後境界 瀏覽:493
鴻蒙方舟編譯器安卓是什麼 瀏覽:239
手機加密中斷數據 瀏覽:505
學校機房伺服器長什麼樣子 瀏覽:248
csharp的編譯器是啥 瀏覽:486
linux電腦如何連接伺服器地址 瀏覽:955
阿里域名郵箱pop伺服器地址 瀏覽:852
李白pdf 瀏覽:782
pdfmax 瀏覽:223
跑步緊身褲壓縮褲 瀏覽:215
鄭州金水房價查詢app哪個好 瀏覽:575
房地產開發流程pdf 瀏覽:243
小鵬p7能耗APP怎麼查 瀏覽:243
如何代掛伺服器 瀏覽:286
安卓機拍視頻時怎麼當背景音樂 瀏覽:879
方舟編譯器華為p20pro更新 瀏覽:30
php程序漏洞 瀏覽:552
手機app怎麼轉過去 瀏覽:233