配置nginx,upstream服務器
搭建Nginx服務器
1.1 問題
在IP地址為192.168.4.5的主機上安裝部署Nginx服務,並可以將Nginx服務器,要求編譯時啟用如下功能:
- SSL加密功能
- 設置Nginx賬戶及組名稱均為nginx
可選項:Nginx服務器升級到更高版本。
然後客戶端訪問頁面驗證Nginx Web服務器:
- 使用火狐瀏覽器訪問
- 使用curl訪問
1.2 方案
使用2臺RHEL6虛擬機,其中一臺作為Nginx服務器(192.168.4.5)、另外一臺作為測試用的Linux客戶機(192.168.4.100),如圖-1所示。
圖-1
安裝nginx-1.8.0版本時,需要使用如下參數:
- 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 ~]# tar -xf 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_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@svr5 ~]# /usr/local/nginx/sbin/nginx //啟動服務
- [root@svr5 ~]# /usr/local/nginx/sbin/nginx -s stop //關閉服務
- [root@svr5 ~]# /usr/local/nginx/sbin/nginx -s reload //重新加載配置文件
- [root@svr5 ~]# /usr/local/nginx/sbin/nginx –V //查看軟件信息
nginx服務默認通過TCP 80端口監聽客戶端請求:
- [root@svr5 ~]# 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 ~]# tar -zxvf nginx-1.9.0.tar.gz
- [root@svr5 ~]# cd nginx-1.9.0
- [root@svr5 nginx-1.9.0]# ./configure \
- > --prefix=/usr/local/nginx \
- > --user=nginx \
- > --group=nginx \
- > --with-http_ssl_module
- [root@svr5 nginx-1.9.0]# make
2) 備份老的nginx主程序,並使用編譯好的新版本nginx替換老版本
- [root@svr5 nginx-1.9.0]# mv /usr/local/nginx/sbin/nginx \
- >/usr/local/nginx/sbin/nginxold
- [root@svr5 nginx-1.9.0]# cp objs/nginx /usr/local/nginx/sbin/ //拷貝新版本
- [root@svr5 nginx-1.9.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.4.5
- [root@client ~]# curl http://192.168.4.5
2 案例2:用戶認證
2.1 問題
沿用練習一,通過調整Nginx服務端配置,實現以下目標:
- 訪問Web頁面需要進行用戶認證
- 用戶名為:tom,密碼為:123456
2.2 方案
通過Nginx實現Web頁面的認證,需要修改Nginx配置文件,在配置文件中添加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 "/usr/local/nginx/pass"; //認證密碼文件
- location / {
- root html;
- index index.html index.htm;
- }
- }
2)生成密碼文件,創建用戶及密碼
使用htpasswd命令創建賬戶文件,需要確保系統中已經安裝了httpd-tools。
- [root@svr5 ~]# yum -y install httpd-tools
- [root@svr5 ~]# htpasswd -cm /usr/local/nginx/pass tom //創建密碼文件
- New password:
- Re-type new password:
- Adding password for user tom
- [root@svr5 ~]# htpasswd -m /usr/local/nginx/pass jerry
- //追加用戶,不使用-c選項
- New password:
- Re-type new password:
- Adding password for user jerry
3)重啟Nginx服務
- [root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload
- //請先確保nginx是啟動狀態才可以執行命令成功,否則報錯
步驟二:客戶端測試
1)登錄192.168.4.100客戶端主機進行測試
- [root@client ~]# firefox http://192.168.4.5 //輸入密碼後可以訪問
3 案例3:基於域名的虛擬主機
3.1 問題
沿用練習二,配置基於域名的虛擬主機,實現以下目標:
- 實現兩個基於域名的虛擬主機,域名分別為www.aa.com和www.bb.com
- 對域名為www.aa.com的站點進行用戶認證,用戶名稱為tom,密碼為123456
3.2 方案
修改Nginx配置文件,添加server容器實現虛擬主機功能;對於需要進行用戶認證的虛擬主機添加auth認證語句。
3.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:修改配置文件
1)修改Nginx服務配置,添加相關虛擬主機配置如下
- [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
- .. ..
- server {
- listen 80; //端口
- server_name www.aa.com; //域名
- auth_basic "Input Password:"; //認證提示符
- auth_basic_user_file "/usr/local/nginx/pass"; //認證密碼文件
- location / {
- root html; //指定網站根路徑
- index index.html index.htm;
- }
- }
- … …
- server {
- listen 80; //端口
- server_name www.bb.com; //域名
- location / {
- root www; //指定網站根路徑
- index index.html index.htm;
- }
- }
2)創建賬戶及密碼
- [root@svr5 ~]# htpasswd –cm /usr/local/nginx/pass tom //創建賬戶密碼文件
- New password:
- Re-type new password:
- Adding password for user tom
3)創建網站根目錄及對應首頁文件
- [root@svr5 ~]# mkdir /usr/local/nginx/www
- [root@svr5 ~]# echo "www" > /usr/local/nginx/www/index.html
4)重啟nginx服務
- [root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload
步驟二:客戶端測試
1)修改客戶端主機192.168.4.100的/etc/hosts文件,進行域名解析
- [root@client ~]# vim /etc/hosts
- 192.168.4.5 www.aa.com www.bb.com
2)登錄192.168.4.100客戶端主機進行測試
註意:SSH –X遠程連接調用虛擬機的firefox時,請先關閉真實機的firefox。
- [root@client ~]# firefox http://www.aa.com //輸入密碼後可以訪問
- [root@client ~]# firefox http://www.bb.com //直接訪問
4 案例4:SSL虛擬主機
4.1 問題
沿用練習二,配置基於加密網站的虛擬主機,實現以下目標:
- 域名為www.cc.com
- 該站點通過https訪問
- 通過私鑰、證書對該站點所有數據加密
4.2 方案
源碼安裝Nginx時必須使用--with-http_ssl_module參數,啟用加密模塊,對於需要進行SSL加密處理的站點添加ssl相關指令(設置網站需要的私鑰和證書)。
4.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:配置SSL虛擬主機
1)生成私鑰與證書
- [root@svr5 ~]# cd /usr/local/nginx/conf
- [root@svr5 ~]# openssl genrsa -out cert.key //生成私鑰
- [root@svr5 ~]# openssl req -new -x509 -key cert.key -out cert.pem //生成證書
2)修改Nginx配置文件,設置加密網站的虛擬主機
- [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
- … …
- server {
- listen 443 ssl;
- server_name www.cc.com;
- ssl_certificate cert.pem;
- ssl_certificate_key cert.key;
- ssl_session_cache shared:SSL:1m;
- ssl_session_timeout 5m;
- ssl_ciphers HIGH:!aNULL:!MD5;
- ssl_prefer_server_ciphers on;
- location / {
- root html;
- index index.html index.htm;
- }
- }
步驟二:客戶端驗證
1)修改客戶端主機192.168.4.100的/etc/hosts文件,進行域名解析
- [root@client ~]# vim /etc/hosts
- 192.168.4.5 www.cc.com www.aa.com www.bb.com
2)登錄192.168.4.100客戶端主機進行測試
- [root@client ~]# firefox https://www.cc.com //信任證書後可以訪問
5 案例4:Nginx反向代理
5.1 問題
使用Nginx實現Web反向代理功能,實現如下功能:
- 後端Web服務器兩臺,可以使用httpd實現
- Nginx采用輪詢的方式調用後端Web服務器
- 兩臺Web服務器的權重要求設置為不同的值
- 最大失敗次數為1,失敗超時時間為30秒
5.2 方案
使用4臺RHEL7虛擬機,其中一臺作為Nginx代理服務器,該服務器需要配置兩塊網卡,IP地址分別為192.168.4.5和 192.168.2.5,兩臺Web服務器IP地址分別為192.168.2.100和192.168.2.200。客戶端測試主機IP地址為 192.168.4.100。如圖-2所示。
圖-2
5.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:部署實施後端Web服務器
1)部署後端Web1服務器
後端Web服務器可以簡單使用yum方式安裝httpd實現Web服務,為了可以看出後端服務器的不同,可以將兩臺後端服務器的首頁文檔內容設置為不同的內容。
- [root@web1 ~]# yum -y install httpd
- [root@web1 ~]# echo "192.168.2.100" > /var/www/html/index.html
- [root@web1 ~]# systemctl restart httpd
2)部署後端Web2服務器
- [root@web2 ~]# yum -y install httpd
- [root@web2 ~]# echo "192.168.2.200" > /var/www/html/index.html
- [root@web2 ~]# systemctl restart httpd
步驟二:配置Nginx服務器,添加服務器池,實現反向代理功能
1)修改/usr/local/nginx/conf/nginx.conf配置文件
- [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
- .. ..
- http {
- .. ..
- upstream webserver {
- server 192.168.2.100 weight=1 max_fails=2 fail_timeout=10;
- server 192.168.2.200 weight=2 max_fails=2 fail_timeout=10;
- }
- .. ..
- server {
- listen 80;
- server_name www.tarena.com;
- location / {
- proxy_pass http://webserver;
- }
- }
2)重啟nginx服務
- [root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload
3)使用瀏覽器訪問代理服務器測試輪詢效果
- [root@client ~]# curl http://192.168.4.5 //使用該命令多次訪問查看效果
步驟二:配置upstream服務器集群池屬性
1)設置失敗次數,超時時間,權重
- [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
- .. ..
- http {
- .. ..
- upstream webserver {
- server 192.168.2.100 weight=1 max_fails=2 fail_timeout=10;
- server 192.168.2.200 weight=2 max_fails=2 fail_timeout=10;
- }
- .. ..
- server {
- listen 80;
- server_name www.tarena.com;
- location / {
- proxy_pass http://webserver;
- }
- }
2)重啟nginx服務
- [root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload
3)使用瀏覽器訪問代理服務器測試輪詢效果
- [root@client ~]# curl http://192.168.4.5 //使用該命令多次訪問查看效果
4)設置相同客戶端訪問相同Web服務器
- [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
- .. ..
- http {
- .. ..
- upstream webserver {
- ip_hash;
- server 192.168.2.100 weight=1 max_fails=2 fail_timeout=10;
- server 192.168.2.200 weight=2 max_fails=2 fail_timeout=10;
- }
- .. ..
- server {
- listen 80;
- server_name www.tarena.com;
- location / {
- proxy_pass http://webserver;
- }
- }
5)重啟nginx服務
- [root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload
6)使用瀏覽器訪問代理服務器測試輪詢效果
- [root@client ~]# curl http://192.168.4.5 //使用該命令多次訪問查看效果
配置nginx,upstream服務器