Nginx-PHP始终比apache2.2慢

Nginx-PHP始终比apache2.2慢,php,apache,mongodb,ubuntu,nginx,Php,Apache,Mongodb,Ubuntu,Nginx,在使用Ubuntu13.04上的全库存软件包对Apache2.2和Nginx1.2.6进行负载测试时,我始终发现Nginx PHP请求的性能低于ApachePHP请求如果可能的话,我正在寻找指导,以使我们的Nginx性能在任何情况下都高于Apache的性能 Apache设置相当标准,但Nginx设置进行了大量定制;它们列在基准结果的下方 我使用名为Sakev3.0.2()的基准测试工具为单个并发用户(c1)、10个并发用户(c10)和100个并发用户(c100)生成结果;结果如下: Apache

在使用Ubuntu13.04上的全库存软件包对Apache2.2和Nginx1.2.6进行负载测试时,我始终发现Nginx PHP请求的性能低于ApachePHP请求如果可能的话,我正在寻找指导,以使我们的Nginx性能在任何情况下都高于Apache的性能

Apache设置相当标准,但Nginx设置进行了大量定制;它们列在基准结果的下方

我使用名为Sakev3.0.2()的基准测试工具为单个并发用户(c1)、10个并发用户(c10)和100个并发用户(c100)生成结果;结果如下:

Apache结果:

      Date & Time,  Trans,  Elap Time,  Data Trans,  Resp Time,  Trans Rate,  Throughput,  Concurrent,    OKAY,   Failed
**** c1 Apache Static ****
2013-08-01 00:54:12,   5982,      59.23,         338,       0.01,      101.00,        5.71,        1.00,    5982,       0
**** c1 Apache PHP ****
2013-08-01 00:55:12,    549,      59.98,          88,       0.11,        9.15,        1.47,        1.00,     549,       0
**** c1 Apache Combined ****
2013-08-01 00:56:12,   1609,      59.98,         139,       0.04,       26.83,        2.32,        1.00,    1609,       0
**** c10 Apache Static ****
2013-08-01 00:57:12,  35983,      59.97,        2039,       0.02,      600.02,       34.00,        9.99,   35983,       0
**** c10 Apache PHP ****
2013-08-01 00:58:12,   3769,      59.98,         610,       0.16,       62.84,       10.17,        9.99,    3769,       0
**** c10 Apache Combined ****
2013-08-01 00:59:12,  10928,      59.98,         947,       0.05,      182.19,       15.79,        9.99,   10928,       0
**** c100 Apache Static ****
2013-08-01 01:00:12,  44581,      59.97,        2523,       0.13,      743.39,       42.07,       98.63,   44581,       0
**** c100 Apache PHP ****
2013-08-01 01:01:12,   4427,      59.98,         721,       1.32,       73.81,       12.02,       97.34,    4427,       1
**** c100 Apache Combined ****
2013-08-01 01:02:12,  12735,      59.98,        1125,       0.47,      212.32,       18.76,       99.68,   12735,       0
      Date & Time,  Trans,  Elap Time,  Data Trans,  Resp Time,  Trans Rate,  Throughput,  Concurrent,    OKAY,   Failed
**** c1 Nginx Static ****
2013-08-01 02:36:13,   9040,      59.10,         274,       0.01,      152.96,        4.64,        1.00,    9040,       0
**** c1 Nginx PHP ****
2013-08-01 02:37:13,    581,      59.98,          18,       0.10,        9.69,        0.30,        1.00,     581,       0
**** c1 Nginx Combined ****
2013-08-01 02:38:13,   1786,      59.59,          55,       0.03,       29.97,        0.92,        1.00,    1786,       0
**** c10 Nginx Static ****
2013-08-01 02:39:13,  44557,      59.98,        1353,       0.01,      742.86,       22.56,        9.99,   44557,       0
**** c10 Nginx PHP ****
2013-08-01 02:40:13,   3766,      59.98,         120,       0.16,       62.79,        2.00,        9.98,    3766,       0
**** c10 Nginx Combined ****
2013-08-01 02:41:13,  10962,      59.98,         339,       0.05,      182.76,        5.65,        9.98,   10962,       0
**** c100 Nginx Static ****
2013-08-01 02:42:13,  54463,      59.98,        1642,       0.11,      908.02,       27.38,       99.70,   54463,       0
**** c100 Nginx PHP ****
2013-08-01 02:43:13,   3649,      59.98,         117,       1.62,       60.84,        1.95,       98.70,    3649,       0
**** c100 Nginx Combined ****
2013-08-01 02:44:13,  10802,      59.98,         334,       0.55,      180.09,        5.57,       98.63,   10802,       0
Nginx结果:

      Date & Time,  Trans,  Elap Time,  Data Trans,  Resp Time,  Trans Rate,  Throughput,  Concurrent,    OKAY,   Failed
**** c1 Apache Static ****
2013-08-01 00:54:12,   5982,      59.23,         338,       0.01,      101.00,        5.71,        1.00,    5982,       0
**** c1 Apache PHP ****
2013-08-01 00:55:12,    549,      59.98,          88,       0.11,        9.15,        1.47,        1.00,     549,       0
**** c1 Apache Combined ****
2013-08-01 00:56:12,   1609,      59.98,         139,       0.04,       26.83,        2.32,        1.00,    1609,       0
**** c10 Apache Static ****
2013-08-01 00:57:12,  35983,      59.97,        2039,       0.02,      600.02,       34.00,        9.99,   35983,       0
**** c10 Apache PHP ****
2013-08-01 00:58:12,   3769,      59.98,         610,       0.16,       62.84,       10.17,        9.99,    3769,       0
**** c10 Apache Combined ****
2013-08-01 00:59:12,  10928,      59.98,         947,       0.05,      182.19,       15.79,        9.99,   10928,       0
**** c100 Apache Static ****
2013-08-01 01:00:12,  44581,      59.97,        2523,       0.13,      743.39,       42.07,       98.63,   44581,       0
**** c100 Apache PHP ****
2013-08-01 01:01:12,   4427,      59.98,         721,       1.32,       73.81,       12.02,       97.34,    4427,       1
**** c100 Apache Combined ****
2013-08-01 01:02:12,  12735,      59.98,        1125,       0.47,      212.32,       18.76,       99.68,   12735,       0
      Date & Time,  Trans,  Elap Time,  Data Trans,  Resp Time,  Trans Rate,  Throughput,  Concurrent,    OKAY,   Failed
**** c1 Nginx Static ****
2013-08-01 02:36:13,   9040,      59.10,         274,       0.01,      152.96,        4.64,        1.00,    9040,       0
**** c1 Nginx PHP ****
2013-08-01 02:37:13,    581,      59.98,          18,       0.10,        9.69,        0.30,        1.00,     581,       0
**** c1 Nginx Combined ****
2013-08-01 02:38:13,   1786,      59.59,          55,       0.03,       29.97,        0.92,        1.00,    1786,       0
**** c10 Nginx Static ****
2013-08-01 02:39:13,  44557,      59.98,        1353,       0.01,      742.86,       22.56,        9.99,   44557,       0
**** c10 Nginx PHP ****
2013-08-01 02:40:13,   3766,      59.98,         120,       0.16,       62.79,        2.00,        9.98,    3766,       0
**** c10 Nginx Combined ****
2013-08-01 02:41:13,  10962,      59.98,         339,       0.05,      182.76,        5.65,        9.98,   10962,       0
**** c100 Nginx Static ****
2013-08-01 02:42:13,  54463,      59.98,        1642,       0.11,      908.02,       27.38,       99.70,   54463,       0
**** c100 Nginx PHP ****
2013-08-01 02:43:13,   3649,      59.98,         117,       1.62,       60.84,        1.95,       98.70,    3649,       0
**** c100 Nginx Combined ****
2013-08-01 02:44:13,  10802,      59.98,         334,       0.55,      180.09,        5.57,       98.63,   10802,       0
我关心的数据来自c100“PHP”和“组合”结果。Apache的速度要快得多,我想知道,如果所有在线基准测试都显示了相反的结果,那么这是怎么可能的

两台服务器都是:

      Date & Time,  Trans,  Elap Time,  Data Trans,  Resp Time,  Trans Rate,  Throughput,  Concurrent,    OKAY,   Failed
**** c1 Apache Static ****
2013-08-01 00:54:12,   5982,      59.23,         338,       0.01,      101.00,        5.71,        1.00,    5982,       0
**** c1 Apache PHP ****
2013-08-01 00:55:12,    549,      59.98,          88,       0.11,        9.15,        1.47,        1.00,     549,       0
**** c1 Apache Combined ****
2013-08-01 00:56:12,   1609,      59.98,         139,       0.04,       26.83,        2.32,        1.00,    1609,       0
**** c10 Apache Static ****
2013-08-01 00:57:12,  35983,      59.97,        2039,       0.02,      600.02,       34.00,        9.99,   35983,       0
**** c10 Apache PHP ****
2013-08-01 00:58:12,   3769,      59.98,         610,       0.16,       62.84,       10.17,        9.99,    3769,       0
**** c10 Apache Combined ****
2013-08-01 00:59:12,  10928,      59.98,         947,       0.05,      182.19,       15.79,        9.99,   10928,       0
**** c100 Apache Static ****
2013-08-01 01:00:12,  44581,      59.97,        2523,       0.13,      743.39,       42.07,       98.63,   44581,       0
**** c100 Apache PHP ****
2013-08-01 01:01:12,   4427,      59.98,         721,       1.32,       73.81,       12.02,       97.34,    4427,       1
**** c100 Apache Combined ****
2013-08-01 01:02:12,  12735,      59.98,        1125,       0.47,      212.32,       18.76,       99.68,   12735,       0
      Date & Time,  Trans,  Elap Time,  Data Trans,  Resp Time,  Trans Rate,  Throughput,  Concurrent,    OKAY,   Failed
**** c1 Nginx Static ****
2013-08-01 02:36:13,   9040,      59.10,         274,       0.01,      152.96,        4.64,        1.00,    9040,       0
**** c1 Nginx PHP ****
2013-08-01 02:37:13,    581,      59.98,          18,       0.10,        9.69,        0.30,        1.00,     581,       0
**** c1 Nginx Combined ****
2013-08-01 02:38:13,   1786,      59.59,          55,       0.03,       29.97,        0.92,        1.00,    1786,       0
**** c10 Nginx Static ****
2013-08-01 02:39:13,  44557,      59.98,        1353,       0.01,      742.86,       22.56,        9.99,   44557,       0
**** c10 Nginx PHP ****
2013-08-01 02:40:13,   3766,      59.98,         120,       0.16,       62.79,        2.00,        9.98,    3766,       0
**** c10 Nginx Combined ****
2013-08-01 02:41:13,  10962,      59.98,         339,       0.05,      182.76,        5.65,        9.98,   10962,       0
**** c100 Nginx Static ****
2013-08-01 02:42:13,  54463,      59.98,        1642,       0.11,      908.02,       27.38,       99.70,   54463,       0
**** c100 Nginx PHP ****
2013-08-01 02:43:13,   3649,      59.98,         117,       1.62,       60.84,        1.95,       98.70,    3649,       0
**** c100 Nginx Combined ****
2013-08-01 02:44:13,  10802,      59.98,         334,       0.55,      180.09,        5.57,       98.63,   10802,       0
  • 在四核Xeon处理器上运行
  • 8GB内存
  • 连接到同一网络上的MongoDB v2.2数据库
  • PHP-FPM设置为使用100个PHP进程
  • Apache(设置与库存非常接近):

  • 在CentOS 5上运行
  • Apache2.2
  • mod_php
  • Nginx:

          Date & Time,  Trans,  Elap Time,  Data Trans,  Resp Time,  Trans Rate,  Throughput,  Concurrent,    OKAY,   Failed
    **** c1 Apache Static ****
    2013-08-01 00:54:12,   5982,      59.23,         338,       0.01,      101.00,        5.71,        1.00,    5982,       0
    **** c1 Apache PHP ****
    2013-08-01 00:55:12,    549,      59.98,          88,       0.11,        9.15,        1.47,        1.00,     549,       0
    **** c1 Apache Combined ****
    2013-08-01 00:56:12,   1609,      59.98,         139,       0.04,       26.83,        2.32,        1.00,    1609,       0
    **** c10 Apache Static ****
    2013-08-01 00:57:12,  35983,      59.97,        2039,       0.02,      600.02,       34.00,        9.99,   35983,       0
    **** c10 Apache PHP ****
    2013-08-01 00:58:12,   3769,      59.98,         610,       0.16,       62.84,       10.17,        9.99,    3769,       0
    **** c10 Apache Combined ****
    2013-08-01 00:59:12,  10928,      59.98,         947,       0.05,      182.19,       15.79,        9.99,   10928,       0
    **** c100 Apache Static ****
    2013-08-01 01:00:12,  44581,      59.97,        2523,       0.13,      743.39,       42.07,       98.63,   44581,       0
    **** c100 Apache PHP ****
    2013-08-01 01:01:12,   4427,      59.98,         721,       1.32,       73.81,       12.02,       97.34,    4427,       1
    **** c100 Apache Combined ****
    2013-08-01 01:02:12,  12735,      59.98,        1125,       0.47,      212.32,       18.76,       99.68,   12735,       0
    
          Date & Time,  Trans,  Elap Time,  Data Trans,  Resp Time,  Trans Rate,  Throughput,  Concurrent,    OKAY,   Failed
    **** c1 Nginx Static ****
    2013-08-01 02:36:13,   9040,      59.10,         274,       0.01,      152.96,        4.64,        1.00,    9040,       0
    **** c1 Nginx PHP ****
    2013-08-01 02:37:13,    581,      59.98,          18,       0.10,        9.69,        0.30,        1.00,     581,       0
    **** c1 Nginx Combined ****
    2013-08-01 02:38:13,   1786,      59.59,          55,       0.03,       29.97,        0.92,        1.00,    1786,       0
    **** c10 Nginx Static ****
    2013-08-01 02:39:13,  44557,      59.98,        1353,       0.01,      742.86,       22.56,        9.99,   44557,       0
    **** c10 Nginx PHP ****
    2013-08-01 02:40:13,   3766,      59.98,         120,       0.16,       62.79,        2.00,        9.98,    3766,       0
    **** c10 Nginx Combined ****
    2013-08-01 02:41:13,  10962,      59.98,         339,       0.05,      182.76,        5.65,        9.98,   10962,       0
    **** c100 Nginx Static ****
    2013-08-01 02:42:13,  54463,      59.98,        1642,       0.11,      908.02,       27.38,       99.70,   54463,       0
    **** c100 Nginx PHP ****
    2013-08-01 02:43:13,   3649,      59.98,         117,       1.62,       60.84,        1.95,       98.70,    3649,       0
    **** c100 Nginx Combined ****
    2013-08-01 02:44:13,  10802,      59.98,         334,       0.55,      180.09,        5.57,       98.63,   10802,       0
    
  • Ubuntu 13.04
  • Nginx 1.2.6
  • 带有100个PHP进程的PHP-FPM(FastCGI)
  • nginx.conf

    pid /run/nginx.pid;
    user www-data;
    worker_processes 4;
    
    
    events {
        worker_connections 1024;
    }
    
    
    http {
        # APACHE BACKWARDS COMPATIBILITY ENVIRONMENT VARIABLES
        map $request_uri $my_script_url {
            default $request_uri;
            ~^(?<script_filename>.+\.(php))(.*)?$ $script_filename; #/test.php or /test.php?hello=world
            ~^(?<script_filename>.*)(\?.*)$ $script_filename; #/tos?hello=world
            ~^(?<script_filename>.*)(\?.*)?$ $script_filename; #/tos or /tos/hello/world or /tos/hello/world?omg=what
        }
    
    
        # BASE SETTINGS
        charset utf-8;
        default_type application/octet-stream;
        include /etc/nginx/mime.types;
        server_tokens off;
    
    
        # CLIENT CACHING SETTINGS
        add_header Last-Modified "";
        expires 7d;
    
    
        # CONNECTION SETTINGS
        client_body_timeout 15s;
        client_header_timeout 30s;
        client_max_body_size 8m;
        keepalive_requests 10000;
        keepalive_timeout 30s;
        reset_timedout_connection on;
        resolver_timeout 5s;
        send_timeout 15s;
        tcp_nopush on;
    
    
        # FASTCGI SETTINGS
        # fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=microcache:10m max_size=1000m inactive=60m;
    
    
        # FILE CACHING AND PERFORMANCE SETTINGS
        open_file_cache max=10000 inactive=20s;
        open_file_cache_errors off;
        open_file_cache_min_uses 2;
        open_file_cache_valid 30s;
        sendfile on;
    
    
        # GZIP SETTINGS
        gzip on;
        gzip_comp_level 5;
        gzip_min_length 1024;
        gzip_proxied any;
        gzip_types
            text/css
            text/plain
            text/javascript
            application/javascript
            application/json
            application/x-javascript
            application/xml
            application/xml+rss
            application/xhtml+xml
            application/x-font-ttf
            application/x-font-opentype
            application/vnd.ms-fontobject
            image/svg+xml
            image/x-icon
            application/rss+xml
            application/atom_xml;
        gzip_vary on;
    
    
        # LOGGING SETTINGS
        access_log /var/log/nginx/access.log combined buffer=16k;
        error_log /var/log/nginx/error.log crit;
        open_log_file_cache max=100 inactive=1m min_uses=1 valid=2m;
    
    
        # SSL SETTINGS
        # ssl_ciphers !aNULL:!eNULL:FIPS@STRENGTH;
        # ssl_prefer_server_ciphers on;
        # ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        # ssl_session_cache shared:SSL:10m;
        # ssl_session_timeout 3m;
    
    
        # OTHER GLOBAL CONFIGURATION FILES
        include /etc/nginx/conf.d/*.conf;
    
    
        # VIRTUAL HOST CONFIGS
        include /etc/nginx/sites-enabled/*;
    }
    

    如果您有任何关于加快Nginx速度的建议,我们将不胜感激。如果可能的话,我希望避免内核和TCP/IP调优。

    您已经在nginx中启用了gzip动态压缩-这是一项非常昂贵的操作,那么您期望得到什么呢?更糟糕的是,您已经配置了第5压缩级别,这使得它变得更慢、更慢。

    Apache和nginx之间的PHP性能应该相当相似,因为PHP是比所用服务器更高的瓶颈

    在您的例子中,当并发度=1或并发度=10时,性能看起来是相同的,只有当并发度为100时,nginx/PHP-FPM上的性能才会变慢

    不管您怎么想,并行运行更多的PHP-FPM进程并不能提高许多并发查询的性能。PHP在某一点之后并行运行不会带来很大的好处。在某一点之后,由于额外的上下文切换、I/O访问的更多随机争用、更高的内存使用率等,更多的并行进程可能会降低总体吞吐量

    在我的测试中,PHP-FPM进程的最佳点大约是6到10(我使用8)。这使我获得了最高的性能,即使在测试数百个并发连接时也是如此。在这之后添加更多的PHP-FPM进程开始减慢速度。您的里程数可能会有所不同,但100不太可能是任何服务器上的最佳选择

    请注意,您的PHP-FPM进程数不必等于或大于您支持的并发连接数。为了更好地解释这一点,拥有8个PHP-FPM进程并不意味着您仅限于8个并发连接。只要PHP-FPM中的listen.backlog足够高,您的服务器(nginx)仍将保持数百个并发连接,但PHP-FPM只需在内部一次处理8个连接,而不是一次处理所有连接。这意味着每个查询实际由PHP执行的时间要少得多,因为它与较少的其他进程争用。连接仍将同时保持打开状态,对数百个并发用户进行测试时,仍将看到所有请求都能快速、成功地得到服务


    注意,我还发现了另一种在nginx设置中加速PHP的方法,那就是增加fastcgi_缓冲区的数量/大小。Mine当前设置为
    fastcgi_缓冲区32 16k

    继续问吧,我还没有看过配置,但我想你的Apache MPM允许的PHP实例比你在nginx PHP fpm池中允许的要多,所以你只是拥有更多的并发容量。我将PHP-fpm设置为生成100个PHP进程,这与并发用户的数量相匹配。这本不应该是一个问题,除非我遗漏了什么。它离开发很近(因为很多开发人员都有这个责任),所以请不要投反对票。我想自己知道答案!属于serverfault(在关闭对话框中没有选择选项)是的,这是我的第一个猜测。在四核CPU上同时运行100个高负载进程可能不是最佳选择。因为您可能有8个逻辑内核,所以8个php fpm进程可能正好合适。我自己使用8个php-fpm进程进行的测试是在一个便宜的VM上进行的,该VM具有768MB RAM和一个内核,所以是YMMV。我认为,对于确定有多少个PHP进程是最佳点而言,I/O争用可能比虚拟/实际内核的数量更重要。我鼓励任何人做实际的测试,看看。好的观点。CPU与IO的对比在很大程度上取决于这100个进程的实际用途。