


Nginx load balancing configuration example Zhang Yan f5 load balancing nginx tcp load balancing
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>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
