實驗目的
通過nginx實現反向代理的功能,類似apache反向代理和haproxy反向代理
工作中用nginx做反向代理和負載均衡的也越來越多了
有些公司從web服務器到反向代理,都使用nginx。nginx在1.9版本加入了tcp的反向代理功能
甚至安全策略:nginx+lua 完全可以搞定。
打開nginx官網
nginx做反向代理,安裝命令如下,使用www用戶運行nginx
useradd -s /sbin/noglogin -M www wget http://nginx.org/download/nginx-1.9.12.tar.gz tar zxf nginx-1.9.12.tar.gz cd nginx-1.9.12 ./configure --prefix=/usr/local/nginx-1.9.12 \ --user=www --group=www --with-http_ssl_module \ --with-http_stub_status_module --with-file-aio make && make install ln -s /usr/local/nginx-1.9.12/ /usr/local/nginx
檢查語法
[root@linux-node2 nginx-1.9.12]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.9.12/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.9.12/conf/nginx.conf test is successful [root@linux-node2 nginx-1.9.12]#
檢查服務器有無其它服務占用80端口,可以關閉了。
[root@linux-node1 ~]# /usr/local/httpd/bin/apachectl -k stop
配置nginx反向代理,修改主配置文件
gzip是默認關閉的
長連接默認打開的
sendfile 默認打開的
[root@linux-node1 conf]# cat nginx.conf #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 10240; } 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; upstream backend { server 10.0.1.105:8080 weight=1 max_fails=3 fail_timeout=30s; server 10.0.1.106:8080 weight=2 max_fails=3 fail_timeout=30s; } server { listen 80; server_name www.nginx-nmap.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://backend; } #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } [root@linux-node1 conf]#
上面設置虛擬主機名www.nginx-nmap.com,以及后端集群組backend,設置了location把任何請求都發給后端backend
上面配置文件里也設置了后端web集群
負載均衡配置時的2個參數:fail_timeout和max_fails
這2個參數一起配合,來控制nginx怎樣認為upstream中的某個server是失效的當在fail_timeout的時間內,某個server連接失敗了max_fails次,則nginx會認為該server不工作了。
同時,在接下來的 fail_timeout時間內,nginx不再將請求分發給失效的server。
比如失敗3次,那么接下來10秒不會之內不會把請求發個這個認為失敗的機器。然后過了30秒后,這個機器繼續收到探測請求.一般生產中設置為30秒
upstream backend { server 10.0.1.105:8080 weight=1 max_fails=3 fail_timeout=30s; server 10.0.1.106:8080 weight=2 max_fails=3 fail_timeout=30s; }
關於nginx反向代理功能由下面模塊提供
可以參照下官方個的配置例子
官方文檔做的挺好
檢測語法,啟動或者reload。查看監聽狀態
[root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.9.12/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.9.12/conf/nginx.conf test is successful [root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -s reload [root@linux-node1 conf]# netstat -lntp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27141/nginx: master tcp6 0 0 :::8080 :::* LISTEN 20130/httpd [root@linux-node1 conf]#
客戶端windows的hosts文件里配置如下
10.0.1.105
www.nginx-nmap.com
瀏覽器測試
停止node2的httpd。nginx會自動把請求發送給node1,前端無感知
[root@linux-node2 nginx-1.9.12]# systemctl stop httpd [root@linux-node2 nginx-1.9.12]# systemctl start httpd [root@linux-node2 nginx-1.9.12]#
啟動node2的httpd之后,刷30秒,node2才出現,也就是我們設置的fail_timeout=30的緣故
關於會話保持
會話保持,有基於ip的有ip_hash
直接添加這一行即可
重啟
[root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.9.12/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.9.12/conf/nginx.conf test is successful [root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -s reload [root@linux-node1 conf]#
再次訪問就只有node2了
關於nginx的負載均衡算法有很多,自行百度