littlebizzy / slickstack

Lightning-fast WordPress on Nginx
https://slickstack.io
GNU General Public License v3.0
633 stars 112 forks source link

Implement Server-Wide Connection Limit to Mitigate Request Overload #211

Closed icodeforlove closed 6 months ago

icodeforlove commented 6 months ago

Implementing a limit on connections per IP address is beneficial, but it could be even more effective to establish a shared limit zone for the entire server. This approach allows you to cap the number of open connections on a server-wide basis. For instance, you can configure:

limit_conn_zone $server_name zone=per_server:10m;
limit_conn per_server 100; // As an example

This setup helps in mitigating the accumulation of requests that might overload systems behind NGINX. Since NGINX inherently queues up incoming requests without a built-in timeout for each, the backlog can grow significantly if the backend server is unresponsive. By setting a server-wide limit, it becomes easier to manage and prevent such issues from escalating.

The current implementation only binds limits to the remote address: https://github.com/littlebizzy/slickstack/blob/96215dc804abda6399df4ede3138f29e366474db/modules/nginx/nginx-conf.txt#L283

Delving into more sophisticated strategies, one could continuously monitor the access logs to gauge the frequency of timeouts occurring over a specified period. Should this frequency escalate to an unusually high level, indicative of potential overload or attack, an automated process could intervene by applying an updated nginx.conf configuration that enforces a stricter limit_conn per_server rule. This preemptive measure aims to mitigate the immediate impact by curtailing the number of concurrent connections allowed. Following a designated interval of stability, where the rate of issues falls back to normal levels, the system could then revert to its original configuration. This dynamic response mechanism ensures that the server remains both protected during periods of high demand or attack and optimally accessible under normal conditions, balancing security with user experience.

icodeforlove commented 6 months ago

@jessuppi already added this here:

https://github.com/littlebizzy/slickstack/commit/914a1b3a374d50a77df9848bdb5dac49823e5c89#diff-c3888e4f88d8fbfd304c3b77adea5362fdca3348f0097d6eaef87ca859f0b00aR288