Nginx 负载均衡 初步配置验证 笔记
需求
有兩臺windows服務器,iis承載WebAPI,測試使用Windows平臺Nginx做負載均衡驗證。
A機IP及Web端口:192.168.7.54:8052。
B機IP及Web端口:192.168.7.161:8051。
Nginx所在機器IP及端口:192.168.7.161:8050。
A機Web頁面示意,這里顯示了A機的IP和當前時間。
?B機Web頁面示意,這里顯示了B機的IP和當前時間。
Nginx安裝及啟動
Nginx在windows平臺安裝,已有博友寫的很詳細了。
參考Nginx Windows詳細安裝部署教程 - taiyonghai - 博客園
下載地址:nginx: download
啟動時如果失敗,需要檢查是否IIS或其他程序占用了默認的80端口,如是則進入nginx-1.22.1\conf\目錄找到nginx.conf修改默認的端口,本例改為8123.
cmd下cd到nginx-1.22.1目錄后使用【start nginx】啟動nginx后,在瀏覽器中輸入【http://localhost:8123/】即可看到Nginx的歡迎頁面。
以下是cmd中啟動nginx、查找nginx進程、退出nginx的命令。(start nginx輸入兩遍是因為第一次端口被占用而啟動失敗)
Microsoft Windows [版本 6.1.7601] 版權所有 (c) 2009 Microsoft Corporation。保留所有權利。D:\JavaDevEnv\nginx-1.22.1>start nginxD:\JavaDevEnv\nginx-1.22.1>start nginxD:\JavaDevEnv\nginx-1.22.1>tasklist /fi "imagename eq nginx.exe"映像名稱 PID 會話名 會話# 內存使用 ========================= ======== ================ =========== ============ nginx.exe 4532 RDP-Tcp#0 1 5,824 K nginx.exe 5360 RDP-Tcp#0 1 6,064 KD:\JavaDevEnv\nginx-1.22.1>nginx -s quitnginx歡迎界面
配置Nginx實現兩臺Web服務器負載均衡
進入nginx-1.22.1\conf\目錄找到nginx.conf用記事本打開。
在http花括號內最后增加一個server節(nginx支持多個server節,即一個nginx支持監聽多個端口)
針對本文需求,增加的配置如下
#wdh config the serversupstream myhttpIISServer{server 192.168.7.54:8052;server 192.168.7.161:8051;}#wdh config the proxyserver{listen 8050;server_name localhost;location /{proxy_pass http://myhttpIISServer;}}配置負載均衡的策略有多種,可參考博文nginx配置負載均衡_飛翔絕戀的博客-CSDN博客_nginx配置負載均衡
本文配置的完整的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 1024; }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;server {listen 8123;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#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;# }#}#wdh config the serversupstream myhttpIISServer{server 192.168.7.54:8052;server 192.168.7.161:8051;}#wdh config the proxyserver{listen 8050;server_name localhost;location /{proxy_pass http://myhttpIISServer;}}}配置完之后使用cmd命令重啟nginx(有帖子介紹重啟命令為nginx -s reload,本文未嘗試)
此外還有一個重要事項,分別在A機設置防火墻允許8052端口通過、設置B機防火墻允許8050、8051端口通過。
測試
測試打開瀏覽器輸入【http://192.168.7.161:8050/】,持續刷新頁面,可以看到頁面在
A機(192.168.7.54:8052)和B機(192.168.7.161:8051)之間不斷切換,但客戶端瀏覽器的url未變,印證了nginx已將客戶端請求分發到不同Web服務器上。
延伸使用,在項目實戰中前后端分離項目,可以將nginx負載均衡用到后端WebAPI上;在讀寫分離+mysql一主多從項目中,也可以將nginx負載均衡用于多個mysql服務器。
總結
以上是生活随笔為你收集整理的Nginx 负载均衡 初步配置验证 笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小马哥---高仿苹果6 主板型号z13
- 下一篇: Nginx —— 检查配置文件ngi