03. Nginx Advanced Usuage
October 12, 2024About 1 min
How to manage session using clustering systems
- ip_hash
Other-Hash: $cookie_jsessionid, $request_uri;
lua logic redirect - Redis + SpringSession
hash
- inner ip_hash
upstream httpget{
ip_hash;
server 192.168.1.1;
server 192.168.1.2;
}
只适用于中小型项目
- Drawbacks:
- ip 集中
- 后端服务器宕机
- $cookie_jsessionid
upstream httpget{
hash $cookie_jsessionid;
server 192.168.1.1;
server 192.168.1.2;
}
- $request_uri
upstream httpget{
hash $request_uri;
server 192.168.1.1;
server 192.168.1.2;
}
- suit for
- No cookies supported
- Uneven distribution of resources
sticky
Sticky on Bitbucket
Sticky on Github
Compile nginx with Sticky:
./configure --prefix=/usr/local/nginx --add-module=/root/nginx-sticky-module-ng-folder
Info
Sometimes there has some errors during compiling because of the version of sticky is too old.
can add the following headers to ngx_http_sticky_misc.h
for resolving some problems caused by openssl
#include <openssl/sha.h>
#include <openssl/md5.h>
How to configure sticky
Basic Configuration
upstream httpget{
sticky;
server 192.168.1.1;
server 192.168.1.2;
}
Configuration Sticky-cookie-name
upstream httpget{
sticky name-NewName;
server 192.168.1.1;
server 192.168.1.2;
}
Info
Need to restart nginx
Keepalive
For some resource, to configue the cache is more suitable, such as javascripts/css.
For some api, it's hard to request the second time, which need to close keepalive.
upstream http_servers {
keepalive 100;
keepalive_requests 1000;
keepalive_timeout 65;
server 192.168.1.1;
server 192.168.1.2;
}
server{
listen 80;
server_name localhost;
location / {
proxy_http_version 1.1; # configure the version of http protocal
# default version is 1.0, need to add header "Connection: keep-alive" (default value is close) to support keep_alive
# http 1.1 support it by default
proxy_set_header Connection ""; # or set as keep-alive
proxy_pass http://http_servers;
}
}
How to configure
http{
# keepalive_timeout 65 65; # Add two value for http protocal 1 to add header `keep Alive: timeout=65`
keepalive_timeout 65; # (units:seconds) set 0 to disable the function
keepalive_time 1h; # Maximum time for keep keepalive
keepalive_request 1000; # The maximum request for one connection
# Be careful with this configuration
send_timeout 60; # The connection will be closed for some time-consuming operation
}
For more configuration please refer to nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive_time
Charles Proxy
Stress test Tool - Apache Benchmark
Install
yum install httpd-tools
Test
ab -n 100000 -c 30 http://192.168.1.1/
How nginx works
Nginx reverse proxy, memory and file buffer
proxy_buffering on;