1、編譯nginx
[root@server1 mnt]# ls
nginx-1.18.0.tar.gz
[root@server1 mnt]# tar zxf nginx-1.18.0.tar.gz
[root@server1 mnt]# ls
nginx-1.18.0 nginx-1.18.0.tar.gz
[root@server1 mnt]# cd nginx-1.18.0/
原始碼編譯:
[root@server1 nginx-1.18.0]# ./configure --help /configure 表示腳本 ,可以查看幫助指定引數
編譯需要指定路經,如果不指定路經,系統默認的第三方路經為:
[root@server1 nginx-1.18.0]# ls /usr/local/
bin etc games include lib lib64 libexec sbin share src
[root@server1 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module(監控) 編譯
checking for OS
+ Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found 報錯,需要安裝gcc
[root@server1 nginx-1.18.0]# yum install gcc -y 安裝gcc
[root@server1 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module 繼續執行
提示卻少PCRE庫

[root@server1 nginx-1.18.0]# yum install -y pcre-devel 安裝pcre-devel -devel是默認格式
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module 繼續執行
提示缺少openssl庫

[root@server1 nginx-1.18.0]# yum install openssl-devel -y 安裝openssl
[root@server1 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module 繼續執行,執行成功不會再次報錯了
[root@server1 nginx-1.18.0]# ll Makefile 執行成功后會生成Makefile檔案,會把執行的引數寫入這個檔案,Makefile會指導后續的編譯
-rw-r--r-- 1 root root 376 Dec 9 21:23 Makefile
[root@server1 nginx-1.18.0]# make 編譯
[root@server1 nginx-1.18.0]# make install 安裝,安裝好后會自動創建/usr/local/nginx/ 安裝目錄
[root@server1 nginx]# cd /usr/local/nginx/ 進入安裝目錄,切記后續在編輯配置時,必須進入/usr/local/nginx/ 目錄里
[root@server1 nginx]# ls
conf html logs sbin
為了方便nginx運行,直接定位到nginx命令的話,需要變更niginx環境變數
[root@server1 ~]# vim .bash_profile 變更當前用戶的環境變數

[root@server1 ~]# source .bash_profile 使當前環境變數生效
[root@server1 ~]# nginx nginx就生效了
[root@server1 ~]# netstat -antlp 查看埠
nginx 80 埠已經打開

2、nginx編譯優化
[root@server1 ~]# cd /usr/local/nginx/ 進入安裝目錄
[root@server1 nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@server1 nginx]# du -sh NIGINX運行時安裝目錄大小5.8M,太大了
5.8M
方法一:
.[root@server1 nginx]# nginx -s stop 關閉nginx
[root@server1 nginx]# cd ..
[root@server1 local]# ls
bin etc games include lib lib64 libexec nginx sbin share src
[root@server1 local]# rm -fr nginx/ 洗掉nginx/
[root@server1 local]# cd /mnt/ 進入原始碼
[root@server1 mnt]# cd nginx-1.18.0/
[root@server1 nginx-1.18.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@server1 nginx-1.18.0]# make clean 清理快取
rm -rf Makefile objs
方法二:
[root@server1 ~]# cd /mnt/
[root@server1 mnt]# ls
nginx-1.18.0 nginx-1.18.0.tar.gz
[root@server1 mnt]# rm -fr nginx-1.18.0 洗掉原來解壓的
[root@server1 mnt]# tar zxf nginx-1.18.0.tar.gz 重新解壓
[root@server1 nginx-1.18.0]# vim auto/cc/gcc 注釋掉debug

[root@server1 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module 執行
[root@server1 nginx-1.18.0]# make 編譯
[root@server1 nginx-1.18.0]# make install 安裝
configure作用是生成makefile
make作用是把原碼生成二進制程式,
makinstall是將編譯好的程式拷貝到指定的路經已經其他組態檔
[root@server1 nginx-1.18.0]# cd /usr/local/nginx/ 進入安裝目錄
[root@server1 nginx]# ls
conf html logs sbin
[root@server1 nginx]# du -sh 目錄大小變成了1.9M
980K .
3、 nginx 實作負載均衡
[root@server1 conf]# useradd -M -d /usr/local/nginx/ -s /sbin/nologin nginx 創建nginx用戶
[root@server1 conf]# vim nginx.conf 編輯組態檔,更改用戶

[root@server1 conf]# nginx -s reload 多載服務
[root@server1 conf]# ps aux 可以發現用戶已經從nobady變成nginx

[root@server1 conf]# vim nginx.conf 編輯組態檔

在組態檔最后添加

[root@server1 conf]# 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
[root@server1 conf]# nginx -s reload 多載服務
[root@foundation50 isos]# vim /etc/hosts 添加決議

[root@foundation50 isos]# curl www.westos.org 已經實作負載均衡
server2
[root@foundation50 isos]# curl www.westos.org 已經實作負載均衡
server3
http狀態碼,根據報錯資訊判斷錯誤

4、 nginx優化
[root@server1 conf]# vim nginx.conf 編輯組態檔

[root@server1 conf]# nginx -s reload 多載服務
注意:work processes 數跟cpu數相關,有多少個cpu我們就開多少個worker,但是有的主機配置不一樣不知道開多少個
worker,就設定為自動,如下:
[root@server1 conf]# vim nginx.conf 編輯組態檔

[root@server1 conf]# nginx -s reload 多載服務
[root@server1 conf]# ps ax 查看行程 ,自動生成兩個worker

worker和cpu捆綁,減少cpu來回切換造成的損耗
[root@server1 conf]# vim nginx.conf 編輯組態檔

測驗:
nginx 恢復原有配置時做宿主機做壓力測驗
原有配置:

[root@server1 conf]# nginx -s reload
[root@foundation50 Desktop]# ab -c10 -n10000 http://www.westos.org/index.html 壓力測驗,-c10 表示10個并發 ,-n10000表示打10000的流量

做cpu和worker系結后,宿主機做壓力測驗

更改作業系統訪問量65535
[root@server1 conf]# vim /etc/security/limits.conf 更改作業系統訪問量65535,因為作業系統訪問檔案數一定要大于等于客戶端訪問數
[root@server1 conf]# nginx -s reload 多載服務

[root@foundation50 Desktop]# ab -c10 -n100000 http://www.westos.org/index.html 做壓力測驗

可以發現cpu和worker系結后,比原有訪問量大
5、 設定后端權重
[root@server1 conf]# vim nginx.conf 編輯組態檔

[root@server1 conf]# nginx -s reload 多載服務
[root@foundation50 Desktop]# curl www.westos.org 訪問,發現nginx訪問server2訪問比serve1多
server2
[root@foundation50 Desktop]# curl www.westos.org
server3
[root@foundation50 Desktop]# curl www.westos.org
server2
[root@foundation50 Desktop]# curl www.westos.org
server2
6、 設定backup備機
作用:backup 引數是指當所有非備機server2、server3都宕機或者不可用的情況下,就只能使用帶backup標準的備機,
[root@server1 conf]# vim nginx.conf 編輯組態檔

[root@server1 conf]# nginx -s reload 多載服務
測驗:
[root@server2 ~]# systemctl stop httpd 停掉后端apache
[root@server3 ~]# systemctl stop httpd 停掉后端apache
[root@foundation50 Desktop]# curl www.westos.org 當后端都不可用的時候,再次訪問www.westos.org,nginx調度器會決議backup備機,當前設定的是自己本機
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
7、nginx調度演算法
ip_hash演算法:
[root@server1 conf]# vim nginx.conf 編輯組態檔

`[root@server1 conf]# nginx -s reload` 多載服務
測驗:
[root@foundation50 Desktop]# curl www.westos.org 只要來源不變 訪問就不會變,根據客戶端地址來判斷
server2
[root@foundation50 Desktop]# curl www.westos.org
server2
[root@foundation50 Desktop]# curl www.westos.org
手動下線某臺機器
[root@server1 conf]# vim nginx.conf 編輯組態檔

[root@server1 conf]# nginx -s reload 多載服務
[root@foundation50 Desktop]# curl www.westos.org 訪問,只能訪問到serve3
server3
[root@foundation50 Desktop]# curl www.westos.org
server3
Sticky cookie 演算法:
注意:資料走向 client ->dns->cdn->nginx 一般企業會搭建cdn快取,其實cdn也是一個反向代理,訪問后端,都是cdn去連接nginx,所以不管client如何變,只要經過cdn,都是cdn ip地址,不能做負載均衡,所以不能用ip_hash演算法
Sticky cookie 演算法
[root@server1 conf]# cd /mnt/
[root@server1 mnt]# cd nginx-1.18.0/
[root@server1 nginx-1.18.0]# make clean 清理快取
rm -rf Makefile objs
[root@foundation50 lamp]# scp nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip server1:/mnt 將第三方軟體拷貝到server1上
[root@server1 mnt]# yum install -y unzip 安裝解壓包
[root@server1 mnt]# unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip 解壓

[root@server1 mnt]# cd nginx-1.18.0/ 進入到原始碼目錄,重新編譯
[root@server1 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/mnt/nginx-goodies-nginx-sticky-module-ng-08a395c66e42 執行 --add-module表示添加一個模塊,把第三方模塊加入
[root@server1 nginx-1.18.0]# make && make install 編譯及安裝
[root@server1 nginx]# cd /usr/local/nginx/conf/ 進入組態檔目錄
[root@server1 conf]# vim nginx.conf ,編輯組態檔,添加演算法

[root@server1 conf]# 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
[root@server1 conf]# nginx 啟動nginx

清理瀏覽器快取

再次訪問www.westos.org, cookie發生變化,所以呼叫到另一個后端server3上

注意:不能用curl命令去訪問,因為不支持
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/379508.html
標籤:其他
上一篇:HTTP協議請求
