nginx adds third-party modules

王林
Release: 2020-10-26 16:23:30
forward
3190 people have browsed it

nginx adds third-party modules

Purpose:

Add a plug-in written by a third party, taking nginx-sticky-module as an example, hereafter referred to as sticky

through/usr/local /nginx/sbin/nginx -V View nginx installed modules

(Recommended tutorial: nginx tutorial)

sticky module and Ip_hash are both related to the load balancing algorithm , but there are differences. The difference is:

1. IP hash, allocates requests to different servers based on the client’s IP

2. Sticky, based on the cookie given to the client by the server, When the client requests again, it will bring this cookie, and nginx will forward the request with this cookie to the server that issued the cookie.

Note: There are 3 computers in a LAN, and they have 3 intranet IPs , but when they initiate a request, there is only one external IP, which is allocated by the telecom operator on the router they are connected to. If the ip_hash method is used, Nginx will distribute the request to different upstream servers. If the sticky module is used, it will Distribute requests to the server using cookies to achieve: Balance of intranet NAT users. This is something iphash cannot do

Sticky works:

Sticky is a load balancing solution based on cookies. By distributing and identifying cookies, requests from the same client fall into On the same server, the default cookie identification name is route:

1. The client initiates an access request for the first time. After nginx receives it, it finds that there is no cookie in the request header, and then distributes the request to the back-end server in a polling manner.

2. After the backend server processes the request, it returns the response data to nginx.

3. At this time, nginx generates a cookie with route and returns it to the client. The value of route corresponds to the back-end server. It may be plain text or hash values ​​such as md5 and sha1.

4. The client receives the request and saves the cookie with route.

5. When the client sends a request next time, it will bring the route, and nginx will forward it to the corresponding back-end server based on the route value in the received cookie.

Sticky official website address

Official address:

https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/src
Copy after login

Download address:

wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
Copy after login

Nginx installation Sticky module

#1.下载的文件上传,解压
tar -xvzf nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar

#2.重命名为nginx-sticky-module
mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 /usr/local/nginx-sticky-module

#3.进入nginx源码目录进行编译
./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx-sticky-module --with-http_stub_status_module --with-http_ssl_module 

#4.安装
 1.停止nginx后进行安装:make && make install
 2.在线更新安装: make upgrade
Copy after login

The installation is complete , check the compilation parameters through ./sbin/nginx -V, you can see that the sticky module has been compiled into nginx

[root@bogon nginx]# ./sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --add-module=/usr/local/nginx-sticky-module --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
Copy after login

Modify nginx.conf and enable the sticky function

upstream zyi {
    #使用sticky,不设置expires则浏览器关闭时结束会话
    sticky domain=zy.csxiuneng.com path=/;
    server localhost:9001;
}

server {
     listen       80;
     server_name  zy.csxiuneng.com;
     access_log  logs/zy.access.log  main;
     location / {
        
         proxy_pass http://zyi;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Host $host;
         client_max_body_size 10m;
         client_body_buffer_size 256k;
         proxy_connect_timeout 90;
         proxy_send_timeout 90;
         proxy_buffer_size 4k;
         proxy_buffers 4 32k;
     }
Copy after login

sticky Syntax:

sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] 
       [hash=index|md5|sha1] [no_fallback] [secure] [httponly];
    [name=route]       设置用来记录会话的cookie名称
    [domain=.foo.bar]    设置cookie作用的域名
    [path=/]          设置cookie作用的URL路径,默认根目录
    [expires=1h]        设置cookie的生存期,默认不设置,浏览器关闭即失效
    [hash=index|md5|sha1]   设置cookie中服务器的标识是用明文还是使用md5值,默认使用md5
    [no_fallback]       设置该项,当sticky的后端机器挂了以后,nginx返回502 (Bad Gateway or Proxy Error) ,而不转发到其他服务器,不建议设置
    [secure]          设置启用安全的cookie,需要HTTPS支持
    [httponly]         允许cookie不通过JS泄漏,没用过
Copy after login

Restart Nginx: ./sbin/nginx -s reload

Visit: zy.csxiuneng.com, you can see that one of the cookies is route

nginx adds third-party modules

Note:

1. If the same client initiates multiple requests at the same time during startup, it may fall on different back-end servers
2. Since the cookie is initially placed by the server If the client disables cookies, the cookies will not take effect.
3. The client may not bring cookies. When the Android client sends a request, it generally does not bring all cookies. It is necessary to clearly specify which cookies will be brought. If you want to use sticky for load balancing, please add cookies to Android development.
4. The cookie name should not have the same name as the cookie used by the business. Sticky's default cookie name is route, which can be changed to any value
5. The first request sent by the client does not include a cookie. The cookie issued by the server will only take effect on the client's next request.
6.Nginx sticky module cannot be used with ip_hash at the same time

If you want to add multiple third-party modules, use multiple --add-module instructions:

./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx-sticky-module/ --add-module=/usr/local/nginx-http-concat-1.2.2/
Copy after login

The above is the detailed content of nginx adds third-party modules. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template