1 搭建Nginx服務器
1.1 問題
在IP地址為192.168.10. 5的主機上安裝部署Nginx服務,並可以將Nginx服務器升級到更高版本,要求編譯時啟用如下功能:
- SSL加密功能
- 查看服務器狀態信息功能
- 設置Nginx賬戶及組名稱均為nginx
然后客戶端訪問頁面驗證Nginx Web服務器:
- 使用火狐瀏覽器訪問
- 使用curl訪問
1.2 方案
使用2台RHEL6虛擬機,其中一台作為Nginx服務器(192.168.10.5)、另外一台作為測試用的Linux客戶機(192.168.10.205)
安裝Nginx需要先安裝nginx-1.7.10版本,在升級至nginx-1.8.0版本時,需要使用如下參數:
- with-http_stub_status_module:可以提供服務器狀態信息
- with-http_ssl_module:提供SSL加密功能
- user:指定賬戶
- group:指定組
1.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:構建Nginx服務器
1)使用源碼包安裝nginx軟件包
[root@svr5 ~]# yum –y install gcc pcre-devel openssl-devel //安裝常見依賴包 [root@svr5 ~]# useradd –s /sbin/nologin nginx
[root@svr5 ~]# wget http://nginx.org/download/nginx-1.6.3.tar.gz [root@svr5 ~]# tar -zxvf nginx-1.7.10.tar.gz [root@svr5 ~]# cd nginx-1.7.10 [root@svr5 nginx-1.7.10]# ./configure \ > --prefix=/usr/local/nginx \ //指定安裝路徑 > --user=nginx \ //指定用戶 > --group=nginx \ //指定組 > --with-http_stub_status_module \ //開啟狀態統計功能 > --with-http_ssl_module //開啟SSL加密功能 .. .. nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" [root@svr5 nginx-1.7.10]# make && make install //編譯並安裝
2)啟用nginx服務
[root@nginx ~]# /usr/local/nginx/sbin/nginx –c /usr/local/nginx/conf/nginx.conf
nginx服務默認通過TCP 80端口監聽客戶端請求:
[root@nginx ~]# netstat -anptu | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10441/nginx
3)為Nginx Web服務器建立測試首頁文件
Nginx Web服務默認首頁文檔存儲目錄為/usr/local/nginx/html/,在此目錄下建立一個名為index.html的文件:
[root@svr5 ~]# cat /usr/local/nginx/html/index.html <html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center><h1>Welcome to nginx!</h1></center> </body> </html>
步驟二:升級Nginx服務器
1)編譯新版本nginx軟件
[root@svr5 ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@svr5 ~]# tar -zxvf nginx-1.8.0.tar.gz [root@svr5 ~]# cd nginx-1.8.0 [root@svr5 nginx-1.8.0]# ./configure \ > --prefix=/usr/local/nginx \ > --user=nginx \ > --group=nginx \ > --with-http_stub_status_module \ > --with-http_ssl_module [root@svr5 nginx-1.8.0]# make
2)備份老的nginx主程序,並使用編譯好的新版本nginx替換老版本
[root@svr5 nginx-1.8.0]# cd /usr/local/nginx/sbin/ [root@svr5 sbin]# mv nginx nginxold //備份舊版本 [root@svr5 sbin]# cd /root/ nginx-1.8.0/objs/ [root@svr5 objs]# cp nginx /usr/local/nginx/sbin/ //拷貝新版本 [root@svr5 objs]# cd /root/nginx-1.0.5 [root@svr5 nginx-1.8.0]# make upgrade //升級 /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 kill -USR2 `cat /usr/local/nginx/logs/nginx.pid` sleep 1 test -f /usr/local/nginx/logs/nginx.pid.oldbin kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin` [root@svr5 ~]# /usr/local/nginx/sbin/nginx –v //查看版本
步驟三:客戶端訪問測試
1)分別使用瀏覽器和命令行工具curl測試服務器頁面
[root@client ~]# firefox http://192.168.10.5 [root@client ~]# curl http://192.168.10.5
2 用戶認證及訪問控制
2.1 問題
沿用練習一,通過調整Nginx服務端配置,實現以下目標:
- 訪問Web頁面需要進行用戶認證
- 用戶名為:tom,密碼為:123456
- 網站根目錄下首頁文檔所有主機均可以訪問
- 為網站創建二級目錄/test,並生成index.html文檔
- test目錄下的頁面僅192.168.10.205可以訪問
2.2 方案
通過Nginx實現Web頁面的認證與訪問控制,需要修改Nginx配置文件,在location容器中添加allow及deny語句實現訪問控制,添加auth語句實現用戶認證。最后使用htpasswd命令創建用戶及密碼即可。
2.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:修改Nginx配置文件
1)修改/usr/local/nginx/conf/nginx.conf
[root@pc205 ~]# vim /usr/local/nginx/conf/nginx.conf .. .. server { listen 80; server_name localhost; auth_basic "Input Password:"; //認證提示符 auth_basic_user_file pass.txt; //認證密碼文件 location / { root html; index index.html index.htm; } location /test { allow 192.168.10.205; //訪問控制,僅192.168.10.205可以訪問 deny all; //拒絕所有 index index.html index.htm; }
2)創建二級頁面目錄,並生成index.html文件
[root@svr5 ~]# mkdir /usr/local/nginx/html/test [root@svr5 ~]# echo “test” > /usr/local/nginx/html/test/index.html
3)生成密碼文件,創建用戶及密碼
使用htpasswd命令創建賬戶文件,需要確保系統中已經安裝了httpd-tools。
[root@svr5 ~]# htpasswd -cm /usr/local/nginx/conf/pass.txt tom New password: Re-type new password: Adding password for user tom
4)重啟Nginx服務
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s stop
[root@svr5 ~]# /usr/local/nginx/sbin/nginx
步驟二:客戶端測試
1)登錄192.168.4.205主機進行測試
[root@client ~]# firefox http://192.168.10.5 //輸入密碼后可以訪問 [root@client ~]# firefox http://192.168.10.5/test //輸入密碼后可以訪問
2)登錄非192.168.4.205的其他任意主機測試
[root@client ~]# firefox http://192.168.10.5 //輸入密碼后可以訪問 [root@client ~]# firefox http://192.168.10.5/test //輸入密碼后無法訪問
3 Nginx虛擬主機
3.1 問題
沿用練習二,配置基於域名的虛擬主機,實現以下目標:
- 實現兩個基於域名的虛擬主機,域名分別為www.aaaaa.com和bbs.aaaaa.com
- 域名為bbs.aaaaa.com的Web服務僅允許192.168.10.205訪問
- 對域名為bbs.aaaaa.com的站點進行用戶認證,用戶名稱為tom,密碼為123456
- 對域名為www.aaaaa.com的站點進行SSL加密
3.2 方案
修改Nginx配置文件,添加server容器實現虛擬主機功能;對於需要進行訪問控制的虛擬主機添加allow和deny語句;對於需要進行用戶認證的虛擬主機添加auth認證語句;對於需要進行SSL加密處理的站點添加ssl相關指令。
3.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:修改配置文件
1)修改Nginx服務配置,添加相關虛擬主機配置如下
[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf .. .. server { listen 192.168.10.5:80; //端口 server_name bbs.aaaaa.com; //域名 allow 192.168.10.205; //訪問控制,僅192.168.10.205可以訪問 deny all; //拒絕所有 auth_basic "Input Password:"; //認證提示符 auth_basic_user_file pass.txt; //認證密碼文件 location / { root bbs; //指定網站根路徑 index index.html index.htm; } } server { listen 192.168.10.5:443; server_name www.aaaaa.com; ssl on; //開啟SSL ssl_certificate cert.pem; //指定證書文件 ssl_certificate_key cert.key; //指定私鑰文件 ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root www; //指定網站根路徑 index index.html index.htm; } }
2)生成私鑰與證書
[root@svr5 ~]# openssl genrsa -out cert.key 2048 //生成私鑰 [root@svr5 ~]# openssl req -new -x509 -key cert.key -out cert.pem //生成證書 [root@svr5 ~]# cp {cert.key,cert.pem} /usr/local/nginx/conf
3)創建網站根目錄及對應首頁文件
[root@svr5 ~]# mkdir /usr/local/nginx/{www,bbs} [root@svr5 ~]# echo “www” > /usr/local/nginx/www/index.html [root@svr5 ~]# echo “bbs” > /usr/local/nginx/bbs/index.html
4)重啟nginx服務
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s stop
[root@svr5 ~]# /usr/local/nginx/sbin/nginx
步驟二:客戶端測試
1)修改/etc/hosts文件,進行域名解析
[root@client ~]# vim /etc/hosts 192.168.10.5 www.aaaaa.com bbs.aaaaa.com
2)登錄192.168.4.205主機進行測試
[root@client ~]# firefox http://bbs.aaaaa.com //輸入密碼后可以訪問 [root@client ~]# firefox https://www.aaaaa.com //信任證書后可以訪問
3)登錄非192.168.4.205的其他任意主機測試
[root@client ~]# firefox http://bbs.aaaaa.com //無法訪問 [root@client ~]# firefox https://www.aaaaa.com //信任證書后可以訪問
4 Nginx反向代理
4.1 問題
使用Nginx實現Web反向代理功能,實現如下功能:
- 后端Web服務器兩台,可以使用httpd實現
- Nginx采用輪詢的方式調用后端Web服務器
- 兩台Web服務器的權重要求設置為不同的值
- 最大失敗次數為2,失敗超時時間為30秒
4.2 方案
使用3台RHEL6虛擬機,其中一台作為Nginx代理服務器,該服務器需要配置兩塊網卡,IP地址分別為172.16.0.254和192.168.10.5,兩台Web服務器IP地址分別為192.168.10.205和192.168.10.200。測試主機IP地址為172.16.0.1。
4.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:配置Nginx服務器,添加服務器池,實現反向代理功能
1)修改/usr/local/nginx/conf/nginx.conf配置文件
[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf .. .. http { .. .. upstream webserver { server 192.168.10.205 weight=2 max_fails=2 fail_timeout=10; server 192.168.10.200 weight=1 max_fails=2 fail_timeout=10; } .. .. server { listen 80; server_name www.aaaaa.com; location / { proxy_pass http://webserver; } }
2)重啟nginx服務
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s stop
[root@svr5 ~]# /usr/local/nginx/sbin/nginx
步驟二:部署實施后端Web服務器
1)部署后端Web1服務器
后端Web服務器可以簡單使用yum方式安裝httpd實現Web服務,為了可以看出后端服務器的不同,可以將兩台后端服務器的首頁文檔內容設置為不同的內容。
[root@web1 ~]# yum -y install httpd [root@web1 ~]# echo “192.168.10.205” > /var/www/html/index.html [root@web1 ~]# service httpd start
2)部署后端Web2服務器
[root@web2 ~]# yum -y install httpd [root@web2 ~]# echo “192.168.10.200” > /var/www/html/index.html [root@web2 ~]# service httpd start
步驟三:客戶端測試
1)修改客戶端hosts文件
[root@client ~]# vim /etc/hosts .. .. 172.16.0.254 www.aaaaa.com
2)使用瀏覽器訪問代理服務器測試輪詢效果
[root@client ~]# curl http://www.aaaaa.com //使用該命令多次訪問查看效果
