環境準備
- 操作系統:ubuntu 20.04
- nginx版本:1.12.1 (測試發現,高版本1.23.0故障轉移不好使,不知道是哪配的不對)
- 安裝編譯依賴:
sudo apt-get install libpcre3 libpcre3-dev libssl-dev
編譯安裝
下載源碼,並編譯
操作如下:
# 下載源碼
wget http://nginx.org/download/nginx-1.12.1.tar.gz
# 解壓
tar xavf nginx-1.12.1.tar.gz
# 切換到nginx目錄
cd ./ginx-1.12.1
# configure, 注意,必須加上--with-stream, 否則無法使用udp
./configure --prefix=/usr/local/nginx --with-stream
# 編譯
make
此時編譯可能會報錯:
cc1: all warnings being treated as errors
make[1]: *** [objs/Makefile:511: objs/src/core/ngx_murmurhash.o] Error 1
make[1]: Leaving directory '/tmp/nginx-1.12.1'
make: *** [Makefile:8: build] Error 2
修改修改objs/Makefile
文件, 去掉#CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g
中的-Werror
, 保存退出後,重新執行make
命令。
發現報錯如下:
src/os/unix/ngx_user.c:36:7: error: ‘struct crypt_data’ has no member named ‘current_salt’
打開文件src/os/unix/ngx_user.c
,將這行(第36行)代碼註釋掉:
//cd.current_salt[0] = ~salt[0];
重新執行make
命令即可編譯通過。
安裝
sudo make install
nginx會安裝到 /usr/local/nginx
目錄下
建立軟鏈接,方便調用
cd /usr/bin/
ln -sf /usr/local/nginx/sbin/nginx
配置
修改配置/usr/local/nginx/conf/nginx.conf
,使支持udp高可用。修改如下:
#user nobody;
#worker_processes 1;
#nginx進程數,建議設置爲等於CPU總核心數.
worker_processes 6;
# nginx默認是沒有開啓利用多核cpu的配置的。需要通過增加worker_cpu_affinity配置參數來充分利用多核cpu
worker_cpu_affinity 000001 000010 000100 001000 010000 100000;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(系統的值ulimit -n)與nginx進程數相除,但是nginx分配請求並不均勻,所以建議與ulimit -n的值保持一致.
worker_rlimit_nofile 65535;
events {
#worker_connections 1024;
#參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內核中的高性能網絡I/O模型,如果跑在FreeBSD上面,就用kqueue模型.
use epoll;
#單個進程最大連接數(最大連接數=連接數*進程數)
worker_connections 65535;
# multi_accept :告訴nginx收到一個新連接通知後接受盡可能多的連接,默認是on,設置爲on後,多個worker按串行方式來處理連接,也就是一個連接只有一個worker被喚醒,其他的處於休眠狀態,設置爲off後,多個worker按並行方式來處理連接,也就是一個連接會喚醒所有的worker,直到連接分配完畢,沒有取得連接的繼續休眠。
multi_accept on;
}
stream {
upstream dns_upstreams {
#20s內出現3次錯誤,該服務器將被熔斷20s
server 192.168.10.250:7778 max_fails=3 fail_timeout=20s weight=10;
server 192.168.10.201:7778 max_fails=3 fail_timeout=20s weight=10;
server 192.168.10.202:7778 max_fails=3 fail_timeout=20s weight=10;
}
server {
listen 7777 udp;
#代理服務器、服務器組
proxy_pass dns_upstreams;
#與被代理服務器建立連接的超時時間爲3s
proxy_timeout 3s;
#錯誤日誌
error_log logs/dns.log;
#當被代理的服務器返回錯誤或超時時,將未返回響應的客戶端連接請求傳遞給upstream中的下一個服務器
proxy_next_upstream on;
#轉發嘗試請求不限制
proxy_next_upstream_tries 0;
#轉發嘗試時間不限制
proxy_next_upstream_timeout 0;
}
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#
# websocket高可用
upstream vmws {
server 192.168.10.250:8888 max_fails=1 fail_timeout=3s;
server 192.168.10.201:8888 max_fails=1 fail_timeout=3s ;
server 192.168.10.202:8888 max_fails=1 fail_timeout=3s ;
}
server {
listen 8989;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://vmws;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_next_upstream error timeout invalid_header http_502;
proxy_read_timeout 3600s;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
使用命令nginx -s reload
重新加載配置。
可能報錯:
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
此時重新指定一下配置文件即可,調用命令:nginx -c /usr/local/etc/nginx/nginx.conf
, 然後重新調用 nginx -s reload
即可