nginx负载均衡的配置。把请求通过nginx的反向代理分发到多台服务器处理,可以减轻单台服务器的处理压力,提高访问速度。
一、增加nginx负载均衡代码
1.nginx配置文件http模块中增加
#请求处理的服务器列表 upstream zixuephp { server 127.0.0.1:81 weight=1; #服务器1 weight权重,这里是访问6词有一次是这台服务器 server 106.12.107.148:80 weight=5; #服务器2 }
2.请求反向代理
server { listen 80; #多域名用空格隔开 server_name zixuephp.net www.zixuephp.net m.zixuephp.net; #设置默认访问首页 index index.html index.php; #反向代理到服务器池 location / { proxy_pass http://zixuephp; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
二、完整nginx.conf配置文件代码
这里配置的是负载服务器和主服务器为同一台服务器
#定义nginx运行的用户和用户组 user www www; #设置nginx进程数,一般设置为cpu核心数,auto为自动检测 worker_processes auto; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ] error_log logs/error.log; #error_log logs/error.log warn; #error_log logs/error.log info; #error_log logs/error.log debug; #error_log logs/error.log notice; #error_log logs/error.log crit; #守护进程pid文件 pid logs/nginx.pid; #events模块中包含nginx中所有处理连接的设置 events { #单个进程最大连接数(最大连接数=连接数*进程数) worker_connections 1024; #设置nginx收到一个新链接通知后接受尽可能多的链接 multi_accept on; #设置用于复用客户端线程的轮训方法 use epoll; } #http模块控制着nginx http处理的所有核心特性 http { #文件扩展名与文件类型映射表 include mime.types; #默认文件类型 default_type application/octet-stream; #打开或关闭错误页面中的nginx版本号deng server_tokens on; #客户请求头缓冲大小 large_client_header_buffers 4 64k; #设定通过nginx上传文件的大小 client_max_body_size 1024M; client_body_buffer_size 2048k; #开启高效文件传输模式,优化磁盘IO设置 sendfile on; gzip on; #允许或禁止压缩基于请求和相应的响应流,any代表压缩所有请求 gzip_proxied any; #==设置数据压缩等级,1-9之间,9最慢压缩比最大 gzip_comp_level 9; #设置需要压缩的数据格式 gzip_types text/plain text/css text/xml text/javascript application/json application/x-javascript application/xml application/xml+rss; #服务器分发池列表 upstream zixuephp { server 127.0.0.1:81 weight=1; server 106.12.107.148:80 weight=5; } #负载均衡请求分发 server { listen 80; #多域名用空格隔开 server_name zixuephp.net www.zixuephp.net m.zixuephp.net; #设置默认访问首页 index index.html index.php; location / { #所有请求反向代理到服务器池中的服务器 proxy_pass http://zixuephp; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } #虚拟主机配置 server { listen 81; listen 443 ssl; ssl_certificate /usr/local/nginx/ssl/web.crt; ssl_certificate_key /usr/local/nginx/ssl/web.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL; ssl_prefer_server_ciphers on; #多域名用空格隔开 server_name zixuephp.net www.zixuephp.net m.zixuephp.net; #设置默认访问首页 index index.html index.php; #默认编码 charset utf-8; access_log logs/access.log; #错误设置 error_page 500 502 503 504 404 /404.html; #缓存 location ~ .*.(gif|jpg|jpeg|png|bmp)$ { expires 7d; #禁止缓存 #add_header Cache-Control no-store; #valid_referers none blocked server_names *.baidu.com *.google.com; #if ($invalid_referer) { # rewrite ^/ http://zixuephp.gz01.bdysite.com/static/images/no_call_file.jpg; # #return 403; #} } #网站伪静态 location / { rewrite "^/index.html$" /index.php last; rewrite "^/iphp.html$" /iphp.php last; rewrite "^/tag.html$" /tag.php last; rewrite "^/tag-([0-9]+).html$" /tag.php?tid=$1 last; rewrite "^/tag-([0-9]+)-page([0-9]+).html$" /tag.php?tid=$1&page=$2 last; rewrite "^/login.html$" /login.php last; rewrite "^/post.html$" /post.php last; rewrite "^/article.html$" /article.php last; rewrite "^/article-([0-9]+).html$" /article.php?aid=$1 last; rewrite "^/iphp-([0-9]+).html$" /iphp.php?mid=$1; rewrite "^/iphp-([0-9]+)-([0-9]+).html$" /iphp.php?mid=$1&cid=$2 last; rewrite "^/iphp-([0-9]+)-page([0-9]+).html$" /iphp.php?mid=$1&page=$2; rewrite "^/iphp-c([0-9]+)-page([0-9]+).html$" /iphp.php?cid=$1&page=$2; rewrite "^/search.html$" /search.php; rewrite "^/tool.html$" /tool.php last; rewrite "^/tool-html.html$" /tool-html.php last; rewrite "^/tool-ip.html$" /tool-ip.php last; rewrite "^/tool-css.html$" /tool-css.php last; rewrite "^/tool-domain-ip.html$" /tool-domain-ip.php; rewrite "^/tool-view-source-code.html$" /tool-view-source-code.php; rewrite "^/tool-md5.html$" /tool-md5.php; rewrite "^/tool-base64.html$" /tool-base64.php; rewrite "^/tool-url.html$" /tool-url.php; rewrite "^/tool-imagebase64.html$" /tool-imagebase64.php; rewrite "^/about.html$" /about.php; rewrite "^/message.html$" /message.php; rewrite "^/tool-http.html$" /tool-http.php; rewrite "^/tool-apk-md5-sign.html$" /tool-apk-md5-sign.php; rewrite "^/tool-apk-detail.html$" /tool-apk-detail.php; rewrite "^/tool-runcode.html$" /tool-runcode.php; rewrite "^/tool-runcode-static.html$" /tool-runcode-static.php; rewrite "^/tool-http-request.html$" /tool-http-request.php; rewrite "^/ebooks.html$" /ebooks.php; rewrite "^/content.html$" /content.php; rewrite "^/content-([0-9]+).html$" /content.php?id=$1; } #nginx解析php脚本 location ~ .php$ { #屏蔽 File not found. 错误设置 try_files $uri =404; root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; index index.php index.html; } } }