本文共 3232 字,大约阅读时间需要 10 分钟。
我在本机部署了三个虚拟机,所有虚拟机都运行了相同的项目。三个虚拟机的地址分别为192.168.20.133:8080、192.168.20.135:8080和192.168.20.136:8080。其中,nginx服务器安装在133号虚拟机上,当前的负载均衡配置如下:
根据配置,访问时会按照135、136、135、136、133的顺序轮询访问,这只是一个典型的访问顺序,实际情况可能会有所不同。需要注意的是,如果136号虚拟机暂时停止运行,133号和135号虚拟机仍能继续提供服务,确保服务的稳定性。
关于静态文件的处理,nginx配置如下:
location ~.(gif|jpg|jpeg|png|css|js) { root /usr/nginxStaticFile; expires 2h;} 以下是完整的nginx配置文件内容:
user nobody;worker_processes 1;error_log /usr/nginxLog/error.log;error_log logs/error.log notice;error_log logs/error.log info;pid /usr/nginxLog/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"' $status $body_bytes_sent "$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; access_log /usr/nginxLog/access.log; sendfile on; # tcp_nopush on; keepalive_timeout 65; # gzip on; upstream hostname { server 192.168.20.133:8080 max_fails=0 weight=1; server 192.168.20.135:8080 max_fails=0 weight=2; server 192.168.20.136:8080 max_fails=0 weight=2; } server { listen 80; server_name localhost; # charset koi8-r; # access_log logs/host.access.log main; location / { # root html; # index index.html index.htm; proxy_pass http://hostname; proxy_set_header X-Real-IP $remote_addr; } location ~.(gif|jpg|jpeg|png|css|js) { root /usr/nginxStaticFile; expires 2h; } # enable nginx status page location /nginxstatus { stub_status on; access_log on; } # error pages error_page 404 /404.html; # redirect server error pages to static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy PHP scripts to Apache listening on 127.0.0.1:80 # location ~\.php { # proxy_pass http://127.0.0.1; # } # proxy 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 $fastcgi_script_name; # include fastcgi_params; # } # deny access to .htaccess files # 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; } }} 转载地址:http://xmcfk.baihongyu.com/