nginx php 慢,Nginx PHP始终比Apache 2.2慢

在比较使用Ubuntu 13.04上的完全库存包运行的Apache 2.2和Nginx 1.2.6的负载测试中,我一直看到Nginx PHP请求的性能低于Apache PHP的性能;如果可能的话,我正在寻找指导,以便在所有情况下使我们的Nginx性能高于Apache.

Apache设置是相当标准的,但Nginx设置是相当自定义的;它们列在基准测试结果下方.

我使用名为Siege v3.0.2(http://www.joedog.org/siege-home/)的基准测试工具为单个并发用户(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

Nginx结果:

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”和“Combined”结果. Apache的速度要快得多,我想知道如果在线显示相反的所有基准测试,这是多么可能.

两台服务器都是:

>在四核Xeon处理器上运行

> 8GB RAM

>连接到同一网络上的MongoDB v2.2数据库

> PHP-FPM设置为使用100个PHP进程

Apache(设置非常接近库存):

>在CentOS 5上运行

> Apache 2.2

> mod_php

Nginx的:

> Ubuntu 13.04

> Nginx 1.2.6

> PHP-FPM(FastCGI),包含100个PHP进程

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;

~^(?.+\.(php))(.*)?$$script_filename; #/test.php or /test.php?hello=world

~^(?.*)(\?.*)$$script_filename; #/tos?hello=world

~^(?.*)(\?.*)?$$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/*;

}

虚拟主机配置

server {

# BASE SETTINGS

listen 80;

root /var/www/tbi/example/htdocs;

# server_name local.example.com;

server_name www.example.com;

# LOG SETTINGS

access_log /var/log/nginx/www.example.com.access.log combined buffer=64k;

error_log /var/log/nginx/www.example.com.error.log crit;

# LOCATIONS

location / {

index index.php index.html;

try_files $uri @extensionless-php;

}

location ~ \.(ttf|otf|eot|woff)${

add_header Access-Control-Allow-Origin *;

}

# location /nginx_status {

# See a brief synopsis of real-time, instantaneous performance

# stub_status on;

# }

location ~ \.php${

expires off;

# FASTCGI SETTINGS

fastcgi_index index.php;

fastcgi_pass unix:/var/run/php5-fpm.sock;

fastcgi_split_path_info ^(.+\.php)(/.+)$;

include fastcgi_params;

# FASTCGI CACHE SETTINGS

# fastcgi_cache microcache;

# fastcgi_cache_bypass $http_pragma;

# fastcgi_cache_key $scheme$host$request_uri$request_method;

# fastcgi_cache_methods GET HEAD;

# fastcgi_cache_use_stale updating error timeout invalid_header http_500;

# fastcgi_cache_valid any 1m;

# fastcgi_ignore_headers "Cache-Control" "Expires" "Set-Cookie";

# TBI ENVIRONMENT VARIABLES

fastcgi_param TBI_CONFIG /var/www/tbi/configs/;

fastcgi_param TBI_DOMAIN example.com;

# fastcgi_param TBI_ENV local;

fastcgi_param TBI_ENV www;

fastcgi_param TBI_RELEASETIME 0;

# APACHE BACKWARDS COMPATIBILITY ENVIRONMENT VARIABLES

fastcgi_param SCRIPT_URI $scheme://$http_host$my_script_url;

fastcgi_param SCRIPT_URL $my_script_url;

}

location @extensionless-php {

if (-f $request_filename.php) {

rewrite ^/(.*)$/$1.php last;

}

rewrite ^/(.*)$/index.php?$1 last;

}

}

任何有关使Nginx更快的建议都将非常感激.如果可能的话,我想避免内核和TCP / IP调优.

解决方法:

Apache和nginx之间的PHP性能应该非常相似,因为PHP比使用的服务器瓶颈要高得多.

在你的情况下,当concurrency = 1或concurrency = 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_buffers的数量/大小.我目前设置为fastcgi_buffers 32 16k;

标签:php,nginx,mongodb,ubuntu,apache

来源: https://codeday.me/bug/20190728/1566409.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginxapache是两种常见的Web服务器软件。它们在核心架构和性能方面有一些区别。 首先,最核心的区别在于它们的架构模型。Apache是同步多进程模型,每个连接对应一个进程,而Nginx是异步的,多个连接可以对应一个进程。这使得Nginx在处理高并发请求时具有更高的负载能力和性能优势。\[1\] 其次,Nginx采用C语言编写,占用更少的内存和资源,是一个轻量级的Web服务器。而Apache则是阻塞型的,当PHP处理或前端压力很大时,容易出现进程数飙升的情况,从而导致拒绝服务。\[2\] 此外,Nginx的配置简洁,支持正则配置,使得很多事情变得简单。而Apache的配置相对复杂,配置出错时重启会导致服务器崩溃。\[2\] 另外,Nginx作为负载均衡服务器,支持7层负载均衡,处理静态文件的性能比Apache高三倍以上。它还支持高并发连接,每秒最多的并发连接请求理论上可以达到50000个。\[2\] 然而,Nginx也有一些劣势。它的守护进程受限于php-fpm,一个输入参数可能会导致整个程序运行不下去。此外,Nginx的rewrite性能不如Apache。\[3\] 综上所述,选择使用哪个Web服务器取决于具体的需求和场景。如果需要处理高并发请求和静态文件,以及简洁的配置和高性能,可以考虑使用Nginx。而如果需要更多的模块和功能,以及对rewrite性能有更高要求,可以选择Apache。 #### 引用[.reference_title] - *1* *2* [nginxapache的区别,选择哪个好?](https://blog.csdn.net/liujinwei2005/article/details/122797385)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [ApacheNginx的区别](https://blog.csdn.net/weixin_53560205/article/details/120929157)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值