优化 Nginx + PHP-FPM 可实现更快的响应时间(用于 Openx 广告投放)

发布于 2024-08-21 05:55:39 字数 1765 浏览 7 评论 0原文

我目前正在运行 Nginx + PHP-FPM 在 OpenX 上投放广告。目前,即使在低负载时期,我的响应时间也很糟糕。然而,我的CPU和内存资源都很好,所以我似乎无法弄清楚瓶颈是什么。

我当前的 nginx 和 php-fpm 配置是:

worker_processes 20;
worker_rlimit_nofile 50000;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  15000;
    multi_accept off;
    use epoll;
}

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    tcp_nopush     off;

    keepalive_timeout  0;
    #keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_comp_level 2;
    gzip_proxied    any;
    gzip_types    text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

server {
    listen   80;
    server_name  localhost;
    access_log  /var/log/nginx/localhost.access.log;

# Default location
    location / {
        root   /var/www;
        index  index.php;
    }

## Parse all .php file in the /var/www directory
    location ~ .php$ {
        fastcgi_pass   localhost:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_ignore_client_abort     off;
    }

PHP-FPM:
rlimit_files = 50000
max_children = 500

我只包含了为 PHP-FPM 更改的 PHP-FPM 参数。

有人对如何优化它以便满足更多请求有任何建议吗?我现在看到的响应时间非常糟糕。

I'm currently running Nginx + PHP-FPM for serving ads on OpenX. Currently my response times are horrible, even during times of low load. However, my CPU and Memory resources are fine, so I can't seem to figure out what the bottleneck is.

My current config for nginx and php-fpm is:

worker_processes 20;
worker_rlimit_nofile 50000;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  15000;
    multi_accept off;
    use epoll;
}

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    tcp_nopush     off;

    keepalive_timeout  0;
    #keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_comp_level 2;
    gzip_proxied    any;
    gzip_types    text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

server {
    listen   80;
    server_name  localhost;
    access_log  /var/log/nginx/localhost.access.log;

# Default location
    location / {
        root   /var/www;
        index  index.php;
    }

## Parse all .php file in the /var/www directory
    location ~ .php$ {
        fastcgi_pass   localhost:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_ignore_client_abort     off;
    }

PHP-FPM:
rlimit_files = 50000
max_children = 500

I only included the PHP-FPM paramaters I've changed for PHP-FPM.

Does anyone have any tips on how I can optimize it so I can serve more requests? I'm seeing horrendous response times right now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

怪我闹别瞎闹 2024-08-28 05:55:39

首先,工人太多,限制设置得太高。仅 php-fpm 的最大工作线程数就会使您的服务器陷入相当大的瘫痪。取消服务器的限制不一定会加快速度,但实际上可能会产生相反的效果。

  1. 工作人员数量:20 如果您没有 20 个处理器/核心的计算机,则没有什么意义,实际上会造成负面影响,因为工作人员将进行过多的内容交换。如果您运行的是双核处理器,2 个工作线程就足够了。

  2. 工人联系:再说一次,仅仅向天上施加限制并不能解决你的问题。如果您的 ulimit -n 输出类似于 1024,那么您的工作连接数需要设置为 1024 或更少(甚至可能是 768),您不太可能拥有 2 x 1024 个并发连接,尤其是对于 PHP 之类的连接。

  3. 根位置和 PHP 设置,请参考 http://wiki.nginx.org/Pitfalls ,如果您将 root 指令置于服务器 {} 级别,而不是位置级别。完成此操作后,您可以使用 $document_root$fastcgi_script_name 作为 SCRIPT_FILENAME 值,因为 $document_root 将自动传播到其下方的位置块。

  4. 您可能希望直接处理静态文件,换句话说:

    位置 ~* \.(ico|css|js|gif|jpe?g|png)$ {
        最大过期时间;
        add_header 杂注公共;
        add_header Cache-Control“公共、必须重新验证、代理重新验证”;
    }
    
  5. 使用 PHP 加速器,即 APC(在 php.ini 中使用 apc.enabled=1)或 XCache,并注意你的 php 设置,例如内存限制。例如,如果您的系统只有 2GB 内存,那么允许 500 名工作人员且每人限制为 128MB 是没有意义的。如果您还在服务器上运行其他服务,则尤其如此。

First off, way too many workers, and limits set excessively high. The max worker count for php-fpm alone would bog your server down quite a bit. Uncapping the limits on a server won't necessarily speed it up but may actually have the opposite effect.

  1. Worker Count: 20 makes little sense if you do not have a 20 processor/core machine, you're actually causing a negative effect as the workers will have excessive content swapping. If you're running a dual core processor, 2 workers should suffice.

  2. Worker Connections: Again, just throwing a limit into the heavens doesn't solve your problems. If your ulimit -n output is something like 1024, then your worker connections would need to be set to 1024 or less (maybe even 768), its unlikely that you'll have 2 x 1024 simultaneous connections especially with something like PHP.

  3. Root location, and PHP settings, refer to http://wiki.nginx.org/Pitfalls , it works best if you put your root directive at the server {} level, not the location level. Once you do that you can use $document_root$fastcgi_script_name as the SCRIPT_FILENAME value as $document_root will be automatically propagated to location blocks below it.

  4. You may wish to handle static files directly, in other words:

    location ~* \.(ico|css|js|gif|jpe?g|png)$ {
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }
    
  5. Use a PHP Accelerator, namely APC (with apc.enabled=1 in php.ini) or XCache, and be mindful of your php settings, such as the memory_limit. For example if you only have a system with 2GB of rams, it makes very little sense to allow 500 workers with a limit of 128MB each. Especially true if you're also running other services on your server.

谜兔 2024-08-28 05:55:39

也很有用:

access_log off;

正如我想的那样,您并不真正关心这些请求的日志生成。

Would be also useful to put:

access_log off;

As i suppose you don't really care of log generation on these requests.

半衾梦 2024-08-28 05:55:39

你绝对应该减少工人的数量,因为我怀疑你有 20 个核心/处理器。
另外,我会检查一下你的数据库服务器,问题很可能就在那里。

另外,您已将 worker_rlimit_nofile 提高到 50000,我希望您知道操作系统通常将限制设置为 1024(默认),您可以尝试请求当前限制是多少通过键入 ulimit -n

您可以通过在 init.d 或 访问 stackoverflow 上的另一个问题以了解如何使用 limits.conf 在系统范围内永久设置限制。

You should definitely reduce the number of workers as I doubt you have 20 cores/processors.
Additionally, I'd look into your database server, there's a big possibility that the problem is there.

Additionally you've upped the worker_rlimit_nofile to 50000, I hope you know that operating system usually set the limit to 1024 (default), you can try to request what's the current limit by typing ulimit -n

You can raise hard limit of NOFILE (number of open files) by executing this command ulimit -n 50000 in init.d or visit this other question on stackoverflow to learn how to use limits.conf to permanently set limits system wide.

戏蝶舞 2024-08-28 05:55:39

使用 nginx 和 php5-fpm 真正将性能发挥到极致是一门艺术。需要真正了解您所提供的内容类型。

例如,我在您的配置中没有看到任何 try_files 用法或任何类型的缓存。您知道 nginx 内置了 memcache 支持吗?您可以缓存图像和 html/css,以及 php 页面。如果您最关心点击次数,即使不显示,这些点击次数仍会被计算在内。

将你的横幅放在 tmpfs 挂载中,不记录 access_log 或 error_log,禁用 php 中不需要的模块,使用最新版本的 mysql,使用 innodb 来减少表锁定,使用 innodb 的刷新方法来减少磁盘 Nginx 只是一个非常大且复杂的

公式的一部分。我什至没有提到用于优化 TCP 堆栈和网卡性能、交换使用、内存使用或您可能通过 OpenX 提供服务的 HTML/CSS 的 gzip 压缩的内核参数(如果您是的话)。

是的,就像我上面提到的其他人一样,您的设置看起来过多,并且表明缺乏对基本优化概念的理解。换句话说,聘请专业人士:-)

Really pushing performance to the max with nginx and php5-fpm is an art. It takes really understanding the kind of contents you are serving.

For example, I don't see any try_files usage, or any kind of caching in your configuration. Do you know nginx comes with built-in memcache support? You can cache images and html/css, as well as php pages. If you care mostly for clicks, those clicks will still be counted even if displays are not.

Put your banners in a tmpfs mount, don't log access_log or error_log, disable modules you don't need in php, use a recent version of mysql, use innodb to reduce table locking, play with the flushing method of innodb to reduce disk writes, increase maximum memory tables in mysql to reduce the creation of temporary files on disk when joins are requested via SQL, etc.

Nginx is just one part of a very large and complex formula. I have not even mentioned Kernel params to optimize the TCP Stack and Network Card performance, Swap usage, Memory usage, or gzip compression of HTML/CSS you may be serving via OpenX (If you are).

And yes, like others above me mentioned, your settings look excessive and show a lack of understanding of basic optimization concepts. In other words, hire a professional :-)

月棠 2024-08-28 05:55:39

您的机器上有 20 个处理器或内核吗?也可以尝试使用操作系统默认的事件...也许更多的 fcgi 进程而不是更多的 nginx...可能从 2 - 4 个 nginx 工作线程开始就足够了...

do you have 20 processors or cores on your machine? also maybe try events with the default for your OS... maybe more fcgi processes instead of more nginx... probably starting with 2 - 4 nginx workers is enough...

半夏半凉 2024-08-28 05:55:39

正如人们所提到的,工人也可能如此。对于 php 操作码缓存,我个人更喜欢 xcache 而不是 APC。您应该检查修改后的 centmin auto bash shell nginx/php-fpm 安装脚本中的配置 http://vbtechsupport.com/796/

Definitely way too may workers as folks have mentioned. I personally prefer xcache over APC for php opcode caching. You should check out configuration in modified centmin auto bash shell nginx/php-fpm install script http://vbtechsupport.com/796/

荆棘i 2024-08-28 05:55:39

使服务器系统更快的最有效方法是使用Facebook的HipHop虚拟机(HHVM)而不是PHP(不必再安装PHP)。

HHVM 在 CPU 上游使用“即时编译器”,执行 PHP 代码的速度比 PHP 本身快 5 到 10 倍,因此可以与较少数量的服务器或更小的服务器相处,并降低功耗本质上。维基百科使用 HHVM 将 CPU 服务器负载减少了 5 倍:
http://www.golem.de /news/php-facebooks-hhvm-macht-wikipedia-schneller-1501-111515.html

它可以作为 Linux 包与 Nginx 一起安装,并且非常容易地包含在 Nginx 中,类似于 FastCGI,不久之后分钟后就可以通过一个小的“Hello World”PHP 文件进行测试:
https://github.com/facebook/hhvm/wiki/Getting-Started

根据基准测试,新的 PHP7 PHPNG 实际上应该只快 30%。

感谢您的投票

The most effective way to make a server system much faster is to use Facebooks HipHop Virtual Machine (HHVM) instead of PHP (PHP not must be installed any more).

HHVM uses upstream of the CPU a "Just in Time Compiler" and executes the usually PHP code 5 to 10 times faster than PHP itself, it makes it possible to get along with a smaller number of servers or smaller servers and to reduce the power consumption essentially. Wikipedia used HHVM to reduce the CPU server load by the factor 5:
http://www.golem.de/news/php-facebooks-hhvm-macht-wikipedia-schneller-1501-111515.html

It can be installed together with Nginx as Linux package and be included in Nginx very easily similar as FastCGI, and soon after a few minutes it can be tested trough a small "Hello World" PHP file:
https://github.com/facebook/hhvm/wiki/Getting-Started

The new PHP7 PHPNG should be in truth according to benchmark tests only 30% faster.

thanks for upvoting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文