如何搭建Nginx服务器做到负载均衡?
作者:IT科技类资讯 来源:系统运维 浏览: 【大中小】 发布时间:2025-11-04 00:19:47 评论数:

复制#user nobody; #这里是何搭衡核心worker数,一般设置为与cpu核心数相同的服务数目,避免进程切换造成的到负上下文切换耗费资源,cpu信息可以从/proc/cpuinfo中查看 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 { #use epoll model使用epoll模型,载均采用异步非阻塞模型加快处理速度 use epoll; 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; #设定通过nginx上传文件的云服务器何搭衡大小 client_max_body_size 300m; #使用sendfile函数在两个文件描述符之间直接传递数据(完全在内核中操作,传送),服务从而避免了内核缓冲区数据和用户缓冲区数据之间的到负拷贝,操作效率很高,载均被称之为零拷贝。何搭衡 sendfile on; #tcp_nopush on; #keepalive_timeout 0; #连接活跃时间 keepalive_timeout 65;#使用压缩数据减少IO量,IT技术网服务但是到负在不支持数据解压浏览器可能产生乱码 #gzip on; #静态服务器组 #设定静态资源服务器访问接口 upstream static.zh-jieli.com { server localhost:808 weight=1; } #动态服务器组 upstream zh-jieli.com { #设置Hash轮询规则 #ip_hash; #weight: server ip:port weight=10#默认 轮询# fair:按照后端服务器的响应时间来分配 #url_hash:按照url规则进行分配,使得固定的请求分配到固定的服务器上 server localhost:8080; server localhost:8081; } server{ listen 808; server_name static; location / { } location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff) { #所有静态文件直接读取硬盘内容:读取的静态资源存放位置 root /apache-tomcat-8.5.24/webapps/ROOT ; #资源是否进行缓存与缓存时间 expires 30d; #缓存30天 } } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index1.html index.htm; } location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff) { #proxy_cache cache_one; proxy_cache_valid 200 304 302 5d; proxy_cache_valid any 5d; proxy_cache_key $host:$server_port$request_uri; add_header X-Cache $upstream_cache_status from $host; proxy_pass http://static.zh-jieli.com; # 所有静态文件直接读取硬盘 root /apache-tomcat-8.5.24/webapps/ROOT; expires 30d; #缓存30天 } #其他页面反向代理到tomcat容器 location ^~ /tomcat { indexindex; # proxy_pass http://localhost:8080/; #设定代理服务器组 proxy_pass http://zh-jieli.com/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.WordPress模板
