最近学了下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教程有兴趣的朋友有所帮助。