최근에 nginx의 로드밸런싱 원리를 배웠는데, 예제를 통해 실습해보겠습니다.
1. 머리말
제가 사용하는 환경은 centos7이고, nginx 버전은 1.8.1 입니다. 자세한 설치 방법은 제 이전 글을 참고해주세요. 우리 회사의 애플리케이션 서버에는 두 가지 환경이 있습니다. 하나는 개발 환경이고 다른 하나는 테스트 환경입니다. 두 환경의 애플리케이션은 동일하지만 라이브러리의 데이터가 다르기 때문에 나중에 테스트하기에 편리합니다.
2. nginx 로드 밸런싱 구성
nginx는 시작 구성으로 기본적으로 conf/nginx.conf, conf/nginx.conf.default는 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. 🎜>
<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>