编辑
2024-05-15
中间件
00
请注意,本文编写于 394 天前,最后修改于 394 天前,其中某些信息可能已经过时。

nginx的负载均衡主要是以下配置起作用

#负载均衡 upstream load_banance { #负载均衡方法,可选

,ip_hash等,不填写则为轮询方式; # 服务器的访问地址,最好使用服务器的私有IP以获得更好的性能和安全性。 server 172.163.32.125:8090 weight=1; server 172.163.32.165:8090 weight=1; }

以上配置表示当请求nginx时,会按照轮询的方式去172.163.32.125:8090和172.163.32.165:8090请求服务

以下是一个完整的nginx配置示例:

#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; #负载均衡 upstream load_banance { #负载均衡方法,可选:least_conn,ip_hash等,不填写则为轮询方式; # 服务器的访问地址,最好使用服务器的私有IP以获得更好的性能和安全性。 server 172.163.32.125:8090 weight=1; server 172.163.32.165:8090 weight=1; } server { listen 80; charset utf-8; root /home/allen/mnt/cpc/dist; location = /index.html { add_header Cache-Control "no-cache, no-store"; } location / { try_files $uri $uri/ @router; index index.html index.htm; } location @router { rewrite ^.*$ /index.html last; } location /api { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_pass http://load_banance; rewrite "^/api/(.*)$" /$1 break; } client_max_body_size 10M; } # 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; # } #} }

本文作者:Allen Tang

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!