Nginx配置以下:php
#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 80; 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; #} } #負載均衡 upstream test{ server 192.168.0.80:8080; } server{ listen 8888; server_name localhost; location /mytest{ #root html; #index lzy.html; proxy_pass http://test/xscbm2019/getNation; } } }
解析:html
一、負載均衡用upstream進行配置,例如upstream test{}。前端
二、upstream裏配置的server能夠有多個IP+port的組合。nginx
三、在須要進行負載均衡的路由中加入proxy_pass便可,例如上配置文件中proxy_pass http://test/xscbm2019/getNation;服務器
我的理解:併發
一、所謂負載均衡,就是由Nginx來進行服務器或應用的選擇,選擇方式有多種:app
(1)負載均衡
upstream test{ server IP:port weight=3; server IP:port; ... }
加權重的方式,數值越大越有可能被分配到。tcp
(2)高併發
upstream test{ ip_hash; server IP:port; server IP:port; ... }
Nginx未來自於同一客戶端的請求始終定向到同一服務器或應用(此服務器或應用不可用除外)。
(3)
upstream test{ least_conn; server IP:port; server IP:port; ... }
最少鏈接負載均衡,Nginx將嘗試優先向不太繁忙的服務器或應用分配請求,避免繁忙服務器或應用超載。
(4)
upstream test{ server IP:port; server IP:port; ... }
循環負載(均衡負載),對server進行循環方式分發。
二、proxy_pass http://test/xscbm2019/getNation;就像路由拼接,經過負載均衡定義好的服務器或應用地址進行拼接訪問。
三、若是是https只須要把proxy_pass http://test/xscbm2019/getNation;中的http換成https且在https模塊中進行配置便可,其餘協議則須要使用不一樣的指令,詳情參考:
http://nginx.org/en/docs/http/load_balancing.html
擴展:
一、Nginx和Tomcat的區別:
Nginx常作靜態內容服務和反向代理服務器以及頁面前端高併發服務器。嚴格來講Nginx應該叫作HTTP Server,而Tomcat則是一個Application Server。(摘自:http://www.javashuo.com/article/p-xtcswrlq-ga.html)
未完待續...