Nginx Server Load balancer transmits the parameter method to the backend (the backend is also an nginx server), and nginx Server Load balancer

Source: Internet
Author: User

Nginx Server Load balancer transmits the parameter method to the backend (the backend is also an nginx server), and nginx Server Load balancer

A website uses nginx for load balancing and multiple nginx servers at the backend.

Encountered a problem, when it is used as SSL support, the front-end nginx is distributed to the back-end nginx and becomes an http form (in this way, the backend php cannot be judged by $ _ SERVER [HTTPS] = "on, but the backend must also know this parameter). If you change to https, you also need multiple certificates.

Therefore, we want the front-end nginx to pass a parameter to the backend if it is https. Used in the background for judgment and use.

First, use proxy_set_header in the nginx. conf file of the front-end nginx to set a parameter.UrlprefixThe value isHttps(It is recommended that you do not use underlines and hyphens for parameter names. The underlined parameters are ignored by nginx, while the underlined parameters are underlined by nginx, in order to avoid confusion, we simply don't need them. What should he do:

Location /{
Proxy_pass http: // myserver;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Proxy_set_header Host $ http_host;
Proxy_set_header urlprefix https;

Enable the read header and set php parameters in the backend nginx server configuration.

Add underscores_in_headers on to the nginx. conf file;


You can use $ _ SERVER ["..."] To set php parameters.

Location ~ \. Php $ {
Root htmll;
Fastcgi_pass 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/usr/local/nginx/html $ fastcgi_script_name;
Fastcgi_param URL_PREFIX $ http_urlprefix;
Fastcgi_connect_timeout 300;

Include fastcgi. conf;



Fastcgi_param URL_PREFIX $ http_urlprefix;(The parameter name set at the front end isUrlprefixHere, the read isHttp_urlprefixThis is because http _ is added before all parameter names when nginx processes parameters. In additionFastcgi_param URL_PREFIXThis parameter name is case sensitive and does not need to be prefixed during php reading .)

Then, the backend php can use $ _ SERVER ['url _ prefix'] to obtain the parameters we set at the front end.

If (isset ($ _ SERVER ['url _ prefix']) & $ _ SERVER ['url _ prefix'] = "https "){
// Barabajara

How to distribute multiple nginx instances and achieve Load Balancing for large domestic websites?

This information is basically incomplete. Let me talk about a basic architecture:
1. If you have sufficient funds for the DNS server, we recommend using the BGP data center. Two or three DNS servers are balanced. bind software is usually used. If the funds are tight, you can purchase professional dns services, such as dnspod in China.
2. For CDN servers, you can buy services from professional companies, such as chinacache, if you want to save time in the first place. However, the development costs will increase. If you build your own servers, you may set up servers in different data centers, such as China Telecom, China Unicom, and China Mobile, and use dns for dynamic resolution. For ultra-large websites, you can use Squid, nginx for medium to large sizes, and varnish for internal use.
3. Balance the front-end. If you have enough funds, you can use up to 100,001 hardware devices. If you already have a technical team, use nginx/haproxy + keepalived to build your own front-end. Balanced methods are flexible, with random, weight, ip, and url options.
4. Synchronization depends on what to synchronize. Common files can be synchronized in real time. However, if the database is used, you must select the synchronization mode for the specific type.
5. the backend application servers and database clusters should be based on traffic planning.

An error occurred while using nginx for load balancing in linux.

You need to set the method for accessing the backend server, such as round robin or ip hash, to switch.

Server weight = 5 max_fails = 3 fail_timeout = 10 s;
Server weight = 5 max_fails = 3 fail_timeout = 10 s;

I wrote it like this. See it.

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: 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.