按点赞数排序
按时间排序
所谓性能好,一般指2点:低时延、高并发,由这2点会带来高吞吐量,也就是百万级的QPS。
我先说低时延是怎么办到的:
1、Nginx内部的算法都非常优秀,是性能优先的。
比如hash表会考虑cpu cache line(参见https://www.nginx-cn.net/article/71),比如location匹配是基于URI规则封装的多叉树(参见https://www.nginx-cn.net/article/69)。
2、Nginx充分使用了OS的各种高性能特性
比如Linux的reuseport、accept_defer、lingering_close、sendfile、aio等等。
再说高并发是怎么达到的:
1、每个请求占用的内存极为有限
每个连接占用的基本内存不过几百字节,这需要很深厚的功力,也只有C语言才能办得到。
2、基于事件驱动的多路复用框架
这个已经说烂了,就不多说了
5
回答于2020-05-20 19:47
说多了就一句话,充分利用系统的特性。
以 Linux 为例,
在 Linux 中的多进程,epoll 模型, reuseport, send_file ....都是高并发的利器
1 多进程让所有核心都在干活,而不是划水
2 epoll 模型让每个核心都干它值得干的事情(相对 select/poll,减少不必要的轮询)
3 reuseport 解决狗界难题-惊群
4 。。。未完待续
1
回答于2020-05-20 16:15
看机器配置了,主要表现在内存上(CPU也有影响),不只是Nginx进程使用的,也包括Linux内核的socket缓冲区。
如果你是普通的4core8G或者8core16G机器,到3W并发很轻松
根据你提供的配置和错误信息,有几个问题可能导致你无法访问多级目录的资源。下面是可能的解决方案:
1. 尝试一:访问/applet/img/user/fitness_logo2.png 提示404的配置:
```
location / {
root /app/docker/statics/web;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /applet {
root /app/docker/statics;
index index.html;
}
```
这个配置中,`location /applet` 指定了根目录为 `/app/docker/statics`,而你的资源实际路径是 `/app/docker/statics/applet`。因此,你需要在 `location /applet` 的配置中添加 `/applet` 部分,以便正确地指向资源路径。修改后的配置如下:
```
location / {
root /app/docker/statics/web;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /applet {
root /app/docker/statics;
index index.html;
try_files $uri $uri/ /applet$uri /applet$uri/ =404;
}
```
2. 尝试二:访问/applet/img/user/fitness_logo2.png 提示404的配置:
```
location / {
root /app/docker/statics/web;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /applet {
alias /app/docker/statics/applet;
}
```
在这个配置中,你使用了 `alias` 指令来映射 `/applet` 到 `/app/docker/statics/applet` 目录。但是,由于你的请求是 `/applet/img/user/fitness_logo2.png`,Nginx 尝试将请求 `/applet/img/user/fitness_logo2.png/` 解析为目录,并在该目录下寻找索引文件,导致出现错误。为了解决这个问题,你可以尝试在 `location /applet` 的配置中使用 `try_files` 指令来显式指定文件的匹配。修改后的配置如下:
```
location / {
root /app/docker/statics/web;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /applet {
alias /app/docker/statics/applet;
try_files $uri $uri/ =404;
}
```