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
Download address:
wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
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
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
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; }
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泄漏,没用过
Restart Nginx: ./sbin/nginx -s reload
Visit: zy.csxiuneng.com, you can see that one of the cookies is route
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/
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!

NGINXUnit improves application performance and manageability with its modular architecture and dynamic reconfiguration capabilities. 1) Modular design includes master processes, routers and application processes, supporting efficient management and expansion. 2) Dynamic reconfiguration allows seamless update of configuration at runtime, suitable for CI/CD environments. 3) Multilingual support is implemented through dynamic loading of language runtime, improving development flexibility. 4) High performance is achieved through event-driven models and asynchronous I/O, and remains efficient even under high concurrency. 5) Security is improved by isolating application processes and reducing the mutual influence between applications.

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINXUnit improves application flexibility and performance with its dynamic configuration and high-performance architecture. 1. Dynamic configuration allows the application configuration to be adjusted without restarting the server. 2. High performance is reflected in event-driven and non-blocking architectures and multi-process models, and can efficiently handle concurrent connections and utilize multi-core CPUs.

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINXUnit supports multiple programming languages and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),