Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nginx text-reverse proxy

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Describe the configuration file and explanation of the reverse seven-tier agent

Nginx official website: https://www.nginx.com/

Centos yum install nginx

Yum install nginx-y

So, let's ask you a question first, What? Why use Nginx to implement a seven-tier agent? what is a seven-tier proxy?

1. The difference between nginx proxy and not

Traditional nginx web-free architecture:

Look at the following picture:

First of all, we have a domain name www.test_app.com, and there are three servers at the back end, all of which are running PHP+Nginx services. if we want to realize that all three servers provide website services, we need to have the following conditions (disadvantages).

1. Each web server needs to have a fixed public network IP address (high cost)

2. A domain name needs to correspond to multiple public network IP (poor scalability)

3. Each additional web server needs to use dns configuration and purchase fixed public network IP (poor flexibility)

So, the drawbacks are obvious.

Nginx proxy architecture:

At this time, we can deploy a nginx proxy (reverse proxy) in the front end to solve the problem mentioned above. We can configure proxy pool in nginx proxy, which is called proxy pool, and fill in the IP:PORT of the back-end web node. Each time you expand a Web node, you only need to add IP:PORT to the proxy pool, and then nginx in the reload, and the nginx proxy only needs to have a fixed public network IP. DNS only needs to bind the public network IP

2. Introduction to Nginx Proxy

The load balancing function of Nginx depends on the ngx_http_upsteam_module module, and the supported proxy methods include proxy_pass,fastcgi_pass,memcached_pass and so on. The new version of Nginx software supports more ways. This article mainly explains the way of proxy_pass agent.

The ngx_http_upstream_module module allows Nginx to define one or more groups of node server groups. When using it, you can send the request of the website to the name of the corresponding Upstream group defined in advance through proxy_pass proxy, which is written as "proxy_pass http:// www_server_pools", where www_server_pools is the name of a Upstream node server group.

Some parameters of the server tag inside the upstream module are described:

The module parameters indicate the RS address behind the server load balancer, but the domain name or IPweight server weight. The higher the weight number, the more requests. The default is the number of failed attempts by 1max_fils to connect to the backend node. The default value is 1backup, which means that the node as the backup server fail_timeout fails to define the number of times after max_fails, and the interval between the next check

3. Nginx Proxy scheduling algorithm

Static scheduling algorithm:

The algorithm explains that rr polling, the default scheduling algorithm, allocates requests to different back-end nodes wrr weight polling sequentially. The greater the weight value, the more requests the back-end node accepts, and the more ip_hash session persistence is accepted by the back-end node. Each request is assigned to the back-end node according to the Hash result of the client's IP.

Dynamic scheduling algorithm:

The algorithm explains the fair response time, allocates the minimum number of request least_conn connections according to the response time of the back-end node, allocates the request url_hashweb cache node according to the number of connections of the back-end node, and allocates the request according to the hash result of the visited URL

Note that only one scheduling algorithm can be used.

Nginx proxy common configuration files (multiple domain names):

Worker_processes 1 server events {worker_connections 1024;} http {include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # this is the proxy pool, pool name: blog_server_pools, add the IP:PORT/ domain name of the web node to this upstream blog_server_pools {server 172.16.1.7 80 weight=3 max_fils=3 fail_timeout=5 Server 172.16.1.8:80 weight=2 max_fils=3 fail_timeout=5; server 172.16.1.9:80 weight=1 backup max_fils=3 fail_timeout=5 } upstream www_server_pools {# use scheduling algorithm: ip_hash ip_hash server 172.16.1.10 max_fils=3 fail_timeout=5; server 80 max_fils=3 fail_timeout=5; server 172.16.1.11 max_fils=3 fail_timeout=5 Server 172.16.1.12 weight=1 backup max_fils=3 fail_timeout=5; 80} server {listen 80; server_name blog.youngboy.org; location / {# calls the corresponding proxy pool proxy_pass http://blog_server_pools; through proxy_pass # proxy optimization parameters (explained below) proxy_redirect default; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 32k Proxy_buffers 4 128k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k;}} server {listen 80; server_name www.youngboy.org; location / {proxy_pass http://www_server_pools; proxy_redirect default Proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 32k; proxy_buffers 4 128k; proxy_busy_buffers_size 256k Proxy_temp_file_write_size 256k;}

We'll find out. Every time you add a server tag (a site), you need to paste the nginx proxy parameter once, which will lead to configuration redundancy.

We can write the proxy optimization parameter configuration to a file

# vim / etc/nginx/conf.d/http.proxyproxy_redirect default;proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;proxy_connect_timeout 60th proxyroomsendworthy timeout 60 × proxyroomreadreadout timeout 60th proxybirthday buffers size 32kt proxycake buffers 4 128k proxy bussiness buffers size 256kproxyhands temptation fileholders writecake size 256k

Under the nginx http tag, use include to import the proxy file you just created

.. http {include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include / etc/nginx/conf.d/http.proxy;....}

Smooth restart of nginx

Nginx-s reload

4. Nginx Proxy optimization parameters.

# set the http request header entry to the backend server node proxy_set_header# to specify the size of the client request body buffer client _ body_buffer_size # indicates the timeout for the connection between the reverse proxy and the backend node server, that is, the timeout time for initiating a handshake and waiting for a response # indicates the data return time of the proxy backend server, that is, the server must transmit all the data within a specified time Otherwise, Nginx will disconnect the connection proxy_sed_simeout # set the time for Nginx to obtain information from the agent's back-end server, indicating that after the connection is successfully established, Nginx waits for the response time of the back-end server. In fact, it is the time that Nginx has entered the backend party waiting for processing. Proxy_read_timeout # sets the buffer size. By default the buffer size is equal to the size set by the instruction proxy_buffers proxy_buffer_size # sets the number and size of buffers. The response information obtained by Nginx from the server at the back end of the proxy prevents the buffer proxy_buffers # from being used to set the proxy_buffers size that the system can use when the system is busy. The officially recommended size is proxy_buffers*2proxy_busy_buffers_size # which specifies the size of the proxy cache temporary file proxy_temp_file_write_size

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report