日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nginx源码编译、负载均衡及模块的扩展

發布時間:2023/11/27 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx源码编译、负载均衡及模块的扩展 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、nginx源碼編譯

實驗環境:
iptables和selinux關閉
redhat6.5
nginx:test1: 172.25.1.11

[root@test1 ~]# ls
nginx-1.14.0.tar.gz
[root@test1 ~]# tar zxf nginx-1.14.0.tar.gz???

[root@test1 ~]# useradd -s /sbin/nologin nginx??

[root@test1 ~]# id nginx

?
[root@test1 ~]# cd nginx-1.14.0
[root@test1 nginx-1.14.0]# cd src/core/
[root@test1 core]# vim nginx.h


[root@test1 core]# cd -
/root/nginx-1.14.0
[root@test1 nginx-1.14.0]# cd auto/cc/
[root@test1 cc]# vim gcc????????? //進行注釋


[root@test1 cc]# cd -
/root/nginx-1.14.0

[root@test1 nginx-1.14.0]# yum install pcre-devel openssl-devel -y ? ? ? ? ? ? ? ? ? //下載所需的依賴包

[root@test1 nginx-1.14.0]# ./configure --help???????????????????? //查看模塊

[root@test1 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio

[root@test1 nginx-1.14.0]# make && make install

[root@test1 nginx-1.14.0]# cd
[root@test1 ~]# ln -s /usr/local/nginx/sbin/nginx /sbin/

[root@test1 ~]# nginx -t??????????? //檢查配置是否有錯誤

[root@test1 ~]# nginx?????????? //打開服務

[root@test1 ~]# nginx -s reload?????????? //重新加載
[root@test1 ~]# nginx -s stop?????????? //停止服務

[root@test1 html]# pwd
/usr/local/nginx/html

[root@test1 html]# vim test1.html

[root@test1 html]# nginx

網頁進行訪問

到此,nginx服務就搭建完成了。。。

2、實現負載均衡

nginx的負載均衡是基于反向代理之上實現的。自帶的模塊有:

ngx_http_proxy_module: proxy代理模塊,把請求后拋給服務器節點或upstream服務器池
ngx_http_upstream_module: 負載均衡模塊,可以實現網站的
負載均衡及節點的健康檢查功能

如下是HTTP負載均衡模塊upstream 指定設置群服務器,服務器可以指定不同的權重 ? ?

nginx支持5種方式的查詢:

  • 輪詢 Nginx默認的查詢方式 ,默認權重為1。?????? //下面的實驗用到的即為這種方式
  • weight 指定分配的輪詢方式,根據后端服務器的性能來做權重。?
  • ip_hash 每個請求按照ip的hash結果分配,這樣每個IP地址就可以固定的訪問后端的一臺服務器,解決了集群部署環境下session共享的問題。
  • fair 第三方模塊,這個原理是按照響應時間的優先來分配的。需要安裝upstream_fair模塊
  • url_hash 按照url的hash結果來分配請求,使每個url定向到同一個后端的服務器。需要安裝nginx的hash軟件包

nginx對后端節點健康檢查的方式主要有3種:

  • ngx_http_proxy_module 模塊和ngx_http_upstream_module模塊(自帶)
  • nginx_upstream_check_module模塊,需要安裝
  • ngx_http_healthcheck_module模塊,需要安裝

實驗環境:

test1: nginx 172.25.1.11????? 調度器

test2: httpd 172.25.1.12?????? realserver

test2: httpd 172.25.1.13? ? ?? realserver

foundation:? 172.25.1.250?? client

nginx常用命令:
nginx -t?????????? //nginx語法檢查
nginx??????????? //查看nginx
nginx -s reload??? //刷新nginx服務
nginx -s stop????? //關閉nginx服務

在編譯完成的情況下繼續下一步

負載均衡原理圖(盜用一下哈):

反向代理原理圖:

對test1進行負載均衡的配置:

[root@test1 ~]# cd /usr/local/nginx/conf/

[root@test1 conf]# vim nginx.conf????????????? //記得提前備份一下

user? nginx?? nginx;????????????? //用戶名,可以不改使用默認
worker_processes? 2;???????? //一般跟CPU數保持一致

events {
??? worker_connections? 65535;??????????? //最大連接數,修改后需要修改變量
}

http {
??? upstream server {??????????? ? ? //upstream模塊,實現負載均衡

??? #ip_hash;???????? ? ? ? ?? //ip不變時后端的真實服務器不變

?? ?server 172.25.1.12:80;???????????? //后端的真實服務器,可以添加權重,如server 172.25.1.12:80 weight=3;默認為1
?? ?server 172.25.1.13:80;
?? ?}

??? include?????? mime.types;
??? default_type? application/octet-stream;

??? sendfile??????? on;
??? keepalive_timeout? 65;?????????????? //長連接超時時間,單位是秒

??? #gzip? on;???????????

??? server{
??????? listen?????? 80;??????????????????????? //監聽端口為80,這樣訪問的時候就不需要輸端口,直接輸入訪問域名就可以
??????? server_name? www.westos.org;?????????????????? //訪問的域名,可以有多個(用空格隔開)
??????? location / {
??????? proxy_pass http://server;???????????????? //這里的server與upstream模塊那里的名字保持一致
?? ?}

??????? error_page?? 500 502 503 504? /50x.html;
??????? location = /50x.html {
??????????? root?? html;
??????? }
??? }
}

?
[root@test1 conf]# vim /etc/security/limits.conf???????????? ? ? //在限制文件中的末尾行修改最大連接數 ?????

[root@test1 conf]# nginx -t????????????? //啟動nginx前檢查配置是否存在問題

[root@test1 conf]# nginx????????? //啟動nginx服務

[root@test1 conf]# nginx -s reload???????? //重新加載

realserver端:

[root@test2 ~]# yum install -y httpd?????????? //下載http服務

[root@test2 ~]# vim /var/www/html/index.html

[root@test3 ~]# yum install -y httpd????????? //與test2執行一樣的動作

[root@test2 ~]# vim /var/www/html/index.html

客戶端進行訪問:

訪問前客戶端進行解析

[root@foundation1 ~]# vim /etc/hosts

172.25.1.11? test1 www.westos.org

[root@foundation1 ~]# curl www.westos.org

當test2掛掉時,訪問的是服務器test3,沒有出現錯誤,說明nginx自帶健康檢查的功能

[root@test2 ~]# /etc/init.d/httpd stop

負載均衡到此也結束啦啦啦。。。接下來是擴展nginx模塊

3、nginx模塊的擴展

[root@test1 ~]# nginx -s stop?????????????? //先關閉nginx服務

nginx的默認發布目錄在 /usr/local/nginx/html

[root@test1 ~]# cd nginx-1.14.0
[root@test1 nginx-1.14.0]# pwd
/root/nginx-1.14.0

[root@test1 nginx-1.14.0]# ./configure --help???????????? //用./configure --help查看有哪些模塊

第一種情況若需要安裝的模塊在命令下有,則重新編譯并添加需要編譯的模塊

例如:需要安裝 --add-module=/data/software/ngx_http_google_filter_module模塊,則執行:?

[root@test1 ~]# nginx -V?????????????????????? //查看已經編譯了哪些模塊

[root@test1 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/data/software/ngx_http_google_filter_module????????? //已編譯的和未編譯的一塊進行編譯

[root@test1 nginx-1.14.0]# make???????????????? //!!!必須要注意:不要make install,不然就被覆蓋了

?

第二種情況若需要安裝的模塊在命令下沒有,則:

例如:nginx-sticky-module模塊的介紹及擴展

sticky模塊有什么用呢

??????? 使用nginx做負載均衡器時,經常會遇到一個問題,如何將來自同一用戶的訪問始終定向到一臺后端設備進行響應?

??????? nginx一般有兩種辦法來實現會話保持:

  1. ip_hash:nginx原生支持的基于IP地址來將不同的請求轉發到同一臺服務器進行響應,缺點就是如果前端用戶都來自同一局域網,基于ip的負載方法會導致負載不均衡;
  2. sticky:基于cookie來進行負載轉發,保證將來自同一cookie的訪問始終定向到同一服務器響應,缺點就是需要編譯模塊,且cookie需要瀏覽器支持。

現在使用第一種ip_hash:

[root@test1 ~]# nginx -s stop

[root@test1 ~]# vim /usr/local/nginx/conf/nginx.conf

[root@test1 ~]# nginx -t??????????? //查看語句是否有問題

[root@test1 ~]# nginx?????????? //啟動nginx

客戶端進行訪問:

注意:客戶端需要對test1進行解析

[root@foundation1 ~]# for i in {1..5}; do curl www.westos.org; done;

這時只要客戶端ip不變,則訪問到的真實服務器為同一個,都是test3

我們換個客戶端試一試,啟動虛擬機test4并將其看作另一個客戶端:

先進行解析

[root@test4 ~]# for i in {1..5};do curl www.westos.org;done;

訪問到的也一直是test3,到這里我們就可以看出ip_hash的優點和缺點了,第一種情況就完成了。

現在使用第二種sticky模塊:

由于上面的nginx版本不支持sticky模塊,所以我們換個版本并將上一個版本服務關閉

[root@test1 ~]# nginx -s stop

[root@test1 ~]# tar zxf nginx-1.10.1.tar.gz

[root@test1 ~]# tar zxf nginx-sticky-module-ng.tar.gz

[root@test1 ~]# cd nginx-1.10.1

[root@test1 nginx-1.10.1]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-sticky-module-ng?????????????? //添加模塊

由于筆者上面已經在/usr/local/nginx/conf/nginx.conf 下有nginx1.14的配置文件,所以這里更換路徑為/opt/nginx,所以配置文件在/opt/nginx/conf下才生效

[root@test1 nginx-1.10.1]# make && make install

[root@test1 nginx-1.10.1]# cd /opt/nginx/conf????????????????????? //配置文件生效的路徑
[root@test1 conf]# cp /usr/local/nginx/conf/nginx.conf .???????????????
cp: overwrite `./nginx.conf'? yes

//由于上個實驗已經配置了負載均衡,所以我們可以將上一個nginx服務的配置文件拷貝過來直接用

[root@test1 conf]# vim nginx.conf?????????????? //只修改一處就好

[root@test1 conf]# /opt/nginx/sbin/nginx -t

[root@test1 conf]# /opt/nginx/sbin/nginx????????????????? //檢查并啟動服務

此時客戶端就可以訪問啦

客戶端訪問時,統一主機用命令訪問如下:

我們為您可以看到用curl命令訪問時并沒有實現session共享,訪問還是輪詢方式,這是為是那么呢

那是因為sticky模式有一個弊端:cookie需要瀏覽器支持。

我們用瀏覽器輸入www.westos.org試一下:

不管刷新多少回,訪問到的始終都是test3

?

補充一下:

筆者這里用的模塊壓縮包是nginx-sticky-module-ng.tar.gz大家也可以用nginx-sticky-module-1.1.tar.gz

我個人不太建議用這個壓縮包,做的時候可能會有一點問題。

用第二個的話編譯可能會報錯,如

make[1]: *** [objs/addon/nginx-sticky-module-1.1/ngx_http_sticky_misc.o] Error 1
make[1]: Leaving directory `/root/nginx-1.8.1'
make: *** [build] Error 2

此時修改一下配置文件:

[root@test1 nginx-1.8.1]# pwd

root/nginx-1.8.1

[root@test1 nginx-1.8.1]# vim nginx-sticky-module-1.1/ngx_http_sticky_misc.c?????????????? //修改第28

281???????? digest->len = ngx_sock_ntop(in,sizeof(struct sockaddr_in), digest->data, len, 1);

再次編譯成功

[root@test1 nginx-1.8.1]# make && make install

總結

以上是生活随笔為你收集整理的nginx源码编译、负载均衡及模块的扩展的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

歡迎分享!

轉載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:nginx源码编译、负载均衡及模块的扩展