[root@yaoxiang ~]# yum install nginx
複製代碼
[root@yaoxiang ~]# nginx -v
nginx version: nginx/1.12.2
複製代碼
nginx的版本必須高於1.9.0,由於從1.9開始nginx就支持對TCP的轉發,而到了1.9.13時,UDP轉發也支持了。nginx
[root@yaoxiang ~]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
複製代碼
nginx實現TCP/UDP的轉發依靠的是Stream模塊,查看默認編譯參數中是含有**--with-stream參數,能夠看到上面的參數裏含有--with-stream=dynamic**,說明已動態加載Stream模塊算法
[root@yaoxiang ~]# vim /etc/nginx/nginx.conf
...
events {
worker_connections 1024;
}
stream {
#定義被代理的服務器組upstream 組名
upstream dns {
# zone dns 64k;
server 172.27.9.204:4005;#代理的服務器地址
server 172.27.9.204:4006 weight=5;#weight 權重,默認都是1
server 172.27.9.204:4007 max_fails=3 fail_timeout=30s;#max_fails - NGINX將服務器標記爲不可用的連續失敗嘗試次數。fail_timeout爲NGINX認爲服務器不可用的時間長度。
server 172.27.9.204:4008 backup;#backup 備用服務器,其餘服務器不可用時,纔會收到轉發
}
server {
listen 4000 udp;#監聽udp4000端口,如不加udp則默認監聽tcp
proxy_responses 1;#1表明須要迴應,並將迴應轉發;0表明不須要回應
proxy_timeout 20s;#迴應超時時間,超時未迴應暫停轉發
proxy_pass dns;#代理服務器、服務器組
proxy_buffer_size 512k;#響應緩衝區大小,若是後端發送的響應頭過大能夠嘗試增長此緩衝。
}
}
http {
...}
複製代碼
round-robin(輪詢)——默認,Nginx使用輪詢算法負載均衡通訊。由於是默認方法,因此沒有round-robin指令;只建立upstream配置塊在頂級stream上下文並像以前步驟添加server指令。vim
last_conn(最少鏈接)——Nginx選擇當前活躍鏈接數較少的服務器。後端
least_tome——Nginx選擇最低平均延遲和最少活躍鏈接的服務器。最低活躍鏈接基於least_time指令的如下參數計算:bash
connect——鏈接upstream服務器的時間服務器
first_byte——接收第一個數據字節的時間負載均衡
last_byte——從服務器接收完整響應的時間dom
hash——Nginx基於用戶定義鍵選擇服務器,例如,源IP地址tcp
#####例子:採用最少鏈接和hash負載均衡策略測試
upstream dns {
last_conn;#最少鏈接策略
server 172.27.9.204:4005;#代理的服務器地址
server 172.27.9.204:4006;
server 172.27.9.204:4007;
server 172.27.9.204:4008;
}
複製代碼
upstream stream_backend {
hash $remote_addr;
server backend1.example.com:12345;
server backend2.example.com:12345;
server backend3.example.com:12346;
}
複製代碼
[root@yaoxiang nginx]# sudo nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
複製代碼
出現以上提示說明端口被佔用,修改nginx.conf裏http模塊中的端口號
4006端口
4005端口