Nginx Reverse proxy-related load balancing

Source: Internet
Author: User
Tags nginx reverse proxy

One. Reverse Proxy

1.1.upstream Introduction

Nginx upstream can simultaneously implement reverse proxy and load balancing, currently upstream supports 4 ways of distribution

1. Polling (default)

Each request is assigned to a different back-end server in chronological order, and can be automatically rejected if the backend server is down.

2, Weight

Specifies the polling probability, proportional to the weight and access ratios, for situations where the performance of the backend server is uneven.

2, Ip_hash

Each request is allocated according to the hash result of the access IP, so that each visitor has fixed access to a back-end server that resolves the session issue.

3. Fair (third party)

The response time of the back-end server is allocated to the request, and the response time is short of priority allocation.

4. Url_hash (third Party)

Assign requests by the hash result of the access URL so that each URL is directed to the same back-end server, which is more efficient when the backend server is cached.

Load Balancing:

Only need to add in HTTP

Upstream Tgcluster {#定义负载均衡设备的Ip及设备状态

Ip_hash;

Server 127.0.0.1:9090 down;

Server 127.0.0.1:8080 weight=2;

Server 127.0.0.1:6060;

Server 127.0.0.1:7070 backup;

}

In servers that need to use load balancing, add

Proxy_pass http://tgcluster/;

The status of each device is set to:

1.down indicates that the server is temporarily not participating in the load

2.weight by default, the larger the 1.weight, the greater the load weight.

3.max_fails: The number of times that a request failed is allowed defaults to 1. Returns the error defined by the Proxy_next_upstream module when the maximum number of times is exceeded

4.fail_timeout:max_fails the time of the pause after the failure.

5.backup: When all other non-backup machines are down or busy, request the backup machine. So the pressure on this machine is the lightest.

1.2 Implementing a Reverse proxy instance

re-create a new file under the/etc/nginx/conf.d/directory proxy . conf, and do the configuration:

Vim/etc/nginx/conf.d/proxy.conf

# # Basic Reverse proxy Server # #

Upstream apachephp {

server 1.1.1.1:80; #the IP and port of the background Apache

}

# # Start Www.nowamagic.net # #

server {

Listen 80;

server_name www.111.com www.222.com;

# access_log logs/proxy. Access.log;

Error_log logs/proxy. Error.log;

root HTML;

Index index.html index.htm index.php index.do;

# # Send request back to Apache # #

Location/{

Proxy_pass http://apachephp;

#Proxy Settings

Proxy_redirect off;

Proxy_set_header Host $host;

Proxy_set_header X-real-ip $remote _addr;

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;

Proxy_next_upstream Error timeout Invalid_header http_500 http_502 http_503 http_504;

Proxy_max_temp_file_size 0;

Proxy_connect_timeout 90;

Proxy_send_timeout 90;

Proxy_read_timeout 90;

Proxy_buffer_size 4k;

Proxy_buffers 4 32k;

Proxy_busy_buffers_size 64k;

Proxy_temp_file_write_size 64k;

}

}


Explanatory notes:

apachephp defines a module that indicates the background of the real Apache IP and port, and configures the virtual Host server module below

To use.


The listen 80 under the server module represents the port that the Nginx service listens on.

server_name www.111.com www.222.com; Here to configure some virtual host, your domain name to directly point to the address of the Nginx proxy server, when you access these domain names, the Nginx Agent will implement forwarding, forwarded to 1.1.1.1:80.

Http://apachephp: Here is a reference to the apachephp module just defined!


Two. Load Balancing

I feel the load balancer is almost the same as the above configuration, but add a few more real Apache Web servers to this upstream module.

Upstream apachephp {

server 1.1.1.1:80; #the IP and port of the background Apache

}


For more detailed parameters and configuration, please refer to other people's blogs!





Nginx Reverse proxy-related load balancing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.