最近學了下nginx 的負載平衡原理,動手來配一個實例練練手。
1. 前言
我用的環境是 centos7, nginx版本是 1.8.1,具體怎麼安裝,參考我前面的文章。我公司的應用程式伺服器有個環境,一個是開發環境,一個是測試環境,兩個環境的應用是一樣的,但是柯瑞的資料是不同的,這樣也剛好方便等會兒的測試。
2. 設定nginx負載平衡
nginx預設是以conf/nginx.conf 作為啟動配置的,conf/nginx.conf.default是作為nginx.conf的一個備份,兩個檔案的內容完全一樣,這樣我們可以依照自己的需求在nginx.conf 中設定負載平衡,nginx.conf 內容如下:
<code><span>#user nobody; #使用哪个用户启动nginx 前边是用户 后边是组 </span><span>worker_processes</span><span>1</span>; <span># nginx 工作进程数据量(通常为服务器的cpu核数)</span><span># [debug | info | warn | error | crit] 错误日志的级别及位置</span><span>#error_log logs/error.log;</span><span>#error_log logs/error.log notice;</span><span>#error_log logs/error.log info;</span><span># 进程文件</span><span>#pid logs/nginx.pid;</span><span>events</span> { <span># 每个进程的最大连接数</span><span>worker_connections</span><span>1024</span>; } <span># 设置http服务器,利用它的反向代理实现负载均衡支持</span><span>http</span> { <span>include</span> mime.types; <span># 设定mime类型</span><span>default_type</span> application/octet-stream; <span># 默认文件类型</span><span># 设置日志格式</span><span>#log_format main '$remote_addr - $remote_user [$time_local] "$request" '</span><span># '$status $body_bytes_sent "$http_referer" '</span><span># '"$http_user_agent" "$http_x_forwarded_for"';</span><span>#access_log logs/access.log main;</span><span>sendfile</span><span>on</span>; <span>#开启高效文件传输模式</span><span># 以下两个选项用于防止网络阻塞</span><span>#tcp_nopush on;</span><span>#tcp_nodelay on;</span><span>##tcp_nopush 这个参数只有 sendfile on 的时候才有用。tcp_nodelay 只在 keepalive 连接状态中使用。</span><span># 超时时间</span><span>keepalive_timeout</span><span>65</span>; <span># 开启gzip模块</span><span>#gzip on;</span><span># 负载均衡配置</span><span>upstream</span> myproject { <span># 默认以轮询策略</span><span>server</span><span>192.168.1.111</span>; <span>#开发环境ip</span><span>server</span><span>192.168.1.114</span>; <span>#测试环境ip</span> } <span># 虚拟代理服务器配置</span><span>server</span> { <span>listen</span><span>80</span>; <span># 服务器名称,随便起名</span><span>server_name</span> nginx_proxy; <span>#charset koi8-r;</span><span>#access_log logs/host.access.log main;</span><span>location</span> / { <span># root html;</span><span># index index.html index.htm;</span><span>#设置主机头和客户端真实地址,以便服务器获取客户端真实IP</span><span>proxy_set_header</span> Host <span>$host</span>; <span>proxy_set_header</span> X-Real-IP <span>$remote_addr</span>; <span>proxy_set_header</span> X-Forwarded-For <span>$proxy_add_x_forwarded_for</span>; <span>#禁用缓存</span><span>proxy_buffering</span><span>off</span>; <span># 反向代理的地址</span><span>proxy_pass</span><span>http://myproject</span>; } <span>#error_page 404 /404.html;</span><span># redirect server error pages to the static page /50x.html</span><span>#</span><span>#error_page 500 502 503 504 /50x.html;</span><span>#location = /50x.html {</span><span># root html;</span><span>#}</span><span># proxy the PHP scripts to Apache listening on 127.0.0.1:80</span><span>#</span><span>#location ~ \.php$ {</span><span># proxy_pass http://127.0.0.1;</span><span>#}</span><span># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span><span>#</span><span>#location ~ \.php$ {</span><span># root html;</span><span># fastcgi_pass 127.0.0.1:9000;</span><span># fastcgi_index index.php;</span><span># fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;</span><span># include fastcgi_params;</span><span>#}</span><span># deny access to .htaccess files, if Apache's document root</span><span># concurs with nginx's one</span><span>#</span><span>#location ~ /\.ht {</span><span># deny all;</span><span>#}</span> } <span># another virtual host using mix of IP-, name-, and port-based configuration</span><span>#</span><span>#server {</span><span># listen 8000;</span><span># listen somename:8080;</span><span># server_name somename alias another.alias;</span><span># location / {</span><span># root html;</span><span># index index.html index.htm;</span><span># }</span><span>#}</span><span># HTTPS server</span><span>#</span><span>#server {</span><span># listen 443 ssl;</span><span># server_name localhost;</span><span># ssl_certificate cert.pem;</span><span># ssl_certificate_key cert.key;</span><span># ssl_session_cache shared:SSL:1m;</span><span># ssl_session_timeout 5m;</span><span># ssl_ciphers HIGH:!aNULL:!MD5;</span><span># ssl_prefer_server_ciphers on;</span><span># location / {</span><span># root html;</span><span># index index.html index.htm;</span><span># }</span><span>#}</span>}</code>
3. Nginx常用指令
<code><span>#测试nginx配置:</span> nginx -t /usr/local/nginx/conf/fzjh.conf <span>#启动、关闭</span> ./sbin/nginx <span># 默认配置文件 conf/nginx.conf,-c 指定配置文件启动</span> ./sbin/nginx <span>-s</span> stop 或 pkill nginx <span>#重启,不会改变启动时指定的配置文件</span> ./sbin/nginx <span>-s</span> reload</code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介紹了Nginx 負載平衡配置實例,包含了nginx,負載平衡方面的內容,希望對PHP教程有興趣的朋友有所幫助。