1. 程式人生 > >配置nginx,upstream服務器

配置nginx,upstream服務器

gcc 失敗 進行 rep timeout 圖片 重要 ora 火狐瀏覽器

搭建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軟件包

  1. [root@svr5 ~]# yum –y install gcc pcre-devel openssl-devel //安裝常見依賴包
  2. [root@svr5 ~]# useradd –s /sbin/nologin nginx
  3. [root@svr5 ~]# tar -xf nginx-1.8.0.tar.gz
  4. [root@svr5 ~]# cd nginx-1.8.0
  5. [root@svr5 nginx-1.8.0]# ./configure \
  6. > --prefix=/usr/local/nginx \ //指定安裝路徑
  7. > --user=nginx \ //指定用戶
  8. > --group=nginx \ //指定組
  9. > --with-http_ssl_module //開啟SSL加密功能
  10. .. ..
  11. nginx path prefix: "/usr/local/nginx"
  12. nginx binary file: "/usr/local/nginx/sbin/nginx"
  13. nginx configuration prefix: "/usr/local/nginx/conf"
  14. nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  15. nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  16. nginx error log file: "/usr/local/nginx/logs/error.log"
  17. nginx http access log file: "/usr/local/nginx/logs/access.log"
  18. nginx http client request body temporary files: "client_body_temp"
  19. nginx http proxy temporary files: "proxy_temp"
  20. nginx http fastcgi temporary files: "fastcgi_temp"
  21. nginx http uwsgi temporary files: "uwsgi_temp"
  22. nginx http scgi temporary files: "scgi_temp"
  23. [root@svr5 nginx-1.7.10]# make && make install //編譯並安裝

2)nginx命令的用法

  1. [root@svr5 ~]# /usr/local/nginx/sbin/nginx //啟動服務
  2. [root@svr5 ~]# /usr/local/nginx/sbin/nginx -s stop //關閉服務
  3. [root@svr5 ~]# /usr/local/nginx/sbin/nginx -s reload //重新加載配置文件
  4. [root@svr5 ~]# /usr/local/nginx/sbin/nginx –V //查看軟件信息

nginx服務默認通過TCP 80端口監聽客戶端請求:

  1. [root@svr5 ~]# netstat -anptu | grep nginx
  2. 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的文件:

  1. [root@svr5 ~]# cat /usr/local/nginx/html/index.html
  2. <html>
  3. <head>
  4. <title>Welcome to nginx!</title>
  5. </head>
  6. <body bgcolor="white" text="black">
  7. <center><h1>Welcome to nginx!</h1></center>
  8. </body>
  9. </html>

步驟二:升級Nginx服務器

1)編譯新版本nginx軟件

  1. [root@svr5 ~]# tar -zxvf nginx-1.9.0.tar.gz
  2. [root@svr5 ~]# cd nginx-1.9.0
  3. [root@svr5 nginx-1.9.0]# ./configure \
  4. > --prefix=/usr/local/nginx \
  5. > --user=nginx \
  6. > --group=nginx \
  7. > --with-http_ssl_module
  8. [root@svr5 nginx-1.9.0]# make

2) 備份老的nginx主程序,並使用編譯好的新版本nginx替換老版本

  1. [root@svr5 nginx-1.9.0]# mv /usr/local/nginx/sbin/nginx \
  2. >/usr/local/nginx/sbin/nginxold
  3. [root@svr5 nginx-1.9.0]# cp objs/nginx /usr/local/nginx/sbin/ //拷貝新版本
  4. [root@svr5 nginx-1.9.0]# make upgrade //升級
  5. /usr/local/nginx/sbin/nginx -t
  6. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  7. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  8. kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
  9. sleep 1
  10. test -f /usr/local/nginx/logs/nginx.pid.oldbin
  11. kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
  12. [root@svr5 ~]# /usr/local/nginx/sbin/nginx –v //查看版本

步驟三:客戶端訪問測試

1)分別使用瀏覽器和命令行工具curl測試服務器頁面

  1. [root@client ~]# firefox http://192.168.4.5
  2. [root@client ~]# curl http://192.168.4.5

2 案例2:用戶認證

2.1 問題

沿用練習一,通過調整Nginx服務端配置,實現以下目標:

  1. 訪問Web頁面需要進行用戶認證
  2. 用戶名為:tom,密碼為:123456

2.2 方案

通過Nginx實現Web頁面的認證,需要修改Nginx配置文件,在配置文件中添加auth語句實現用戶認證。最後使用htpasswd命令創建用戶及密碼即可。

2.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:修改Nginx配置文件

1)修改/usr/local/nginx/conf/nginx.conf

  1. [root@pc205 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. server {
  4. listen 80;
  5. server_name localhost;
  6. auth_basic "Input Password:"; //認證提示符
  7. auth_basic_user_file "/usr/local/nginx/pass"; //認證密碼文件
  8. location / {
  9. root html;
  10. index index.html index.htm;
  11. }
  12. }

2)生成密碼文件,創建用戶及密碼

使用htpasswd命令創建賬戶文件,需要確保系統中已經安裝了httpd-tools。

  1. [root@svr5 ~]# yum -y install httpd-tools
  2. [root@svr5 ~]# htpasswd -cm /usr/local/nginx/pass tom //創建密碼文件
  3. New password:
  4. Re-type new password:
  5. Adding password for user tom
  6. [root@svr5 ~]# htpasswd -m /usr/local/nginx/pass jerry
  7. //追加用戶,不使用-c選項
  8. New password:
  9. Re-type new password:
  10. Adding password for user jerry

3)重啟Nginx服務

  1. [root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload
  2. //請先確保nginx是啟動狀態才可以執行命令成功,否則報錯

步驟二:客戶端測試

1)登錄192.168.4.100客戶端主機進行測試

  1. [root@client ~]# firefox http://192.168.4.5 //輸入密碼後可以訪問

3 案例3:基於域名的虛擬主機

3.1 問題

沿用練習二,配置基於域名的虛擬主機,實現以下目標:

  1. 實現兩個基於域名的虛擬主機,域名分別為www.aa.com和www.bb.com
  2. 對域名為www.aa.com的站點進行用戶認證,用戶名稱為tom,密碼為123456

3.2 方案

修改Nginx配置文件,添加server容器實現虛擬主機功能;對於需要進行用戶認證的虛擬主機添加auth認證語句。

3.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:修改配置文件

1)修改Nginx服務配置,添加相關虛擬主機配置如下

  1. [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. server {
  4. listen 80; //端口
  5. server_name www.aa.com; //域名
  6. auth_basic "Input Password:"; //認證提示符
  7. auth_basic_user_file "/usr/local/nginx/pass"; //認證密碼文件
  8. location / {
  9. root html; //指定網站根路徑
  10. index index.html index.htm;
  11. }
  12. }
  13. … …
  14. server {
  15. listen 80; //端口
  16. server_name www.bb.com; //域名
  17. location / {
  18. root www; //指定網站根路徑
  19. index index.html index.htm;
  20. }
  21. }

2)創建賬戶及密碼

  1. [root@svr5 ~]# htpasswd –cm /usr/local/nginx/pass tom //創建賬戶密碼文件
  2. New password:
  3. Re-type new password:
  4. Adding password for user tom

3)創建網站根目錄及對應首頁文件

  1. [root@svr5 ~]# mkdir /usr/local/nginx/www
  2. [root@svr5 ~]# echo "www" > /usr/local/nginx/www/index.html

4)重啟nginx服務

  1. [root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload

步驟二:客戶端測試

1)修改客戶端主機192.168.4.100的/etc/hosts文件,進行域名解析

  1. [root@client ~]# vim /etc/hosts
  2. 192.168.4.5 www.aa.com www.bb.com

2)登錄192.168.4.100客戶端主機進行測試

註意:SSH –X遠程連接調用虛擬機的firefox時,請先關閉真實機的firefox。

  1. [root@client ~]# firefox http://www.aa.com //輸入密碼後可以訪問
  2. [root@client ~]# firefox http://www.bb.com //直接訪問

4 案例4:SSL虛擬主機

4.1 問題

沿用練習二,配置基於加密網站的虛擬主機,實現以下目標:

  1. 域名為www.cc.com
  2. 該站點通過https訪問
  3. 通過私鑰、證書對該站點所有數據加密

4.2 方案

源碼安裝Nginx時必須使用--with-http_ssl_module參數,啟用加密模塊,對於需要進行SSL加密處理的站點添加ssl相關指令(設置網站需要的私鑰和證書)。

4.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:配置SSL虛擬主機

1)生成私鑰與證書

  1. [root@svr5 ~]# cd /usr/local/nginx/conf
  2. [root@svr5 ~]# openssl genrsa -out cert.key //生成私鑰
  3. [root@svr5 ~]# openssl req -new -x509 -key cert.key -out cert.pem //生成證書

2)修改Nginx配置文件,設置加密網站的虛擬主機

  1. [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. … …
  3. server {
  4. listen 443 ssl;
  5. server_name www.cc.com;
  6. ssl_certificate cert.pem;
  7. ssl_certificate_key cert.key;
  8. ssl_session_cache shared:SSL:1m;
  9. ssl_session_timeout 5m;
  10. ssl_ciphers HIGH:!aNULL:!MD5;
  11. ssl_prefer_server_ciphers on;
  12. location / {
  13. root html;
  14. index index.html index.htm;
  15. }
  16. }

步驟二:客戶端驗證

1)修改客戶端主機192.168.4.100的/etc/hosts文件,進行域名解析

  1. [root@client ~]# vim /etc/hosts
  2. 192.168.4.5 www.cc.com www.aa.com www.bb.com

2)登錄192.168.4.100客戶端主機進行測試

  1. [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服務,為了可以看出後端服務器的不同,可以將兩臺後端服務器的首頁文檔內容設置為不同的內容。

  1. [root@web1 ~]# yum -y install httpd
  2. [root@web1 ~]# echo "192.168.2.100" > /var/www/html/index.html
  3. [root@web1 ~]# systemctl restart httpd

2)部署後端Web2服務器

  1. [root@web2 ~]# yum -y install httpd
  2. [root@web2 ~]# echo "192.168.2.200" > /var/www/html/index.html
  3. [root@web2 ~]# systemctl restart httpd

步驟二:配置Nginx服務器,添加服務器池,實現反向代理功能

1)修改/usr/local/nginx/conf/nginx.conf配置文件

  1. [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. http {
  4. .. ..
  5. upstream webserver {
  6. server 192.168.2.100 weight=1 max_fails=2 fail_timeout=10;
  7. server 192.168.2.200 weight=2 max_fails=2 fail_timeout=10;
  8. }
  9. .. ..
  10. server {
  11. listen 80;
  12. server_name www.tarena.com;
  13. location / {
  14. proxy_pass http://webserver;
  15. }
  16. }

2)重啟nginx服務

  1. [root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload

3)使用瀏覽器訪問代理服務器測試輪詢效果

  1. [root@client ~]# curl http://192.168.4.5 //使用該命令多次訪問查看效果

步驟二:配置upstream服務器集群池屬性

1)設置失敗次數,超時時間,權重

  1. [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. http {
  4. .. ..
  5. upstream webserver {
  6. server 192.168.2.100 weight=1 max_fails=2 fail_timeout=10;
  7. server 192.168.2.200 weight=2 max_fails=2 fail_timeout=10;
  8. }
  9. .. ..
  10. server {
  11. listen 80;
  12. server_name www.tarena.com;
  13. location / {
  14. proxy_pass http://webserver;
  15. }
  16. }

2)重啟nginx服務

  1. [root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload

3)使用瀏覽器訪問代理服務器測試輪詢效果

  1. [root@client ~]# curl http://192.168.4.5 //使用該命令多次訪問查看效果

4)設置相同客戶端訪問相同Web服務器

  1. [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. http {
  4. .. ..
  5. upstream webserver {
  6. ip_hash;
  7. server 192.168.2.100 weight=1 max_fails=2 fail_timeout=10;
  8. server 192.168.2.200 weight=2 max_fails=2 fail_timeout=10;
  9. }
  10. .. ..
  11. server {
  12. listen 80;
  13. server_name www.tarena.com;
  14. location / {
  15. proxy_pass http://webserver;
  16. }
  17. }

5)重啟nginx服務

  1. [root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload

6)使用瀏覽器訪問代理服務器測試輪詢效果

  1. [root@client ~]# curl http://192.168.4.5 //使用該命令多次訪問查看效果

配置nginx,upstream服務器