NGINX工作过程和连接
设置 worker_processes 为 auto(即 CPU 核心数)以充分利用多核性能;2. 根据系统文件描述符限制和预期流量设置 worker_connections(如 1024 或更高),确保 ulimit -n 足够大;3. 最大并发连接数 = worker_processes × worker_connections,合理配置可支持数千至数万连接,避免瓶颈,提升 Nginx 生产环境性能。
When setting up Nginx for production use, two key directives—worker_processes
and worker_connections
—directly impact performance, scalability, and how efficiently Nginx handles traffic. Here's what you need to know:

✅ What Are worker_processes
and worker_connections
?
worker_processes
:
This tells Nginx how many worker processes to run. Each process is a separate instance that handles requests independently (leveraging multiple CPU cores).-
worker_connections
:
This sets the maximum number of simultaneous connections each worker process can handle.
Together, they define Nginx’s total connection capacity:
Max clients = worker_processes × worker_connections
? How to Set worker_processes
Best Practice:
Set this equal to the number of CPU cores on your server.

Auto-detect (recommended):
worker_processes auto;
Nginx will automatically match the number of available CPU cores—simple and effective.
Manual (if needed):
worker_processes 4; # for a 4-core CPU
? Why not more than CPU cores?
Extra processes won’t improve performance and may cause overhead due to context switching.
? How to Set worker_connections
This depends on your expected traffic and system limits.
Example:
events { worker_connections 1024; }
Check system limits first: Run:
ulimit -n
If this is less than
worker_connections × worker_processes
, you’ll hit errors.
To increase it temporarily:ulimit -n 4096
Also update
/etc/security/limits.conf
for persistence:* soft nofile 65536 * hard nofile 65536
⚠️ Each connection (client → Nginx) uses one file descriptor. If you proxy to upstream servers, each connection to the backend also counts—so plan for more than just client connections.
? Real-World Example
Say you have:
- 4 CPU cores
- Each worker handles 1024 connections
Then:
Max concurrent connections = 4 × 1024 = 4096
That’s enough for moderate traffic. For high-traffic sites, scale up:
worker_processes auto; events { worker_connections 4096; }
→ Supports ~8K–16K connections depending on CPU count.
✅ Quick Tips
- Always test with
nginx -t
before reloading. - Monitor with
htop
ortop
to see if workers are under-utilized or maxed out. - Use
ab
orwrk
to simulate load and verify limits. - Don’t forget to adjust
keepalive_timeout
and enablekeepalive
for upstreams to reduce connection churn.
Basically, set worker_processes auto
, tune worker_connections
based on your traffic and system limits, and monitor. It’s not complex—but getting it right avoids bottlenecks early.
以上是NGINX工作过程和连接的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undress AI Tool
免费脱衣服图片

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

启用HSTS的方法是在HTTPS网站中配置Strict-Transport-Security响应头,具体操作为:1.Nginx在server块添加add_header指令;2.Apache在配置文件或.htaccess添加Header指令;3.IIS在web.config添加customHeaders;需确保站点已完整支持HTTPS,参数包括max-age(有效期)、includeSubDomains(子域名生效)、preload(预加载列表),提交到HSTSPreload列表前提包括根域名和子

要配置Nginx的SSL/TLS服务,需准备证书和私钥并在serverblock中设置相关参数。1.准备证书文件:获取.crt或.pem格式的证书及对应的.key私钥,可使用Let'sEncrypt或商业机构颁发,并合并中间证书至bundle文件;2.配置serverblock:在站点配置文件中定义listen443ssl、ssl_certificate路径为/etc/ssl/example.com.crt、ssl_certificate_key路径为/etc/ssl/example.com.k

修改Nginx配置后应先测试语法再重载服务。1.使用nginx-t检查配置文件语法,若提示“syntaxisok”和“testissuccessful”则表示无误;若有错误会显示具体问题行。2.若配置文件权限较高,需使用sudonginx-t执行。3.确认测试的是实际加载的配置路径,可通过nginx-t-c/path/to/your/nginx.conf指定路径,或通过ps-ef|grepnginx查看主进程使用的配置文件。4.测试通过后执行sudonginx-sreload重载服务使新配置生效

TosetupanNginxserverblock,firstunderstanditsstructureusingtheserverdirectivewithsettingslikelisten,server_name,andlocation;next,createadirectorystructureforyoursitesuchas/var/www/example.com/htmlandsetproperpermissions;thenenabletheserverblockbycreat

^~修饰符在Nginx中用于优先匹配指定前缀的URL,且阻止后续正则表达式匹配。当请求的URL以该前缀开头时,Nginx将立即采用此块配置,并跳过所有正则表达式(location~或location~*)的检查,但不像=那样要求完全精确匹配。1.它适用于需要特定路径处理(如静态资源)且避免被正则规则覆盖的情况;2.常用于提升性能并确保某些规则优先执行;3.典型场景包括服务图片、脚本或内部API路由。与其它修饰符相比:4.普通前缀匹配仍会继续检查正则;5.精确匹配仅适用于完整路径;6.正则匹配会在

ToconfigureabackupserverinNginx,addthe"backup"parametertoaserverintheupstreamblock,ensuringitonlyreceivestrafficwhenallotherserversareunavailable.1.Definethebackupserverusingthesyntax"serverbackup;"withintheupstreamblock.2.Combine

要屏蔽特定的User-Agent,可在Nginx、Apache或代码(如PHP、Python)中实现。1.在Nginx中,通过if判断$http_user_agent并返回403;2.在Apache中,使用SetEnvIfNoCase和Deny拒绝访问;3.在程序中判断User-Agent并拦截请求。常见需屏蔽的UA包括python-requests、curl、空UA等,选择合适方式可有效减少垃圾流量和安全风险。

proxy_pass在Nginx中用于将客户端请求转发到后端服务器,其核心作用是使Nginx能够作为反向代理处理HTTP请求。1.它接收用户请求并转发至指定的后端服务(如运行在3000端口的Node.js应用);2.Nginx会处理后端返回的响应,并将其送回给用户,同时可在此过程中添加缓存、压缩或访问控制等功能;3.设置时需注意路径匹配与尾部斜杠的关系,以决定是否剥离匹配部分的路径;4.需配合设置标准代理头(如Host、X-Real-IP等),确保后端获取正确的上下文信息;5.常见问题包括路径不
