nginx(二) nginx編譯安裝 及 配置WEB服務
在前面《nginx詳解》文章中,我們對nginx有了一個基本的認識:包括應用場景、nginx基本架構、功能特性、并發模型以及配置說明等,我們知道nginx應用比較多的場景是WEB伺服器和反向代理伺服器。
下面将先進行nginx編譯安裝,然後再進行nginx的WEB服務相關的應用配置:包括設定配置檔案vim下文法高亮顯示、配置虛拟主機、基于IP的通路控制、基于使用者認證的通路控制、建立下載下傳站點下載下傳清單、URL位址重寫/重定向、防盜鍊、提供Nginx狀态頁面、配置gzip壓縮、日志、基于SSL提供https服務等。
1、配置環境準備
1、WEB伺服器:2、Client端:主機系統:CentOS 6.4 x86_64;
IP:192.168.18.242 (host name:node2.tjiyu.com);
IP:192.168.18.245;
一般浏覽器;
2、下載下傳編譯安裝
去nginx官網上把最新穩定版本的源碼包下載下傳下來,我們這裡使用nginx-1.10.2版本;然後把源碼包放到我們操作的主機上,下面開始編譯安裝。
2-1、解壓,建立軟連接配接
[[email protected] ~]# tar xf nginx-1.10.2.tar.gz [[email protected] ~]# ln -sv nginx-1.10.2 nginx [[email protected] ~]# cd nginx [[email protected] nginx]# ll
2-2、安裝編譯開發工具類庫
用yum安裝、更新開發工具"Development Tools"和"Server Platform Deveopment",而nginx會依賴openssl-devel和pcre-devel類庫,安裝如下:
[[email protected] nginx]# yum groupinstall "Development Tools" "Server Platform Deveopment" [[email protected] ~]# yum install openssl-devel pcre-devel
2-3、建立使用者和使用者組
分别建立名為"nginx"的使用者群組,用來運作nginx的worker程序,操作如下:
[[email protected] nginx]# groupadd -r nginx [[email protected] nginx]# useradd -r -g nginx nginx
2-4、編譯并安裝
先configure指定編譯選項,如安裝目錄、上面建立的運作使用者、需要的擴充子產品(SSL、FastCGI)等,選項及參數說明:http://nginx.org/en/docs/configure.html,操作如下:
[[email protected] nginx]# ./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --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
Configure成功如下:
接着開始編譯并安裝,如下:
[[email protected] nginx]# make && make install
2-5、為nginx提供SysV init服務腳本
先建立/etc/init.d/nginx服務腳本,這基于ngInx自身提供的指令實作的,腳本内容如下:
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
并為此腳本賦予執行權限,然後添加到系統服務管理清單,并讓其開機自動啟動,操作如下:
[[email protected] nginx]# vim /etc/init.d/nginx [[email protected] nginx]# chmod +x /etc/init.d/nginx [[email protected] nginx]# chkconfig --add nginx [[email protected] nginx]# chkconfig nginx on [[email protected] nginx]# chkconfig --list nginx
2-6、啟動并通路測試
啟動nginx,我們看到因為httpd占用80端口而失敗,關閉httpd後再啟動nginx正常;檢視網絡狀态,可以看到nginx正在監聽80端口;用測試主機通路nginx主機的IP,可以看到nginx的歡迎頁面,過程如下:
[[email protected] nginx]# service nginx start [[email protected] nginx]# netstat -ntulp | grep nginx
3、配置Nginx
前面編譯nginx的時候,我們用選項--conf-path=/etc/nginx/nginx.conf(預設也是這個目錄),指定的了配置檔案及所在目錄,是以我們到/etc/nginx/下可以看到nginx.conf配置檔案,而.default結尾的是nginx預設編譯選項的配置檔案,已經沒有意義了。
前面《nginx詳解》我們已經詳細分析說明nginx配置檔案的幾個配置區域塊和大部分的配置選項,下面就不一一說明了,隻說明一些用到的配置選項。
3-1、設定配置檔案vim下文法高亮顯示
linux系統下vim或者vi編輯器預設是沒有對nginx配置的文法高亮設定。但是我們可以到http://www.vim.org/scripts/script.php?script_id=1886下載下傳nginx.vim,然後根據它上面的說明,進行簡單的配置,如下:
[[email protected] ~]# mkdir .vim/syntax -pv [[email protected] ~]# cd .vim/syntax/ [[email protected] syntax]# mv ~/nginx.vim ./ [[email protected] syntax]# ls nginx.vim [[email protected] syntax]# cd .. [[email protected] .vim]# vim filetype.vim [[email protected] .vim]# cat filetype.vim au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif [[email protected] .vim]# vim /etc/nginx/nginx.conf
3-2、配置虛拟主機
編輯nginx.conf,配置兩個虛拟主機www.tjiyu.com和www.test.com,資源檔案目錄分别/data/tjiyu和/data/test,可以看到最簡單的配置隻需要三行,操作如下:
[[email protected] nginx]# cp nginx.conf nginx.conf.bak [[email protected] nginx]# vim nginx.conf
server { listen 80; server_name www.tjiyu.com; location / { root /data/tjiyu; index index.html index.htm; } } server { listen 80 default_server; #配置預設虛拟主機 server_name www.test.com; root /data/test; }
然後分别建立資源目錄,提供index.html測試頁面,然後使nginx重新加載配置,操作如下:
[[email protected] nginx]# mkdir /data [[email protected] nginx]# mkdir /data/tjiyu [[email protected] nginx]# echo "<h1>www.tjiyu.com</h1>" > /data/tjiyu/index.html [[email protected] nginx]# mkdir /data/test [[email protected] nginx]# echo "<h1>www.test.com</h1>" > [[email protected] nginx]# service nginx reload
在我們的測試主機上配置hosts,讓通路虛拟主機名可以指向主機IP,win7 x86_64上C:\Windows\System32\drivers\etc\HOSTS添加如下兩行記錄,如上:
192.168.18.242 www.tjiyu.com 192.168.18.242 www.test.com
然後通路兩個通路虛拟主機,可以看到提供的測試頁面;因為我們在www.test.com的listen選項配置了default_server,是以直接通路主機IP是傳回的是該虛拟主機的頁面,如下:
3-3、基于IP的通路控制
基于IP的通路控制是ngx_http_access_module子產品的功能,隻有允許選項allow和禁止選項deny。
配置第二台虛拟主機隻允許的192.168.18.*網段内的主機通路,但除測試主機192.168.18.244外,注意,多個規則是自上而下進行比對的,是以不允許測試主機通路得在最上面;測試通路www.test.com可以看到傳回403禁止通路頁面,而通路www.tjiyu.com是正常的,過程如下:
[[email protected] nginx]# vim nginx.conf [[email protected] nginx]# service nginx reload
server { listen 80 default_server; server_name www.test.com; root /data/test; deny 192.168.18.244; #不允許測試主機通路 allow 192.168.18.0/24; #隻允許的192.168.18.*網段内的主機通路 deny all; #不允許其他所有外網通路 }
3-4、基于使用者認證的通路控制
基于使用者認證的通路控制是ngx_http_auth_basic_module子產品的功能,隻有配置名稱(或者off)選項auth_basic和配置檔案選項auth_basic_user_file,檔案由htpasswd生成,包括使用者和密碼。
先建立需要使用者認證的目錄/data/test/admin,提供一個測試頁面;然後用htpasswd生成認證檔案;編輯配置檔案,用location比對/data/test/admin目錄,接着在裡面配置通路控制配置的名稱字元串和通路控制配置的檔案,過程如下:
[[email protected] nginx]# mkdir /data/test/admin [[email protected] nginx]# echo "<h1>admin area</h1>" > /data/test/admin/index.html [[email protected] nginx]# htpasswd -c -m /etc/nginx/.htpasswd admin [[email protected] nginx]# vim nginx.conf [[email protected] nginx]# service nginx reload
server { listen 80 default_server; #配置預設虛拟主機 server_name www.test.com; #配置虛拟主機名 root /data/test; #配置資源檔案根目錄 #deny 192.168.18.244; #不允許測試主機通路 allow 192.168.18.0/24; #隻允許的192.168.18.*網段内的主機通路 deny all; #不允許其他所有外網通路 location /admin/ { #比對基于使用者認證的通路控制 auth_basic "admin area"; #通路控制配置的名稱字元串,或者off關閉 auth_basic_user_file /etc/nginx/.htpasswd; #通路控制配置的檔案,htpasswd生成,包含使用者名用密碼 } }
測試通路www.test.com正常,通路www.test.com/admin/需要使用者和密碼認證,也就實作了目錄/data/test/admin裡的資源需要使用者認證,如下:
3-5、建立下載下傳站點下載下傳清單
下載下傳站點下載下傳清單是ngx_http_autoindex_module子產品的功能,有幾個選項,最基本就是開關選項autoindex on | off和顯示時間選項autoindex_localtime on | off。
先建立存放下載下傳資源的目錄/data/test/download,複制一些檔案進去提供測試;然後編輯配置檔案,用location比對/data/test/download目錄,接着在裡面配置打開下載下傳清單功能;測試通路www.test.com/download可以看到下載下傳清單,過程如下:
[[email protected] nginx]# mkdir /data/test/download [[email protected] nginx]# cp /etc/nginx/* /data/test/download/ [[email protected] nginx]# vim nginx.conf [[email protected] nginx]# service nginx reload
server { listen 80 default_server; server_name www.test.com; root /data/test; location /download/ { autoindex on; #打開下載下傳清單功能 autoindex_localtime on; #顯示時間 } }
3-6、URL位址重寫/重定向
URL位址重寫/重定向是ngx_http_rewrite_module子產品的功能,通過正則比對,把比對的URL重寫為指定的URL,以重寫的URL來請求響應。主要應用在實作URL跳轉、域名跳轉、站點鏡像等,比如網站改版,目錄結構發生改變,但不希望不改變頁面中的URL,用URL位址重寫來實作。
1、if和rewrite選項
這裡有兩個比較重要的選項,if和rewrite:If在上面已經介紹過了,在很多地方都用到,這裡用于檢測條件是否成立;而rewrite regex replacement [flag],比對regex正規表達式(可以省略,直接重寫),以replacement重寫代替,flag為标志位,主要有:
last:一旦被目前規則比對并重寫後立即停止檢查後續餓的其他rewrite的規則,而後通過重寫後的規則重新發起請求; break:一旦被目前規則比對并重寫後立即停止檢查後續餓的其他rewrite的規則,而後繼續由nginx進行後續的操作; redirect:傳回302臨時重定向代碼; permanent:傳回301永久重定向;
注意:當有多個rewrite規則一起使用時,可能會循環比對,nginx最多循環10次,超出之後傳回500錯誤;一般将rewrite寫在location中時都使用break标志,或者将rewrite寫if上下文中。
2、配置
配置www.test.com虛拟主機的當目錄$root_dir/images改為$root_dir /imgs時,可以通過URL重寫,讓www.test.com/images/*請求還能和原來一樣,不過實際變為了www.test.com/imgs/*。
直接建立/data/test/imgs目錄,放一些圖檔檔案進去用來測試;然後配置檔案;測試通路www.test.com/images/2.jpg可以正常看到剛才放到/data/test/imgs圖檔,過程如下:
[[email protected] nginx]# mkdir /data/test/imgs [[email protected] nginx]# mv /root/*.jpg /data/test/imgs/ [[email protected] nginx]# ls /data/test/imgs/ [[email protected] nginx]# vim nginx.conf [[email protected] nginx]# service nginx reload
server { listen 80 default_server; server_name www.test.com; root /data/test; location /images/ { rewrite ^/images/(.*)$ /imgs/$1 break; #URL重寫:讓www.test.com/images/*請求還能和原來一樣,不>過實際變為了www.test.com/imgs/* } }
3-7、防盜鍊
防盜鍊基于是ngx_http_referer_module子產品的功能,主要通過請求頭部"Referer"字段識别,配置有兩點:
(1)valid_referers選項,定義合規引用
valid_referers none |blocked |server_names|string ... none表示沒有"Referer",blocked表示 "Referer" 沒有沒有以"http://"或"https://"開頭;
(2)($invaild_referer變量,判斷不合規的引用,傳回一個提示
if ($invaild_referer) { rewrite ^/.*$ http://www.a.com/403.html }
下面配置當www.tjiyu.com虛拟主機頁面引用www.test.com虛拟主機上的圖檔時,傳回一個"圖檔僅供内部交流使用的圖檔"。www.test.com虛拟主機上配置location比對圖檔檔案,valid_referers定義合規引用,包括自己的虛拟主機名稱等,然後if ($invaild_referer)判斷不合規的引用,用URL位址重寫傳回一個提示圖檔,操作配置如下:
[[email protected] nginx]# vim nginx.conf
location ~* \.(jpg|gif|jpeg|png)$ { #比對圖檔檔案請求 valid_referers none blocked www.test.com *.test.com; #定義合規引用,包括自己站主機等 if ($invalid_referer) { rewrite ^/ http://www.test.com/imgs/a.jpg; #判斷不合規的引用,用URL位址重寫傳回一個提示>圖檔 } }
然後在www.tjiyu.com虛拟主機測試頁面加入圖檔引用,測試通路www.tjiyu.com可以看到引用的http://www.test.com/imgs/1.jpg,但傳回的是我們配置http://www.test.com/imgs/a.jpg,過程如下:
[[email protected] nginx]# vim /data/tjiyu/index.html [[email protected] nginx]# service nginx reload
<img src="http://www.test.com/imgs/1.jpg">
3-8、提供Nginx狀态頁面
提供Nginx狀态頁面指的是ngx_http_stub_status_module子產品的功能,還有一個可以提供更詳細資訊的ngx_http_status_module子產品,配置都很簡單,不過編譯nginx時需要指定包含進來。
可以看到的資訊有:
當下處于活動狀态的總數;
接受的總數,已經建立和處理總數,請求的總數;
正在接受的并發請求個數,正在讀取的個數或發往用戶端的,長連接配接中的處于活動狀态的值;
配置一個location比對通路,裡面"stub_status;"就可以打開狀态頁面功能,注意1.7.5版本前需要"stub_status on;",然後加入使用者認證和不記錄日志,以防資訊洩露,過程如下:
[[email protected] nginx]# vim nginx.conf [[email protected] nginx]# service nginx reload
location /stub_status { stub_status; #打開狀态頁面功能 auth_basic "stub_status"; #通路控制配置的名稱字元串,或者off關閉 auth_basic_user_file /etc/nginx/.htpasswd; #通路控制配置的檔案,htpasswd生成,包含使用者名用密碼 access_log off; #通路不記錄日志 }
3-9、配置gzip壓縮
提供gzip壓縮指的是ngx_http_gzip_module子產品的功能,nginx預設會附帶gzip壓縮的功能,而ngx_http_gzip_static_module子產品是提供gzip預壓縮功能,需要編譯指定。
配置選項有:
gzip on|off gzip_buffer 使用的緩存大小 gzip_comp_level 壓縮的級别 gzip_disable 不壓縮的類型或浏覽器 gzip_min_length 最少壓縮的大小 gzip_http_version 壓縮完成以後發送http的版本 gzip_types:隻壓縮的格式
nginx将響應封包發送至用戶端之前可以啟用壓縮功能,這能夠有效地節約帶寬,并提高響應至用戶端的速度。可以在http塊或server塊中配置,一般在http塊中配置,配置如下:
gzip on; #打開gzip壓縮功能 gzip_http_version 1.0; #使用1.0版本 gzip_comp_level 4; #壓縮級别為4 gzip_min_length 64; #内容超過最少長度後才開啟壓縮:64k gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json; #壓縮的檔案類型:文本壓 gzip_disable msie6; #不壓縮的浏覽器類型:ie6不支援
3-10、配置日志
Nginx常用的日志分為:錯誤日志error_log,這是nginx核心子產品ngx_core_module提供的,前面《nginx詳解》已有詳細的說明;http通路日志access_log,這是ngx_http_log_module子產品提供的。
這兩種日志預設都是打開記錄的,而我們configure配置編譯的時候已經指定了日志存放的目錄,我們可以看到目錄下已經存在這兩個日志檔案,如下:
下面就來配置http通路日志,配置選項有:
注意,預設access_log logs/access.log combined; log_format combined "...";也就是說日志格式為combined,如下:access_log off | path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; #設定存放路徑、(引用)日志格式、緩存區大小、壓縮等級、緩存時間等; log_format name string ...; #定義日志格式,access_log引用 open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; #設定日志檔案緩存
log_format combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';
格式定義中可以使用公共變量和僅日志格式中的變量。
典型應用配置:如果請求經過nginx反向代理伺服器,後端web伺服器無法直接擷取到用戶端真實的IP位址(因為$remote_addr擷取到的是反向代理IP位址)。可以配置反向代理伺服器在轉發請求的http頭資訊中增加"x-Forwarded-For"行資訊,該資訊中記錄用戶端IP位址和用戶端請求的伺服器位址;而後在後端伺服器就配置重新定義日志格式,增加"$http_x_forwarded_for"變量資訊到格式,就如原來配置檔案中存在的配置(已注釋,後面介紹nginx負載均衡再配置),如下:
3-11、配置基于SSL提供https服務
提供基于SSL提供https服務的是ngx_mail_ssl_module子產品,需要編譯指定。
1、建立CA自簽證書
[[email protected] ~]# cd /etc/pki/CA/ [[email protected] CA]# ls certs crl newcerts private [[email protected] CA]# cd private/ [[email protected] private]# ls [[email protected] private]# (umask 077; openssl genrsa 2048 > cakey.pem) Generating RSA private key, 2048 bit long modulus ....+++ ..........................................................+++ e is 65537 (0x10001) [[email protected] private]# openssl req -new -x509 -key ./cakey.pem -out ../cacert.pem 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]:CN State or Province Name (full name) []:HZ Locality Name (eg, city) [Default City]:ZJ Organization Name (eg, company) [Default Company Ltd]:TJIYU Organizational Unit Name (eg, section) []:TEST Common Name (eg, your name or your server's hostname) []:ca.tjiyu.com Email Address []:[email protected] [[email protected] private]# ll 總用量 4 -rw-------. 1 root root 1675 10月 20 17:18 cakey.pem [[email protected] private]# cd .. [[email protected] CA]# touch serial [[email protected] CA]# echo 01 > serial [[email protected] CA]# touch index.txt [[email protected] CA]# ll 總用量 24 -rw-r--r--. 1 root root 1383 10月 20 17:25 cacert.pem drwxr-xr-x. 2 root root 4096 9月 27 20:30 certs drwxr-xr-x. 2 root root 4096 9月 27 20:30 crl -rw-r--r--. 1 root root 0 10月 20 17:26 index.txt drwxr-xr-x. 2 root root 4096 9月 27 20:30 newcerts drwx------. 2 root root 4096 10月 20 17:18 private -rw-r--r--. 1 root root 3 10月 20 17:26 serial
2、生成證書申請
[[email protected] CA]# mkdir /etc/nginx/ssl [[email protected] CA]# cd /etc/nginx/ssl/ [[email protected] ssl]# (umask 077; openssl genrsa 1024 > nginx.key) Generating RSA private key, 1024 bit long modulus ....................................................++++++ ..............++++++ e is 65537 (0x10001) [[email protected] ssl]# openssl req -new -key nginx.key -out nginx.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]:CN State or Province Name (full name) []:HZ Locality Name (eg, city) [Default City]:ZJ Organization Name (eg, company) [Default Company Ltd]:TJIYU Organizational Unit Name (eg, section) []:TEST Common Name (eg, your name or your server's hostname) []:www.test.com Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
3、讓CA簽名并頒發證書
[[email protected] ssl]# openssl ca -in nginx.csr -out nginx.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) Validity Not Before: Oct 20 09:31:01 2016 GMT Not After : Oct 18 09:31:01 2026 GMT Subject: countryName = CN stateOrProvinceName = HZ organizationName = TJIYU organizationalUnitName = TEST commonName = www.test.com X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: AD:3D:C4:0D:11:A0:68:51:1B:CE:5E:45:B3:7C:A0:A8:2C:01:A8:27 X509v3 Authority Key Identifier: keyid:5B:22:1A:8A:67:E6:C2:8A:CA:DA:F5:5C:97:86:76:5B:09:94:88:48 Certificate is to be certified until Oct 18 09:31:01 2026 GMT (3650 days) Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated [[email protected] ssl]# ll 總用量 12 -rw-r--r--. 1 root root 3736 10月 20 17:33 nginx.crt -rw-r--r--. 1 root root 635 10月 20 17:30 nginx.csr -rw-------. 1 root root 887 10月 20 17:27 nginx.key
4、修改配置檔案
使用另外一個server配置SSL監聽433端口,注意,配置如下:
server { listen 443 ssl; #打開SSL,監聽433端口 server_name localhost; ssl_certificate /etc/nginx/ssl/nginx.crt; #證書檔案 ssl_certificate_key /etc/nginx/ssl/nginx.key; #證書key檔案 ssl_session_cache shared:SSL:1m; #所有worker程序共享會話緩存 ssl_session_timeout 5m; #會話逾時時間:5分鐘 ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2; #支援SSL協定,從1.1.13和1.0.12版本開始支援TLSv1.1和TLSv1.2 ssl_ciphers HIGH:!aNULL:!MD5; #加密方法 ssl_prefer_server_ciphers on; #由伺服器選擇加密方法 location / { root html; index index.html index.htm; } }
5、測試
重新加載配置後,檢視網絡狀态,可以看到nginx監聽了433端口;通路可以看到警告提示,因為我們的證書不是正規機構申請的,無法認證;繼續通路可以看到正常頁面 ,如下:
到這裡,我們成功對nginx進行了編譯安裝以及配置WEB服務相關内容,後面将進行nginx的反向代理、負載均衡、後端健康檢測和緩存等相關功能配置……
【參考資料】
1、nginx官網文檔:http://nginx.org/en/docs/
2、nginx詳解:http://blog.csdn.net/tjiyu/article/details/53027619
3、Web伺服器之Nginx詳解:http://freeloda.blog.51cto.com/2033581/1285722