1. 程式人生 > >Nginx 輕鬆學 圖文並茂 一學就會 附案例原始碼

Nginx 輕鬆學 圖文並茂 一學就會 附案例原始碼

導讀

  篇幅較長,乾貨滿滿,需花費較長時間,轉載請註明出處!

Nginx概述

簡介

  Nginx (engine x) 是一個高效能的HTTP和反向代理web伺服器,同時也提供了IMAP/POP3/SMTP服務。Nginx是由伊戈爾·賽索耶夫為俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發的,第一個公開版本0.1.0釋出於2004年10月4日。

  Nginx是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,在BSD-like 協議下發行。其特點是佔有記憶體少,併發能力強,事實上nginx的併發能力在同類型的網頁伺服器中表現較好,中國大陸使用nginx網站使用者有:百度、京東、新浪、網易、騰訊、淘寶等。

代理伺服器

  代理伺服器根據其代理物件的不同,可以分為正向代理伺服器與反向代理伺服器。這裡的“正”與“反”均是站在客戶端角度來說的。

正向代理

  正向代理是對客戶端的代理。客戶端C想要從服務端S獲取資源,但由於某些原因不能直接訪問服務端,而是通過另外一臺主機P向服務端傳送請求。當服務端處理完畢請求後,將響應傳送給主機P,主機P在接收到來自服務端的響應後,將響應又轉給了客戶端C。此時的主機P,就稱為客戶端C的正向代理伺服器。

  客戶端在使用正向代理伺服器時是知道其要訪問的目標伺服器的地址等資訊的。

  正向代理伺服器是伺服器的使用者(客戶端)架設的主機,與伺服器無關,正向代理伺服器的出現,使服務端根本就不知道真正客戶端的存在。

反向代理

  反向代理,其實客戶端對代理是無感知的,因為客戶端不需要任何配置就可以訪問,我們只需要將請求傳送到反向代理伺服器,由反向代理伺服器去選擇目標伺服器獲取資料後,在將響應返回給客戶端,此時反向代理伺服器和目標伺服器對外就是一個伺服器,暴露的是代理伺服器地址,隱藏了真實伺服器IP地址。

兩者區別

  在知乎上找了2張圖,可以幫助我們更好的理解。

 

Nginx的特點

  • 高併發
  • 低消耗
  • 熱部署
  • 高擴充套件
  • 高可用

Nginx的web請求處理機制

  Nginx結合多程序機制和非同步機制對外提供服務,非同步機制使用的是非同步非阻塞方式。Nginx的master程序會生成多個worker程序,master程序負責管理這些worker程序的生命週期、接受外部命令、解析perl指令碼等。而worker程序則用於接受和處理客戶端請求。

  每個worker程序能夠使用非同步非阻塞方式處理多個客戶端請求。當某個worker程序接收到客戶端的請求後,會呼叫IO程序處理,如果不能立即得到結果,worker程序就去處理其他的請求。當IO返回結果後,就會通知worker程序,而worker程序得到通知後,就會掛起當前正在處理的事務,拿IO返回結果去響應客戶端請求,worker程序採用的是epoll事件驅動模型與IO進行通訊的。epoll模型底層採用的是“回撥callback”代替裡輪詢,使效率高於select模型。

Nginx的下載與安裝

Nginx的下載

nginx的官網:http://nginx.org/

  注:主線版,是最新的版本;穩定版,推薦生產環境下使用;舊版,以前的版本。

百度雲盤地址

連結:https://pan.baidu.com/s/1kjQST_x1Sf_thg3XDmqx6w  密碼:18sc

將nginx上傳至linux

環境搭建

前期準備

  因為nginx是C語言寫的,而且是原始碼安裝,安裝前需安裝C語言環境!!!

yum install -y gcc-c++ gcc

安裝依賴庫

  基本的Nginx功能依賴於一些基本的庫,在安裝Nginx之前需要提前安裝這些庫。

  pcre-devel:pcre,Perl Compatible Regular Expressions,Perl指令碼語言相容正則表示式,為Nginx提供正則表示式庫。

  openssl-devel:為Nginx提供SSL(安全套接字層)密碼庫,包含主要的密碼演算法,常用的金鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程式供測試或其他目的使用。

  在安裝之前需要注意,很多庫具有devel庫與非devel庫兩種。devel庫表示development開發庫,比非devel庫會多出一些標頭檔案、靜態庫、原始碼包等。而這些包在執行時不可能用到,但在開發時有可能用到。所以對於程式設計師來說,一般都是需要安裝devel庫的。不過在yum安裝devel庫時,由於其依賴於非devel庫,所以其會先自動安裝非devel庫,而後再安裝devel庫。所以真正安裝時,只需顯示的安裝devel庫即可。

 yum -y install pcre-devel openssl-devel

解壓Nginx包

tar -zxvf nginx-1.16.1.tar.gz -C /opt/apps

進入剛才解壓後的目錄

檢視幫助(可忽略)

./configure --help

安裝模組

此時Nginx解壓路徑下會多出一個:Makefile

系統配置資訊

  • path prefix:Nginx安裝目錄
  • binary file:Nginx命令檔案
  • modules path:Nginx模組存放路徑
  • configuration prefix:Nginx配置檔案存放路徑
  • configuration file:Nginx配置檔名
  • pid file:Nginx的程序id檔案
  • error log file:錯誤日誌檔案
  • http access log file:http訪問日誌檔案
  • http xxx:其他http請求相關的檔案

  配置成功後,再次檢視Nginx解壓目錄,發現其中多出一個檔案Makefile。後面的編譯就是依靠該檔案進行的。

編譯安裝

  這是兩個命令,make:為編譯命令;make install:為安裝命令,可以分別執行。這裡使用&&將兩個命令連線執行,會在前面命令執行成功的前提下才會執行第二個命令。

編譯安裝完成後,會在/usr/local下出現nginx目錄

進入安裝目錄

設定任務目錄可以使用sbin(可忽略)

ln -n /usr/local/nginx/sbin/nginx /usr/local/sbin

格式:
ln -n 源路徑 目標路徑

注:軟連結建立完成後就可以在任意路徑下使用nginx命令

Nginx命令

  • 檢視命令選項:nginx -h
  • 啟動命令:nginx -c file
  • 停止命令:nginx -s stop/quit
  • 平滑重啟命令:nginx -s reload
  • 測試配置檔案命令:nginx -tq

nginx -t:測試配置檔案是否正確,預設只測試預設的配置檔案conf/nginx.conf

nginx -T:測試配置檔案是否正確,並顯示配置檔案內容

nginx -tq:在配置檔案測試過程中,禁止顯示非錯誤資訊,即只顯示錯誤資訊

啟動nginx

  啟動後,並檢視,發現有2個執行緒,一個master,一個worker

檢視nginx埠號

nginx頁面訪問測試

注:linux需關閉防火牆!!!!

Nginx效能調優

檢視nginx配置檔案

nginx程序

  worker_processes,工作程序,用於指定Nginx的工作程序數量。該值應該設定為多少合適呢?其數值一般設定為cpu核心數量,或核心數量的整數倍。注意,現代的cpu一般都是多核,即一塊cpu中包含多個核心。

  若當前系統具有2塊cpu,而每塊cpu中包含2個核心,那麼worker_processes的值一般可以設定為4或8個。當然也可以為2個。

  不過需要注意,該值不僅僅取決於cpu核心數量,還與硬碟數量及負載均衡模式相關,在不確定時還可以指定其值為auto。

worker_cpu_affinity的設定

  為了進一步提高系統性能,我們會將worker程序與具體的核心進行繫結。該繫結操作是通過worker_cpu_affinity屬性進行設定的。affinity,密切關係。

  不過,若指定worker_processes的值為auto,則無法設定worker_cpu_affinity。該設定是通過二進位制進行的。每個核心使用一個二進位制表示,0代表核心關閉;1代表核心開啟。也就是說,有幾個核心,就需要使用幾個二進位制位。

  下面通過幾個例子來增進對worker_processes與worker_cpu_affinity的理解  

Nginx核心功能

請求定位

探究歡迎頁面顯示原因

訪問資源

檢視nginx.conf配置

使用perl語言自定義一個網站

檢測語法格式是否正確

重啟nginx

訪問成功

  注:字打錯了,應該是“歡迎來到陳彥斌的Nginx!!”

靜態代理

  Nginx靜態代理是指,將所有的靜態資源,如:css、js、html、jpg等資源存放到Nginx伺服器,而不存在應用伺服器Tomcat中,當客戶端發出的請求是對這些靜態資源的請求時,Nginx直接將這些靜態資源響應給客戶端,而無需提交給應用伺服器處理。這樣就降低了應用伺服器的壓力。

  同時,Nginx對於靜態資源的處理較Tomcat,效能更高,效率更高。所以,在實際生產環境下,會使用Nginx作為靜態代理伺服器,專門處理靜態資源的響應。

  Nginx對於靜態資源請求的攔截方式,可以通過靜態資源名稱的副檔名攔截,也可以通過靜態資源所在的目錄名稱攔截。

建立目錄存放檔案

往images目錄下存放一些圖片

編輯nginx.conf配置檔案

副檔名攔截

副檔名攔截測試

目錄名攔截

負載均衡

   負載均衡,Load Balancing,就是將對請求的處理分攤到多個操作單元上進行。這個均衡是指在大批量訪問前提下的一種基本均衡,並非是絕對的平均。

  對於Web工程中的負載均衡,就是將相同的Web應用部署到多個不同的Web伺服器上,形成多個Web應用伺服器。當請求到來時,由負載均衡伺服器負責將請求按照事先設定好的比例向Web應用伺服器進行分發,從而增加系統的整體吞吐量。

總體規劃

  該機群包含一臺Nginx伺服器,兩臺Tomat伺服器。

  首先開發一個web工程,將其打包。然後,在克隆出兩臺Tomcat主機,前面的web工程分別部署到這兩臺Tomcat主機上。然後,在Nginx伺服器上設定對這兩臺Tomcat主機的均在均衡。 

配置說明

  1. tomcat伺服器1:ip地址192.168.31.213
  2. tomcat伺服器2:ip地址192.168.31.214
  3. nginx伺服器:ip地址192.168.31.201

建立一個Web工程

專案結構圖

配置一臺Tomcat主機

具體配置,請參考另一篇部落格:點我直達

複製並配置另一臺Tomcat主機

   克隆上面配置好的Tomcat主機

配置Nginx主機

配置nginx.conf

  注:weight權重

平滑重啟nginx

專案的啟動與訪問

專案下載原始碼

jsp專案
百度雲盤
連結:https://pan.baidu.com/s/18z5c93jM6S-qvoT2cUZi7g  密碼:u08i

動靜分離

簡介

  動靜分離,就是將JSP、Servlet等動態資源交由Tomcat或其他Web伺服器處理,將CSS、js、image等靜態資源交由Nginx或其他Http伺服器處理,充分發揮各自的優勢,減輕其他伺服器的壓力,搭建更為高效的系統架構。

Nginx動靜分析的實現

  下面要搭建Nginx,環境中有三臺Nginx主機;一臺用於完成負載均衡,兩臺Nginx用於存放前面專案中的靜態資源。另外,還包含前面的兩臺Tomcat主機。

複製並配置一臺Nginx伺服器

  將原來安裝有Nginx的主機作為母機,克隆一臺Nginx主機,用於存放靜態資源:css、js、image。

修改nginx.conf

存放靜態資源

複製並配置一臺Nginx伺服器

  重複上面,複製的那臺Nginx操作!!!

修改原始那臺nginx配置檔案(負載均衡,重要!!!)

  設定靜態資源的負載均衡代理,兩臺nginx的ip分別為:192.168.31.212、192.168.31.213

 

web工程

 

測試

由於圖片太大,截成2段了

linux搭建相關配置

伺服器配置資訊

  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33     # 配置上傳流,用於負載均衡
 34     upstream tomcat.cyb.com{
 35     server 192.168.31.214:8080 weight=1;
 36         server 192.168.31.215:8080 weight=1;
 37     }
 38     upstream static.cyb.com{
 39     server 192.168.31.212:80 weight=1;
 40         server 192.168.31.213:80 weight=1;
 41     }
 42     server {
 43         listen       80;
 44         server_name  localhost;
 45 
 46         #charset koi8-r;
 47 
 48         #access_log  logs/host.access.log  main;
 49 
 50         location / {
 51            # root   html;
 52            # index  index.html index.htm;
 53             proxy_pass http://tomcat.cyb.com;
 54         }
 55         location ~.*\.(jpg|jpeg|js|css|html)$ {
 56             proxy_pass http://static.cyb.com; 
 57        }
 58         #error_page  404              /404.html;
 59 
 60         # redirect server error pages to the static page /50x.html
 61         #
 62         error_page   500 502 503 504  /50x.html;
 63         location = /50x.html {
 64             root   html;
 65         }
 66 
 67         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 68         #
 69         #location ~ \.php$ {
 70         #    proxy_pass   http://127.0.0.1;
 71         #}
 72 
 73         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 74         #
 75         #location ~ \.php$ {
 76         #    root           html;
 77         #    fastcgi_pass   127.0.0.1:9000;
 78         #    fastcgi_index  index.php;
 79         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 80         #    include        fastcgi_params;
 81         #}
 82 
 83         # deny access to .htaccess files, if Apache's document root
 84         # concurs with nginx's one
 85         #
 86         #location ~ /\.ht {
 87         #    deny  all;
 88         #}
 89     }
 90 
 91 
 92     # another virtual host using mix of IP-, name-, and port-based configuration
 93     #
 94     #server {
 95     #    listen       8000;
 96     #    listen       somename:8080;
 97     #    server_name  somename  alias  another.alias;
 98 
 99     #    location / {
100     #        root   html;
101     #        index  index.html index.htm;
102     #    }
103     #}
104 
105 
106     # HTTPS server
107     #
108     #server {
109     #    listen       443 ssl;
110     #    server_name  localhost;
111 
112     #    ssl_certificate      cert.pem;
113     #    ssl_certificate_key  cert.key;
114 
115     #    ssl_session_cache    shared:SSL:1m;
116     #    ssl_session_timeout  5m;
117 
118     #    ssl_ciphers  HIGH:!aNULL:!MD5;
119     #    ssl_prefer_server_ciphers  on;
120 
121     #    location / {
122     #        root   html;
123     #        index  index.html index.htm;
124     #    }
125     #}
126 
127 }
nginx-1
  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 
 34     server {
 35         listen       80;
 36         server_name  localhost;
 37 
 38         #charset koi8-r;
 39 
 40         #access_log  logs/host.access.log  main;
 41 
 42        # location / {
 43        #     root   html;
 44        #     index  index.html index.htm;
 45        # }
 46         # 通過副檔名方式攔截
 47     location ~.*\.(jpg|jpeg|js|css|html)$ {
 48         root /opt;
 49     }
 50 
 51         #error_page  404              /404.html;
 52 
 53         # redirect server error pages to the static page /50x.html
 54         #
 55         error_page   500 502 503 504  /50x.html;
 56         location = /50x.html {
 57             root   html;
 58         }
 59 
 60         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 61         #
 62         #location ~ \.php$ {
 63         #    proxy_pass   http://127.0.0.1;
 64         #}
 65 
 66         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 67         #
 68         #location ~ \.php$ {
 69         #    root           html;
 70         #    fastcgi_pass   127.0.0.1:9000;
 71         #    fastcgi_index  index.php;
 72         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 73         #    include        fastcgi_params;
 74         #}
 75 
 76         # deny access to .htaccess files, if Apache's document root
 77         # concurs with nginx's one
 78         #
 79         #location ~ /\.ht {
 80         #    deny  all;
 81         #}
 82     }
 83 
 84 
 85     # another virtual host using mix of IP-, name-, and port-based configuration
 86     #
 87     #server {
 88     #    listen       8000;
 89     #    listen       somename:8080;
 90     #    server_name  somename  alias  another.alias;
 91 
 92     #    location / {
 93     #        root   html;
 94     #        index  index.html index.htm;
 95     #    }
 96     #}
 97 
 98 
 99     # HTTPS server
100     #
101     #server {
102     #    listen       443 ssl;
103     #    server_name  localhost;
104 
105     #    ssl_certificate      cert.pem;
106     #    ssl_certificate_key  cert.key;
107 
108     #    ssl_session_cache    shared:SSL:1m;
109     #    ssl_session_timeout  5m;
110 
111     #    ssl_ciphers  HIGH:!aNULL:!MD5;
112     #    ssl_prefer_server_ciphers  on;
113 
114     #    location / {
115     #        root   html;
116     #        index  index.html index.htm;
117     #    }
118     #}
119 
120 }
nginx-2
  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 
 34     server {
 35         listen       80;
 36         server_name  localhost;
 37 
 38         #charset koi8-r;
 39 
 40         #access_log  logs/host.access.log  main;
 41 
 42        # location / {
 43        #     root   html;
 44        #     index  index.html index.htm;
 45        # }
 46         # 通過副檔名方式攔截
 47     location ~.*\.(jpg|jpeg|js|css|html)$ {
 48         root /opt;
 49     }
 50 
 51         #error_page  404              /404.html;
 52 
 53         # redirect server error pages to the static page /50x.html
 54         #
 55         error_page   500 502 503 504  /50x.html;
 56         location = /50x.html {
 57             root   html;
 58         }
 59 
 60         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 61         #
 62         #location ~ \.php$ {
 63         #    proxy_pass   http://127.0.0.1;
 64         #}
 65 
 66         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 67         #
 68         #location ~ \.php$ {
 69         #    root           html;
 70         #    fastcgi_pass   127.0.0.1:9000;
 71         #    fastcgi_index  index.php;
 72         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 73         #    include        fastcgi_params;
 74         #}
 75 
 76         # deny access to .htaccess files, if Apache's document root
 77         # concurs with nginx's one
 78         #
 79         #location ~ /\.ht {
 80         #    deny  all;
 81         #}
 82     }
 83 
 84 
 85     # another virtual host using mix of IP-, name-, and port-based configuration
 86     #
 87     #server {
 88     #    listen       8000;
 89     #    listen       somename:8080;
 90     #    server_name  somename  alias  another.alias;
 91 
 92     #    location / {
 93     #        root   html;
 94     #        index  index.html index.htm;
 95     #    }
 96     #}
 97 
 98 
 99     # HTTPS server
100     #
101     #server {
102     #    listen       443 ssl;
103     #    server_name  localhost;
104 
105     #    ssl_certificate      cert.pem;
106     #    ssl_certificate_key  cert.key;
107 
108     #    ssl_session_cache    shared:SSL:1m;
109     #    ssl_session_timeout  5m;
110 
111     #    ssl_ciphers  HIGH:!aNULL:!MD5;
112     #    ssl_prefer_server_ciphers  on;
113 
114     #    location / {
115     #        root   html;
116     #        index  index.html index.htm;
117     #    }
118     #}
119 
120 }
nginx-3

虛擬主機

簡介

  虛擬主機,就是將一臺物理伺服器虛擬為多個伺服器來使用,從而實現在一臺伺服器上配置多個站點,即可以在一臺物理主機上配置多個域名。Nginx中,一個server標籤就是一臺虛擬主機,配置多個server標籤就虛擬出了多臺主機。

  Nginx虛擬主機的實現方式有兩種:域名虛擬方式與埠虛擬方式。域名虛擬方式是指不同的虛擬機器使用不同的域名,通過不同的域名虛擬出不同的主機;埠虛擬方式是指不同的虛擬機器使用相同的域名不同的埠號,通過不同的埠號虛擬出不同的主機。基於埠的虛擬方式不常用。。。

規劃

  現在很多生活服務類網路平臺都具有這樣的功能:不同城市的使用者可以開啟不同城市專屬的站點。使用者首先開啟的是平臺總的站點,然後允許使用者切換到不同的城市。其實,不同的城市都是一個不同的站點。

  這裡我們要實現的功能是為平臺總站點、北京、上海兩個城市站點分別建立一個虛擬主機。每個虛擬主機都具有兩臺Tomcat的負載均衡主機。由於有三個站點,所以共需六臺Tomcat主機。

  首先要建立一個web工程,其中就一個index.jsp頁面,頁面除了顯示當前城市外,還要顯示城市切換的超連結。為了能夠明細的區分出當前訪問的Tomcat,在頁面中顯示出客戶端ip和伺服器ip。

規劃圖

注:字打錯了,北京應該是:bj.cyb.com

建立web工程

  直接複製前面的web工程,只需要一個jsp即可。

war包

總站

北京

上海 

 

專案原始碼下載 

百度雲盤
連結:https://pan.baidu.com/s/14n1tluHohe5Il_9vi1w9SA  密碼:n2sc

修改本機hosts檔案

  修改hosts檔案,域名繫結ip(繫結的是nginx負載均衡的ip地址!!),不能直接修改hosts檔案,需要複製出來後,修改完畢後,在覆蓋回去

mac方式

操作步驟:訪達->shift+command+G->/private/etc

windows方式

檔案位置:C:\Windows\System32\Drivers\etc

修改hosts檔案

 

叢集

結構圖

 

nginx負載均衡nginx.conf

 

nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

    keepalive_timeout  65;

    #gzip  on;
     # 總站
    upstream entry.com{
           server 192.168.1.111:8080 weight=1;
         server 192.168.1.112:8080 weight=1;
     }
     #北京
    upstream bj.com{
           server 192.168.1.113:8080 weight=1;
         server 192.168.1.114:8080 weight=1;
     }
     #上海
    upstream sh.com{
           server 192.168.1.115:8080 weight=1;
         server 192.168.1.116:8080 weight=1;
     }
    server{
        listen       80;
        server_name  www.entry.com;
           # 負載均衡
        location / {
        proxy_pass http://entry.com;
    }
     }
    server{
        listen       80;
        server_name  bj.entry.com;
           # 負載均衡
        location / {
        proxy_pass http://bj.com;
    }
     }
    server{
        listen       80;
        server_name  sh.entry.com;
           # 負載均衡
        location / {
        proxy_pass http://sh.com;
    }
     }
}

測試(虛擬主機&負載均衡)

搞定!!!!