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

Source: Internet
Author: User

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;

Continue

You can use $ _ server ["..."] To set PHP parameters.

Location ~ \. Php $ {
Root htmll;
Fastcgi_pass 127.0.0.1: 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;

}

Explanation:

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
}

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

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.