Home Operation and Maintenance Nginx What is the upstream configuration and function of nginx?

What is the upstream configuration and function of nginx?

May 30, 2023 pm 10:28 PM
nginx upstream

Configuration example

upstream backend {
  server backend1.example.com    weight=5;
  server backend2.example.com:8080;
  server unix:/tmp/backend3;

  server backup1.example.com:8080  backup;
  server backup2.example.com:8080  backup;
}

server {
  location / {
    proxy_pass http://backend;
  }
}
Copy after login

Command

Syntax:upstream name { ... }
Default:
Context:http

Define a group of servers. These servers can listen on different ports. Furthermore, servers listening on TCP and UNIX domain sockets can be mixed.

Example:

upstream backend {
  server backend1.example.com weight=5;
  server 127.0.0.1:8080    max_fails=3 fail_timeout=30s;
  server unix:/tmp/backend3;
}
Copy after login

By default, nginx distributes requests to each server in a weighted rotation. In the above example, every 7 requests will be distributed in the following way: 5 requests are distributed to backend1.example.com, 1 request is distributed to the second server, and 1 request is distributed to the third server. If an error occurs while communicating with the server, the request will be passed to the next server until all available servers have been tried. If all servers return failure, the client will get a (failure) response from the last server it communicated with.

Syntax:server address [parameters];
Default value:
Context:upstream

Define the server's address address and other parameters. The address can be a domain name or an IP address, and the port is optional, or a path to a unix domain socket specifying the "unix:" prefix. If no port is specified, port 80 is used. If a domain name resolves to multiple IPs, multiple servers are essentially defined.

You can define the following parameters: weight=number sets the weight of the server. The default is 1. max_fails=number sets the number of failed attempts by nginx to communicate with the server. Within the time period defined by the fail_timeout parameter, if the number of failures reaches this value, nginx will consider the server to be unavailable. During the next fail_timeout period, the server will not be tried again. The number of failed attempts defaults to 1. Setting it to 0 will stop counting attempts and consider the server to be always available. You can configure what is considered a failed attempt via the directives proxy_next_upstream, fastcgi_next_upstream and memcached_next_upstream. When configured by default, the http_404 status is not considered a failed attempt. fail_timeout=time sets the time period for

  • # to count the number of failed attempts. During this period, if the server fails for the specified number of attempts, the server is considered unavailable.

  • The period of time during which the server is considered unavailable.

By default, the timeout is 10 seconds. backup is marked as a backup server. When the main server is unavailable, requests will be passed to these servers. down marks the server as permanently unavailable and can be used with the ip_hash directive.

example:

upstream backend {
  server backend1.example.com   weight=5;
  server 127.0.0.1:8080      max_fails=3 fail_timeout=30s;
  server unix:/tmp/backend3;

  server backup1.example.com:8080 backup;
}
Copy after login
Syntax:ip_hash;
Default Value:
Context:upstream

Specifies the load for the server group Balanced method, requests are distributed among servers based on the client's IP address. The first three bytes of the IPv4 address, or the entire IPv6 address, will be used as a hash key. This method ensures that requests from the same client will be passed to the same server. Except when the server is considered unavailable, these client requests will be passed to other servers, most likely the same server.

IPv6 addresses are supported starting from versions 1.3.2 and 1.2.2.

If one of the servers wants to be temporarily removed, the down parameter should be added. This preserves the current client IP address hash distribution.

Example:

upstream backend {
  ip_hash;

  server backend1.example.com;
  server backend2.example.com;
  server backend3.example.com down;
  server backend4.example.com;
}
Copy after login

Starting from versions 1.3.1 and 1.2.2, the load balancing method of ip_hash only supports setting the server weight value.

Syntax:keepalive connections;
Default value:
Context:upstream

这个指令出现在版本 1.1.4.

激活对上游服务器的连接进行缓存。

connections参数设置每个worker进程与后端服务器保持连接的最大数量。这些保持的连接会被放入缓存。 如果连接数大于这个值时,最久未使用的连接会被关闭。

需要注意的是,keepalive指令不会限制nginx进程与上游服务器的连接总数。 新的连接总会按需被创建。 connections参数应该稍微设低一点,以便上游服务器也能处理额外新进来的连接。

配置memcached上游服务器连接keepalive的例子:

upstream memcached_backend {
  server 127.0.0.1:11211;
  server 10.0.0.2:11211;

  keepalive 32;
}

server {
  ...

  location /memcached/ {
    set $memcached_key $uri;
    memcached_pass memcached_backend;
  }

}
Copy after login

对于http代理,proxy_http_version指令应该设置为“1.1”,同时“connection”头的值也应被清空。

upstream http_backend {
  server 127.0.0.1:8080;

  keepalive 16;
}

server {
  ...

  location /http/ {
    proxy_pass http://http_backend;
    proxy_http_version 1.1;
    proxy_set_header connection "";
    ...
  }
}
Copy after login

另外一种选择是,http/1.0协议的持久连接也可以通过发送“connection: keep-alive”头来实现。不过不建议这样用。

对于fastcgi的服务器,需要设置 fastcgi_keep_conn 指令来让连接keepalive工作:

upstream fastcgi_backend {
  server 127.0.0.1:9000;

  keepalive 8;
}

server {
  ...

  location /fastcgi/ {
    fastcgi_pass fastcgi_backend;
    fastcgi_keep_conn on;
    ...
  }
}
Copy after login

当使用的负载均衡方法不是默认的轮转法时,必须在keepalive 指令之前配置。

针对scgi和uwsgi协议,还没有实现其keepalive连接的打算。

语法: least_conn;
 
默认值:
上下文: upstream

这个指令出现在版本 1.3.1 和 1.2.2.

指定服务器组的负载均衡方法,根据其权重值,将请求发送到活跃连接数最少的那台服务器。 如果这样的服务器有多台,那就采取有权重的轮转法进行尝试。

嵌入的变量

ngx_http_upstream_module模块支持以下嵌入变量:

$upstream_addr保存服务器的ip地址和端口或者是unix域套接字的路径。 在请求处理过程中,如果有多台服务器被尝试了,它们的地址会被拼接起来,以逗号隔开,比如: “192.168.1.1:80, 192.168.1.2:80, unix:/tmp/sock”。 如果在服务器之间通过“x-accel-redirect”头或者error_page有内部跳转,那么这些服务器组之间会以冒号隔开,比如:“192.168.1.1:80, 192.168.1.2:80, unix:/tmp/sock : 192.168.10.1:80, 192.168.10.2:80”。$upstream_response_time以毫秒的精度保留服务器的响应时间,(输出)单位是秒。 出现多个响应时,也是以逗号和冒号隔开。$upstream_status保存服务器的响应代码。 出现多个响应时,也是以逗号和冒号隔开。$upstream_http_...保存服务器的响应头的值。比如“server”响应头的值可以通过$upstream_http_server变量来获取。 需要注意的是只有最后一个响应的头会被保留下来。

The above is the detailed content of What is the upstream configuration and function of nginx?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

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.

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

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.

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to solve nginx304 error How to solve nginx304 error Apr 14, 2025 pm 12:45 PM

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.

How to solve nginx403 error How to solve nginx403 error Apr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to solve nginx403 How to solve nginx403 Apr 14, 2025 am 10:33 AM

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

See all articles