回答
收藏
分享
举报
nginx为什么性能这么优越?
提问于2020-05-08 16:39

浏览 1.9k

已修改于2023-03-17 17:59



写下您的回答
发表回答
全部回答(4)

按点赞数排序

按时间排序

所谓性能好,一般指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



回复陶辉
回复
lnx回复了


老师,请问这种场景怎么解决呢?nginx代理服务器会不会存在性能瓶颈呢?

0

回复举报

回答于2021-11-04 11:35



回复lnx
回复

说多了就一句话,充分利用系统的特性。

以 Linux 为例,

    在 Linux 中的多进程,epoll 模型, reuseport, send_file ....都是高并发的利器

1 多进程让所有核心都在干活,而不是划水

2 epoll 模型让每个核心都干它值得干的事情(相对 select/poll,减少不必要的轮询)

3 reuseport 解决狗界难题-惊群

4 。。。未完待续

赞同

1

回复举报

回答于2020-05-20 16:15



回复头号游戏教父B哥
回复

因为C语言编写的

赞同

1

回复举报

回答于2020-05-08 16:50



回复13191816265@qq.com
回复
黄阿马回复了
您这句话倒是简单粗暴啊

0

回复举报

回答于2020-05-21 08:12



回复黄阿马
回复

同问,坐等回答

赞同

0

回复举报

回答于2020-05-08 16:40



回复你猜我不猜
回复
提问者
繁星
这家伙很懒还未留下介绍~
0
文章
6
问答
8
粉丝
相关问答

看机器配置了,主要表现在内存上(CPU也有影响),不只是Nginx进程使用的,也包括Linux内核的socket缓冲区。

如果你是普通的4core8G或者8core16G机器,到3W并发很轻松

点赞 0
浏览 1.8k

哈哈,小皮面板的 nginx_waf 目前免费,哈哈,只是目前

点赞 0
浏览 1.1k

根据你提供的配置和错误信息,有几个问题可能导致你无法访问多级目录的资源。下面是可能的解决方案:


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;

}

```

点赞 0
浏览 2.3k