lb-01:192.168.33.135 nginx-lb centos7 rs-01:192.168.33.131 apache-web centos6.x rs-02:192.168.33.132 nginx-web centos6.x
lb-01 安裝nginx,配置nginx源php
# cat /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
yum安裝css
[root@lb-01 ~]# yum install nginx -y
啓動html
[root@lb-01 ~]# systemctl start nginx.service
測試nginx
[root@lb-01 ~]# curl -I localhost HTTP/1.1 200 OK Server: nginx/1.10.1 Date: Tue, 28 Jun 2016 21:17:48 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 31 May 2016 14:09:55 GMT Connection: keep-alive ETag: "574d9b33-264" Accept-Ranges: bytes
其餘rs機器rs-01安裝apache使用默認yum安裝便可,rs-02安裝nginx
rs-01安裝web
# yum install httpd -y
rs-02安裝正則表達式
# yum install nginx -y
rs-01 web訪問結果算法
[root@rs-01 ~]# cat /var/www/html/index.html <h1>this is rs-01 ip 192.168.33.131 </h1> [root@rs-01 ~]# curl localhost <h1>this is rs-01 ip 192.168.33.131 </h1>
rs-02 web訪問結果apache
[root@rs-02 ~]# cat /usr/share/nginx/html/index.html <h1>this is rs-02 ip 192.168.33.132 </h1> [root@rs-02 ~]# curl localhost <h1>this is rs-02 ip 192.168.33.132 </h1>
[root@lb-01 ~]# cat /etc/nginx/conf.d/upstream.conf upstream blog { server 192.168.33.131:80 weight=3; server 192.168.33.132:80 weight=3; server 192.168.33.133:81 weight=3; } server { listen 80; server_name blog.zxl.com; location / { proxy_pass http://blog; } }
upsteam池中端口不寫也不要緊默認是80,若是是別的端口能夠填寫相關端口便可。upsteam必須在http{}
標籤內,weight
權重。upstream.conf配置文件放在include /etc/nginx/conf.d/*.conf
目錄下,官方地址http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream後端
upstream模塊經常使用的指令有:centos
ip_hash:基於客戶端IP地址完成請求的分發,它能夠保證來自於同一個客戶端的請求始終被轉發至同一個upstream服務器; keepalive:每一個worker進程爲發送到upstream服務器的鏈接所緩存的個數; least_conn:最少鏈接調度算法; server:定義一個upstream服務器的地址,還可包括一系列可選參數,如: weight:權重;默認1 max_fails:最大失敗鏈接次數,失敗鏈接的超時時長由fail_timeout指定;默認1,0則爲禁止失敗嘗試 fail_timeout:等待請求的目標服務器發送響應的時長;默認10s backup:用於fallback的目的,全部服務均故障時才啓動此服務器; down:手動標記其再也不處理任何請求;
[root@lb-01 ~]# tail -n 1 /etc/hosts 192.168.33.135 blog.zxl.com
[root@lb-01 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb-01 ~]# nginx -s reload
[root@lb-01 ~]# curl http://blog.zxl.com <h1>this is rs-02 ip 192.168.33.132 </h1> [root@lb-01 ~]# curl http://blog.zxl.com <h1>this is rs-01 ip 192.168.33.131 </h1> [root@lb-01 ~]# curl http://blog.zxl.com <h1>this is rs-02 ip 192.168.33.132 </h1> [root@lb-01 ~]# curl http://blog.zxl.com <h1>this is rs-01 ip 192.168.33.131 </h1>
注:爲何沒有訪問33.131:81服務,由於我沒有這臺機器假若有的話,沒有81端口。nginx自己自帶健康檢查,有問題的話會自帶剔除
[root@lb-01 ~]# curl http://blog.zxl.com <h1>this is rs-01 ip 192.168.33.131 </h1> [root@lb-01 ~]# curl http://blog.zxl.com <h1>this is rs-01 ip 192.168.33.131 </h1> [root@lb-01 ~]# curl http://blog.zxl.com <h1>this is rs-01 ip 192.168.33.131 </h1>
注:能夠看到訪問一直都是後端rs-01服務
注意:若是upstream
中使用ip_hash
算法,那麼不能使用weight和backup
參數
nginx核心模塊 http://nginx.org/en/docs/http/ngx_http_core_module.html
location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } 功能:容許根據用戶請求的URI來匹配指定的各location以進行訪問配置;匹配到時,將被location塊中的配置所處理;好比:http://www.zxl.com/p_w_picpaths/logo.gif =:精確匹配; ~:正則表達式模式匹配,匹配時區分字符大小寫 ~*:正則表達式模式匹配,匹配時忽略字符大小寫 ^~: URI前半部分匹配,不檢查正則表達式
$uri: 當前請求的uri,不帶參數; $request_uri: 請求的uri,帶完整參數; $host: http請求報文中host首部;若是請求中沒有host首部,則以處理此請求的虛擬主機的主機名代替; $hostname: nginx服務運行在的主機的主機名; $remote_addr: 客戶端IP $remote_port: 客戶端Port $remote_user: 使用用戶認證時客戶端用戶輸入的用戶名; $request_filename: 用戶請求中的URI通過本地root或alias轉換後映射的本地的文件路徑; $request_method: 請求方法 $server_addr: 服務器地址 $server_name: 服務器名稱 $server_port: 服務器端口 $server_protocol: 服務器向客戶端發送響應時的協議,如http/1.1, http/1.0 $scheme: 在請求中使用scheme, 如https://www.magedu.com/中的https; $http_HEADER: 匹配請求報文中指定的HEADER,$http_host匹配請求報文中的host首部 $sent_http_HEADER: 匹配響應報文中指定的HEADER,例如$http_content_type匹配響應報文中的content-type首部; $document_root:當前請求映射到的root配置;
分別配置三個虛擬主機域名爲 www.zxl.com bbs.zxl.com blog.zxl.com
[root@rs-01 conf]# tail -n 18 httpd.conf <VirtualHost *:80> DocumentRoot /var/www/www ServerName www.zxl.com ErrorLog logs/www.zxl.com-error_log CustomLog logs/www.zxl.com-access_log common </VirtualHost> <VirtualHost *:80> DocumentRoot /var/www/bbs ServerName bbs.zxl.com ErrorLog logs/bbs.zxl.com-error_log CustomLog logs/bbs.zxl.com-access_log common </VirtualHost> <VirtualHost *:80> DocumentRoot /var/www/blog ServerName blog.zxl.com ErrorLog logs/blog.zxl.com-error_log CustomLog logs/blog.zxl.com-access_log common </VirtualHost>
注:apache配置虛擬主機時把#NameVirtualHost :80註釋須要打開*
建立虛擬主機站點目錄
[root@rs-01 httpd]# mkdir /var/www/{www,bbs,blog} -pv mkdir: created directory `/var/www/www' mkdir: created directory `/var/www/bbs' mkdir: created directory `/var/www/blog'
rs-01節點配置hosts文件
[root@rs-01 ~]# tail -n 1 /etc/hosts 192.168.33.131 www.zxl.com 192.168.33.131 blog.zxl.com 192.168.33.131 bbs.zxl.com
站點文件內容
[root@rs-01 ~]# cat /var/www/www/index.html apache www 131 [root@rs-01 ~]# cat /var/www/bbs/index.html apache bbs 131 [root@rs-01 ~]# cat /var/www/blog/index.html apache blog 131
使用curl訪問測試
[root@rs-01 ~]# curl www.zxl.com apache www 131 [root@rs-01 ~]# curl blog.zxl.com apache blog 131 [root@rs-01 ~]# curl bbs.zxl.com apache bbs 131
rs-02節點wwww虛擬主機配置文件以下
[root@rs-02 conf.d]# cat www.conf server { listen 80; server_name www.zxl.com; location / { index index.html index.htm; root /usr/share/nginx/www; } }
rs-02節點bbs虛擬主機配置文件以下
[root@rs-02 conf.d]# cat bbs.conf server { listen 80; server_name bbs.zxl.com; location / { index index.html index.htm; root /usr/share/nginx/bbs; } }
rs-02節點blog虛擬主機配置文件以下
[root@rs-02 conf.d]# cat blog.conf server { listen 80; server_name blog.zxl.com; location / { index index.html index.htm; root /usr/share/nginx/blog; } }
檢查rs-02節點配置文件語法以及從新加載
[root@rs-02 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@rs-02 conf.d]# nginx -s reload
建立站點配置文件
[root@rs-02 conf.d]# mkdir /usr/share/nginx/{www,bbs,blog} -pv mkdir: created directory `/usr/share/nginx/www' mkdir: created directory `/usr/share/nginx/bbs' mkdir: created directory `/usr/share/nginx/blog'
建立站點內容
[root@rs-02 ~]# cat /usr/share/nginx/www/index.html nginx www 132 [root@rs-02 ~]# cat /usr/share/nginx/bbs/index.html nginx bbs 132 [root@rs-02 ~]# cat /usr/share/nginx/blog/index.html nginx blog 132
rs-02節點配置hosts文件
[root@rs-02 conf.d]# tail -n 1 /etc/hosts 192.168.33.132 www.zxl.com 192.168.33.132 blog.zxl.com 192.168.33.132 bbs.zxl.com
rs-02節點進行測試訪問
[root@rs-02 ~]# curl www.zxl.com nginx www 132 [root@rs-02 ~]# curl bbs.zxl.com nginx bbs 132 [root@rs-02 ~]# curl blog.zxl.com nginx blog 132
配置hosts文件
[root@lb-01 ~]# tail -n 3 /etc/hosts 192.168.33.135 www.zxl.com 192.168.33.135 blog.zxl.com 192.168.33.135 bbs.zxl.com
訪問blog.zxl.com
[root@lb-01 conf.d]# curl blog.zxl.com apache www 131 [root@lb-01 conf.d]# curl blog.zxl.com nginx blog 132
注:爲何會出現和預期的不同呢?由於lb根本不知道該去找個節點,由於後端都是80端口。lb須要進行參數設置
proxy_set_header Host $host ;獲取後端head的信息,代理那個後端主機,完整示例以下
[root@lb-01 conf.d]# cat upstream.conf upstream blog { server 192.168.33.131:80 weight=3; server 192.168.33.132:80 weight=3; } server { listen 80; server_name blog.zxl.com; location / { proxy_pass http://blog; proxy_set_header Host $host; } }
其實就是在location中添加了proxy_set_header Host $host;
檢查lb上nginx語法以及從新加載並測試
[root@lb-01 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb-01 conf.d]# nginx -s reload
結果就是能夠rr權重了
[root@lb-01 conf.d]# curl blog.zxl.com apache blog 131 [root@lb-01 conf.d]# curl blog.zxl.com nginx blog 132
bbs和www和blog相似,完整示例以下
[root@lb-01 conf.d]# cat upstream.conf upstream blog { server 192.168.33.131:80 weight=3; server 192.168.33.132:80 weight=3; } server { listen 80; server_name blog.zxl.com; location / { proxy_pass http://blog; proxy_set_header Host $host; } } server { listen 80; server_name bbs.zxl.com; location / { proxy_pass http://blog; proxy_set_header Host $host; } } server { listen 80; server_name www.zxl.com; location / { proxy_pass http://blog; proxy_set_header Host $host; } }
注:bbs和www以及blog使用相同的upstream池,由於後端節點都是同樣的
檢查語法以及測試
[root@lb-01 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb-01 conf.d]# nginx -s reload
能夠看到每一個站點都是rr輪詢訪問的
[root@lb-01 conf.d]# curl www.zxl.com apache www 131 [root@lb-01 conf.d]# curl www.zxl.com nginx www 132 [root@lb-01 conf.d]# curl bbs.zxl.com apache bbs 131 [root@lb-01 conf.d]# curl bbs.zxl.com nginx bbs 132
後端節點如何查看來自客戶端訪問的真實ip地址?看的話只能從日誌看了,因此設置好後端節點的日誌,apache上面定義虛擬主機的時候已經配置好了,下面配置nginx節點的日誌,示例以下
rs-02後端節點bbs虛擬主機log日誌
[root@rs-02 conf.d]# cat bbs.conf server { listen 80; server_name bbs.zxl.com; location / { index index.html index.htm; root /usr/share/nginx/bbs; access_log logs/bbs.access.log main; } }
rs-02後端節點blog虛擬主機log日誌
[root@rs-02 conf.d]# cat blog.conf server { listen 80; server_name blog.zxl.com; location / { index index.html index.htm; root /usr/share/nginx/blog; access_log logs/blog.access.log main; } }
rs-02後端節點www虛擬主機log日誌
[root@rs-02 conf.d]# cat www.conf server { listen 80; server_name www.zxl.com; location / { index index.html index.htm; root /usr/share/nginx/www; access_log logs/www.access.log main; } }
注:access__log添加字段是記錄log日誌的
建立rs-02節點虛擬主機記錄log日誌的目錄
[root@rs-02 conf.d]# mkdir /usr/share/nginx/logs
檢查語法以及從新加載
[root@rs-02 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@rs-02 conf.d]# nginx -s reload
在本身電腦上設置hosts文件綁定域名進行訪問,而後查看後端節點日誌看看是來自哪一個ip地址?C:\Windows\System32\drivers\etc\hosts
192.168.33.135 www.zxl.com 192.168.33.135 blog.zxl.com 192.168.33.135 bbs.zxl.com
rs-02節點日誌
[root@rs-02 logs]# tail bbs.access.log 192.168.33.135 - - [27/Jun/2016:09:53:07 +0800] "GET / HTTP/1.0" 200 14 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" "-" 192.168.33.135 - - [27/Jun/2016:09:53:10 +0800] "GET / HTTP/1.0" 200 14 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" "-" [root@rs-02 logs]# tail blog.access.log 192.168.33.135 - - [27/Jun/2016:10:02:39 +0800] "GET / HTTP/1.0" 200 15 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" "-" 192.168.33.135 - - [27/Jun/2016:10:02:56 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" "-" [root@rs-02 logs]# tail www.access.log 192.168.33.135 - - [27/Jun/2016:10:03:33 +0800] "GET / HTTP/1.0" 200 14 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" "-"
注:能夠看到上面ip地址來自lb負載均衡器
rs-01節點日誌
[root@rs-01 httpd]# tail www.zxl.com-access_log 192.168.33.135 - - [17/Jun/2016:07:22:34 +0800] "GET / HTTP/1.0" 200 15 [root@rs-01 httpd]# tail bbs.zxl.com-access_log 192.168.33.135 - - [17/Jun/2016:07:23:00 +0800] "GET / HTTP/1.0" 304 - [root@rs-01 httpd]# tail blog.zxl.com-access_log 192.168.33.135 - - [17/Jun/2016:07:22:53 +0800] "GET / HTTP/1.0" 200 16
注:上面ip地址來自lb負載均衡器
後端節點如何才能記錄來自真實的ip地址訪問呢?
其實後端節點nginx已經準備接收參數默認已經開啓了,http{}
標籤中,log_format main ....$http_x_forwarded_for
,$http_x_forwarded_for
記錄客戶端的真實ip地址
那麼apache呢?修改示例以下
[root@rs-01 conf]# sed -n '498p' httpd.conf LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common
修改配置文件後從新啓動服務
[root@rs-01 conf]# /etc/init.d/httpd restart Stopping httpd: [ OK ] Starting httpd: httpd: apr_sockaddr_info_get() failed for rs-01 httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
完成示例以下
[root@lb-01 conf.d]# cat upstream.conf upstream blog { server 192.168.33.131:80 weight=3; server 192.168.33.132:80 weight=3; } server { listen 80; server_name blog.zxl.com; location / { proxy_pass http://blog; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 80; server_name bbs.zxl.com; location / { proxy_pass http://blog; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } server { listen 80; server_name www.zxl.com; location / { proxy_pass http://blog; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
注:上面server
段標籤中的proxy_set_header X-Forwarded-For $remote_addr;
與proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
功能都是同樣獲取來自真實的客戶端訪問的ip地址
檢查lb的nginx語法以及從新加載服務
[root@lb-01 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb-01 conf.d]# nginx -s reload
查看rs-01的bbs站點日誌訪問
[root@rs-01 ~]# tail -n 2 /var/log/httpd/bbs.zxl.com-access_log "192.168.33.1" - - [17/Jun/2016:20:07:35 +0800] "GET / HTTP/1.0" 200 15 "192.168.33.1" - - [17/Jun/2016:20:07:35 +0800] "GET / HTTP/1.0" 200 15
注:能夠看到已經記錄了來自客戶端訪問的真實ip地址了
查看rs-02的bbs站點日誌訪問
[root@rs-02 ~]# tail -n 2 /usr/share/nginx/logs/bbs.access.log 192.168.33.135 - - [27/Jun/2016:22:57:04 +0800] "GET / HTTP/1.0" 200 14 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" "192.168.33.1" 192.168.33.135 - - [27/Jun/2016:22:57:40 +0800] "GET / HTTP/1.0" 200 14 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" "192.168.33.1
注:能夠看到已經記錄了來自客戶端訪問的真實ip地址了
其實lb負載均衡器配置文件中的location中能夠把一些n多參數使用配置文件引用便可,就拿blog站點示例以下
[root@lb-01 conf.d]# cat upstream.conf upstream blog { server 192.168.33.131:80 weight=3; server 192.168.33.132:80 weight=3; } server { listen 80; server_name blog.zxl.com; location / { proxy_pass http://blog; #proxy_set_header Host $host; #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; include /etc/nginx/conf.d/a.conf; } }
a.conf文件內容以下
[root@lb-01 conf.d]# cat a.conf proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 30; proxy_send_timeout 15; proxy_read_timeout 15;
使用www站點來作說明
lb-nginx配置以下
[root@lb-01 conf.d]# cat upstream.conf upstream static_pools { server 192.168.33.131:80 weight=3; } upstream dynamic_pools { server 192.168.33.132:80 weight=3; } server { listen 80; server_name www.zxl.com; location /static/ { proxy_pass http://static_pools; include /etc/nginx/conf.d/a.conf; } location /dynamic/ { proxy_pass http://dynamic_pools; include /etc/nginx/conf.d/a.conf; } }
include引用的a.conf配置文件
檢查lb-nginx配置文件語法以及從新加載
[root@lb-01 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb-01 conf.d]# nginx -s reload
rs-02節點配置以下
[root@rs-02 nginx]# pwd /usr/share/nginx [root@rs-02 nginx]# ll total 20 drwxr-xr-x. 2 root root 4096 Jun 27 09:26 bbs drwxr-xr-x. 2 root root 4096 Jun 27 09:26 blog drwxr-xr-x. 2 root root 4096 Jun 27 09:18 html drwxr-xr-x. 2 root root 4096 Jun 27 09:56 logs drwxr-xr-x. 2 root root 4096 Jun 27 09:26 www [root@rs-02 nginx]# mkdir www/dynamic -p [root@rs-02 nginx]# echo dynamic > www/dynamic/index.html
rs-02節點測試訪問www.zxl.com
[root@rs-02 nginx]# curl www.zxl.com nginx www 132 [root@rs-02 nginx]# curl www.zxl.com/dynamic/index.html dynamic [root@rs-02 nginx]# curl www.zxl.com/static/index.html <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.0.15</center> </body> </html>
同理得rs-01節點也是如此
rs-01節點配置以下
[root@rs-01 ~]# mkdir /var/www/www/static/ [root@rs-01 ~]# echo static >/var/www/www/static/index.html
rs-01節點測試訪問結果以下
[root@rs-01 ~]# curl www.zxl.com/static/index.html static [root@rs-01 ~]# curl www.zxl.com/dynamic/index.html <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>404 Not Found</title> </head><body> <h1>Not Found</h1> <p>The requested URL /dynamic/index.html was not found on this server.</p> <hr> <address>Apache/2.2.15 (CentOS) Server at www.zxl.com Port 80</address> </body></html>
以上訪問就已經實現動態分離了根據lb-nginx的字符串匹配,使用瀏覽器訪問結果也是同樣的
lb-nginx配置文件內容以下
server { listen 80; server_name www.zxl.com; location ~ .*. (gif|jpg|jpeg|png|bmp|swf|css|js)$ { proxy_pass http://static_pools; include a.conf; } location ~ .*. (php|php5)$ { proxy_pass http://dynamic_pools; include a.conf; } }