简体   繁体   English

每秒优化Nginx请求

[英]Optimizing nginx requests per second

I'm trying to optimize a single core 1GB ram Digital Ocean VPS to handle more requests per second. 我正在尝试优化单核1GB ram Digital Ocean VPS,以每秒处理更多请求。 After some tweaking (workers/gzip etc.) it serves about 15 requests per second. 经过一些调整(workers / gzip等)后,它每秒可处理约15个请求。 I don't have anything to compare it with but I think this number can be higher. 我没有什么可比较的,但我认为这个数字可能会更高。

The stack works like this: 堆栈的工作方式如下:

VPS -> Docker container -> nginx (ssl) -> Varnish -> nginx -> uwsgi (Django) VPS-> Docker容器-> nginx(ssl)->清漆-> nginx-> uwsgi(Django)

I'm aware of the fact that this is a long chain and that Docker might cause some overhead. 我知道以下事实:这是一条长链,并且Docker可能会导致一些开销。 However, almost all requests can be handled by Varnish. 但是,几乎所有请求都可以由Varnish处理。

These are my tests results: 这些是我的测试结果:

ab  -kc 100 -n 1000 https://mydomain | grep 'Requests per second'
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Requests per second:    18.87 [#/sec] (mean)

I have actually 3 questions: 我实际上有3个问题:

  • Am I correct that 18.87 requests per second is low? 我是否纠正为每秒18.87个请求较低?
  • For a simple Varnished Django blog app, what would be an adequate value (indication)? 对于一个简单的Varnished Django博客应用程序,什么是适当的值(指示)?
  • I already applied the recommended tweaks (tuned for my system) from this tutorial . 我已经应用了本教程中推荐的调整(针对我的系统进行了调整)。 What can I tweak more and how do I figure out the bottlenecks. 我可以进一步调整哪些内容,以及如何找出瓶颈。

First some note about Docker. 首先要注意有关Docker的注意事项。 It is not meant to run a multiple processes in a single docker container. 并不意味着在单个Docker容器中运行多个进程。 Docker is not a replacement for a VM. Docker不能替代VM。 It simply allows to run processes in isolation. 它仅允许独立运行进程。 So the docker diagram should be: 因此,docker图应为:

VPS -> docker nginx container -> docker varnish container -> docker django container

To make your life using multiple Docker containers simpler, I would recommend to use Docker-compose. 为了简化使用多个Docker容器的工作,我建议使用Docker-compose。 It is not perfect but its an excellent start. 它不是完美的,但是它是一个很好的开始。

Old but still fundamentally relent blog post about that. 旧的但仍然从根本上拒绝了有关此的博文 Note that some suggestions are no-longer relevant like nsenter since docker exec command is now available but most of the blog post is still correct. 请注意,某些建议不再适用,例如nsenter因为nsenter docker exec命令现已可用,但大多数博客文章仍然正确。


As for your performance issues, yes, 18 requests per second is pretty low. 至于您的性能问题,是的,每秒18个请求非常低。 However the issue is probably has nothing to do with nginx and is most likely in your Django application and possibly varnish (however very unlikely). 但是,该问题可能与nginx无关,并且很可能在您的Django应用程序中,并且可能是清漆(但是可能性很小)。

To debug PA issues in Django, I would recommend to use django-debug-toolbar . 要在Django中调试PA问题,我建议使用django-debug-toolbar Most issues in Django are caused by unnecessary SQL queries. Django中的大多数问题是由不必要的SQL查询引起的。 You can see them easily in debug toolbar. 您可以在调试工具栏中轻松查看它们。 To solve most them you can use select_related() and prefetch_related . 要解决大多数问题,可以使用select_related()prefetch_related For more detailed analysis, I would also recommend profiling your application. 为了进行更详细的分析,我还建议对您的应用程序进行性能分析。 cProfile is a great start. cProfile是一个很好的开始。 Also some IDEs like PyCharm include built-in profilers so its pretty easy to profile your application to see which functions are taking most of the time which you can optimize. 另外,某些PyCharm之类的IDE都包含内置的探查器,因此很容易地对您的应用程序进行探查,以查看哪些功能在大部分时间里可以进行优化。 Finally you can use 3rd party tools to profile your application. 最后,您可以使用第三方工具来分析您的应用程序。 Even free newrelic account will give you quite a bit of information. 甚至免费的newrelic帐户也会为您提供大量信息。 Alternatively you can use opbeat which is a new cool kid on the block. 或者,您可以使用opbeat ,这是一个新手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM