Home  >  Article  >  Backend Development  >  Nginx load balancing configuration example Zhang Yan f5 load balancing nginx tcp load balancing

Nginx load balancing configuration example Zhang Yan f5 load balancing nginx tcp load balancing

WBOY
WBOYOriginal
2016-07-29 08:54:261536browse

I recently learned the load balancing principle of nginx, and I will practice it with an example.

1. Preface

The environment I use is centos7, and the nginx version is 1.8.1. For details on how to install it, please refer to my previous article. My company's application server has one environment, one is the development environment, and the other is the testing environment. The applications in the two environments are the same, but the data in the library is different, which is convenient for testing later.

2. Configure nginx load balancing

nginx defaults to conf/nginx.conf as the startup configuration, conf/nginx.conf.default is a backup of nginx.conf, the contents of the two files are exactly the same, so we You can configure load balancing in nginx.conf according to your own needs. The content of nginx.conf is as follows:

<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 common commands

<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); }); });

The above introduces Nginx load balancing configuration examples, including nginx and load balancing content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn