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)
未完待续...