nginx(二) nginx編譯安裝 及 配置WEB服務
在前面《nginx詳解》文章中,我們對nginx有了一個基本的認識:包括應用場景、nginx基本架構、功能特性、併發模型以及配置說明等,我們知道nginx應用比較多的場景是WEB服務器和反向代理服務器。
下面將先進行nginx編譯安裝,然後再進行nginx的WEB服務相關的應用配置:包括設置配置文件vim下語法高亮顯示、配置虛擬主機、基於IP的訪問控制、基於用戶認證的訪問控制、建立下載站點下載列表、URL地址重寫/重定向、防盜鏈、提供Nginx狀態頁面、配置gzip壓縮、日誌、基於SSL提供https服務等。
1、配置環境準備
1、WEB服務器:
主機系統:CentOS 6.4 x86_64;
IP:192.168.18.242 (host name:node2.tjiyu.com);
2、Client端:
IP:192.168.18.245;
一般瀏覽器;
2、下載編譯安裝
去nginx官網上把最新穩定版本的源碼包下載下來,我們這裏使用nginx-1.10.2版本;然後把源碼包放到我們操作的主機上,下面開始編譯安裝。
2-1、解壓,創建軟連接
[root@node2 ~]# tar xf nginx-1.10.2.tar.gz [root@node2 ~]# ln -sv nginx-1.10.2 nginx [root@node2 ~]# cd nginx [root@node2 nginx]# ll
2-2、安裝編譯開發工具類庫
用yum安裝、更新開發工具"Development Tools"和"Server Platform Deveopment",而nginx會依賴openssl-devel和pcre-devel類庫,安裝如下:
[root@node2 nginx]# yum groupinstall "Development Tools" "Server Platform Deveopment" [root@node2 ~]# yum install openssl-devel pcre-devel
2-3、創建用戶和用戶組
分別創建名爲"nginx"的用戶和組,用來運行nginx的worker進程,操作如下:
[root@node2 nginx]# groupadd -r nginx [root@node2 nginx]# useradd -r -g nginx nginx
2-4、編譯並安裝
先configure指定編譯選項,如安裝目錄、上面創建的運行用戶、需要的擴展模塊(SSL、FastCGI)等,選項及參數說明:http://nginx.org/en/docs/configure.html,操作如下:
[root@node2 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成功如下:
接着開始編譯並安裝,如下:
[root@node2 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
併爲此腳本賦予執行權限,然後添加到系統服務管理列表,並讓其開機自動啓動,操作如下:
[root@node2 nginx]# vim /etc/init.d/nginx [root@node2 nginx]# chmod +x /etc/init.d/nginx [root@node2 nginx]# chkconfig --add nginx [root@node2 nginx]# chkconfig nginx on [root@node2 nginx]# chkconfig --list nginx
2-6、啓動並訪問測試
啓動nginx,我們看到因爲httpd佔用80端口而失敗,關閉httpd後再啓動nginx正常;查看網絡狀態,可以看到nginx正在監聽80端口;用測試主機訪問nginx主機的IP,可以看到nginx的歡迎頁面,過程如下:
[root@node2 nginx]# service nginx start [root@node2 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,然後根據它上面的說明,進行簡單的配置,如下:
[root@node2 ~]# mkdir .vim/syntax -pv [root@node2 ~]# cd .vim/syntax/ [root@node2 syntax]# mv ~/nginx.vim ./ [root@node2 syntax]# ls nginx.vim [root@node2 syntax]# cd .. [root@node2 .vim]# vim filetype.vim [root@node2 .vim]# cat filetype.vim au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif [root@node2 .vim]# vim /etc/nginx/nginx.conf
3-2、配置虛擬主機
編輯nginx.conf,配置兩個虛擬主機www.tjiyu.com和www.test.com,資源文件目錄分別/data/tjiyu和/data/test,可以看到最簡單的配置只需要三行,操作如下:
[root@node2 nginx]# cp nginx.conf nginx.conf.bak [root@node2 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重新加載配置,操作如下:
[root@node2 nginx]# mkdir /data [root@node2 nginx]# mkdir /data/tjiyu [root@node2 nginx]# echo "<h1>www.tjiyu.com</h1>" > /data/tjiyu/index.html [root@node2 nginx]# mkdir /data/test [root@node2 nginx]# echo "<h1>www.test.com</h1>" > [root@node2 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是正常的,過程如下:
[root@node2 nginx]# vim nginx.conf [root@node2 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目錄,接着在裏面配置訪問控制配置的名稱字符串和訪問控制配置的文件,過程如下:
[root@node2 nginx]# mkdir /data/test/admin [root@node2 nginx]# echo "<h1>admin area</h1>" > /data/test/admin/index.html [root@node2 nginx]# htpasswd -c -m /etc/nginx/.htpasswd admin [root@node2 nginx]# vim nginx.conf [root@node2 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可以看到下載列表,過程如下:
[root@node2 nginx]# mkdir /data/test/download [root@node2 nginx]# cp /etc/nginx/* /data/test/download/ [root@node2 nginx]# vim nginx.conf [root@node2 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圖片,過程如下:
[root@node2 nginx]# mkdir /data/test/imgs [root@node2 nginx]# mv /root/*.jpg /data/test/imgs/ [root@node2 nginx]# ls /data/test/imgs/ [root@node2 nginx]# vim nginx.conf [root@node2 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地址重寫返回一個提示圖片,操作配置如下:
[root@node2 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,過程如下:
[root@node2 nginx]# vim /data/tjiyu/index.html [root@node2 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;",然後加入用戶認證和不記錄日誌,以防信息泄露,過程如下:
[root@node2 nginx]# vim nginx.conf [root@node2 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 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]; #設置日誌文件緩存
注意,默認access_log logs/access.log combined; log_format combined "...";也就是說日誌格式爲combined,如下:
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自簽證書
[root@node2 ~]# cd /etc/pki/CA/ [root@node2 CA]# ls certs crl newcerts private [root@node2 CA]# cd private/ [root@node2 private]# ls [root@node2 private]# (umask 077; openssl genrsa 2048 > cakey.pem) Generating RSA private key, 2048 bit long modulus ....+++ ..........................................................+++ e is 65537 (0x10001) [root@node2 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] [root@node2 private]# ll 總用量 4 -rw-------. 1 root root 1675 10月 20 17:18 cakey.pem [root@node2 private]# cd .. [root@node2 CA]# touch serial [root@node2 CA]# echo 01 > serial [root@node2 CA]# touch index.txt [root@node2 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、生成證書申請
[root@node2 CA]# mkdir /etc/nginx/ssl [root@node2 CA]# cd /etc/nginx/ssl/ [root@node2 ssl]# (umask 077; openssl genrsa 1024 > nginx.key) Generating RSA private key, 1024 bit long modulus ....................................................++++++ ..............++++++ e is 65537 (0x10001) [root@node2 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簽名並頒發證書
[root@node2 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 [root@node2 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