Nginx Tweaks AND Tricks
We encountered an issue while working with WebSockets behind Nginx as our web server — the server crashed after reaching approximately 786 active connections. After investigating, we discovered that Nginx has default limits on the number of simultaneous connections it can handle. To resolve this, we applied specific configuration tweaks to Nginx, which allowed us to increase the limit and support more active connections. These changes helped us stabilise the system and ensure smoother real-time communication. maximum capability of system user@ubuntu:~$ cat /proc/sys/fs/file-max 708444 available limit user@ubuntu:~$ ulimit -n 1024 To increase the available limit to 200000 user@ubuntu:~$ sudo nano /etc/sysctl.conf Add the following line to it fs.file-max = 200000 run this to refresh with the new config user@ubuntu:~$ sudo sysctl -p edit the following file user@ubuntu:~$ sudo nano /etc/security/limits.conf Add the following lines to it soft nproc 200000 hard nproc 200000 soft nofile 200000 hard nofile 200000 root soft nproc 200000 root hard nproc 200000 root soft nofile 200000 root hard nofile 200000 #update root based on your user i.e ubuntu edit the following file user@ubuntu:~$ sudo nano /etc/pam.d/common-session add this line to it session required pam_limits.so logout and login and try the following command user@ubuntu:~$ ulimit -n 200000 now you can increase no.of.connections per Nginx worker and worker_rlimit_nofile in Nginx main config /etc/nginx/nginx.conf worker_rlimit_nofile 200000; worker_connections 200000;#(inside event object)

We encountered an issue while working with WebSockets behind Nginx as our web server — the server crashed after reaching approximately 786 active connections. After investigating, we discovered that Nginx has default limits on the number of simultaneous connections it can handle.
To resolve this, we applied specific configuration tweaks to Nginx, which allowed us to increase the limit and support more active connections. These changes helped us stabilise the system and ensure smoother real-time communication.
maximum capability of system
user@ubuntu:~$ cat /proc/sys/fs/file-max
708444
available limit
user@ubuntu:~$ ulimit -n
1024
To increase the available limit to 200000
user@ubuntu:~$ sudo nano /etc/sysctl.conf
Add the following line to it
fs.file-max = 200000
run this to refresh with the new config
user@ubuntu:~$ sudo sysctl -p
edit the following file
user@ubuntu:~$ sudo nano /etc/security/limits.conf
Add the following lines to it
- soft nproc 200000
- hard nproc 200000
- soft nofile 200000
- hard nofile 200000 root soft nproc 200000 root hard nproc 200000 root soft nofile 200000 root hard nofile 200000 #update root based on your user i.e ubuntu
edit the following file
user@ubuntu:~$ sudo nano /etc/pam.d/common-session
add this line to it
session required pam_limits.so
logout and login and try the following command
user@ubuntu:~$ ulimit -n
200000
now you can increase no.of.connections per Nginx worker and worker_rlimit_nofile
in Nginx main config /etc/nginx/nginx.conf
worker_rlimit_nofile 200000;
worker_connections 200000;#(inside event object)