📄 perf-tuning.html.en
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX This file is generated from xml source: DO NOT EDIT XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX --><title>Apache Performance Tuning - Apache HTTP Server</title><link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" /><link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" /><link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link href="../images/favicon.ico" rel="shortcut icon" /></head><body id="manual-page"><div id="page-header"><p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p><p class="apache">Apache HTTP Server Version 2.0</p><img alt="" src="../images/feather.gif" /></div><div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div><div id="path"><a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.0</a> > <a href="./">Miscellaneous Documentation</a></div><div id="page-content"><div id="preamble"><h1>Apache Performance Tuning</h1><div class="toplang"><p><span>Available Languages: </span><a href="../en/misc/perf-tuning.html" title="English"> en </a> |<a href="../ko/misc/perf-tuning.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p></div> <p>Apache 2.x is a general-purpose webserver, designed to provide a balance of flexibility, portability, and performance. Although it has not been designed specifically to set benchmark records, Apache 2.x is capable of high performance in many real-world situations.</p> <p>Compared to Apache 1.3, release 2.x contains many additional optimizations to increase throughput and scalability. Most of these improvements are enabled by default. However, there are compile-time and run-time configuration choices that can significantly affect performance. This document describes the options that a server administrator can configure to tune the performance of an Apache 2.x installation. Some of these configuration options enable the httpd to better take advantage of the capabilities of the hardware and OS, while others allow the administrator to trade functionality for speed.</p> </div><div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#hardware">Hardware and Operating System Issues</a></li><li><img alt="" src="../images/down.gif" /> <a href="#runtime">Run-Time Configuration Issues</a></li><li><img alt="" src="../images/down.gif" /> <a href="#compiletime">Compile-Time Configuration Issues</a></li><li><img alt="" src="../images/down.gif" /> <a href="#trace">Appendix: Detailed Analysis of a Trace</a></li></ul></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="hardware" id="hardware">Hardware and Operating System Issues</a></h2> <p>The single biggest hardware issue affecting webserver performance is RAM. A webserver should never ever have to swap, as swapping increases the latency of each request beyond a point that users consider "fast enough". This causes users to hit stop and reload, further increasing the load. You can, and should, control the <code class="directive"><a href="../mod/mpm_common.html#maxclients">MaxClients</a></code> setting so that your server does not spawn so many children it starts swapping. This procedure for doing this is simple: determine the size of your average Apache process, by looking at your process list via a tool such as <code>top</code>, and divide this into your total available memory, leaving some room for other processes.</p> <p>Beyond that the rest is mundane: get a fast enough CPU, a fast enough network card, and fast enough disks, where "fast enough" is something that needs to be determined by experimentation.</p> <p>Operating system choice is largely a matter of local concerns. But some guidelines that have proven generally useful are:</p> <ul> <li> <p>Run the latest stable release and patchlevel of the operating system that you choose. Many OS suppliers have introduced significant performance improvements to their TCP stacks and thread libraries in recent years.</p> </li> <li> <p>If your OS supports a <code>sendfile(2)</code> system call, make sure you install the release and/or patches needed to enable it. (With Linux, for example, this means using Linux 2.4 or later. For early releases of Solaris 8, you may need to apply a patch.) On systems where it is available, <code>sendfile</code> enables Apache 2 to deliver static content faster and with lower CPU utilization.</p> </li> </ul> </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="runtime" id="runtime">Run-Time Configuration Issues</a></h2> <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="../mod/mod_dir.html">mod_dir</a></code></li><li><code class="module"><a href="../mod/mpm_common.html">mpm_common</a></code></li><li><code class="module"><a href="../mod/mod_status.html">mod_status</a></code></li></ul></td><td><ul><li><code class="directive"><a href="../mod/core.html#allowoverride">AllowOverride</a></code></li><li><code class="directive"><a href="../mod/mod_dir.html#directoryindex">DirectoryIndex</a></code></li><li><code class="directive"><a href="../mod/core.html#hostnamelookups">HostnameLookups</a></code></li><li><code class="directive"><a href="../mod/core.html#enablemmap">EnableMMAP</a></code></li><li><code class="directive"><a href="../mod/core.html#enablesendfile">EnableSendfile</a></code></li><li><code class="directive"><a href="../mod/core.html#keepalivetimeout">KeepAliveTimeout</a></code></li><li><code class="directive"><a href="../mod/prefork.html#maxspareservers">MaxSpareServers</a></code></li><li><code class="directive"><a href="../mod/prefork.html#minspareservers">MinSpareServers</a></code></li><li><code class="directive"><a href="../mod/core.html#options">Options</a></code></li><li><code class="directive"><a href="../mod/mpm_common.html#startservers">StartServers</a></code></li></ul></td></tr></table> <h3><a name="dns" id="dns">HostnameLookups and other DNS considerations</a></h3> <p>Prior to Apache 1.3, <code class="directive"><a href="../mod/core.html#hostnamelookups">HostnameLookups</a></code> defaulted to <code>On</code>. This adds latency to every request because it requires a DNS lookup to complete before the request is finished. In Apache 1.3 this setting defaults to <code>Off</code>. If you need to have addresses in your log files resolved to hostnames, use the <code class="program"><a href="../programs/logresolve.html">logresolve</a></code> program that comes with Apache, or one of the numerous log reporting packages which are available.</p> <p>It is recommended that you do this sort of postprocessing of your log files on some machine other than the production web server machine, in order that this activity not adversely affect server performance.</p> <p>If you use any <code><code class="directive"><a href="../mod/mod_access.html#allow">Allow</a></code> from domain</code> or <code><code class="directive"><a href="../mod/mod_access.html#deny">Deny</a></code> from domain</code> directives (i.e., using a hostname, or a domain name, rather than an IP address) then you will pay for two DNS lookups (a reverse, followed by a forward lookup to make sure that the reverse is not being spoofed). For best performance, therefore, use IP addresses, rather than names, when using these directives, if possible.</p> <p>Note that it's possible to scope the directives, such as within a <code><Location /server-status></code> section. In this case the DNS lookups are only performed on requests matching the criteria. Here's an example which disables lookups except for <code>.html</code> and <code>.cgi</code> files:</p> <div class="example"><p><code> HostnameLookups off<br /> <Files ~ "\.(html|cgi)$"><br /> <span class="indent"> HostnameLookups on<br /> </span> </Files> </code></p></div> <p>But even still, if you just need DNS names in some CGIs you could consider doing the <code>gethostbyname</code> call in the specific CGIs that need it.</p> <h3><a name="symlinks" id="symlinks">FollowSymLinks and SymLinksIfOwnerMatch</a></h3> <p>Wherever in your URL-space you do not have an <code>Options FollowSymLinks</code>, or you do have an <code>Options SymLinksIfOwnerMatch</code> Apache will have to issue extra system calls to check up on symlinks. One extra call per filename component. For example, if you had:</p> <div class="example"><p><code> DocumentRoot /www/htdocs<br /> <Directory /><br /> <span class="indent"> Options SymLinksIfOwnerMatch<br /> </span> </Directory> </code></p></div> <p>and a request is made for the URI <code>/index.html</code>. Then Apache will perform <code>lstat(2)</code> on <code>/www</code>, <code>/www/htdocs</code>, and <code>/www/htdocs/index.html</code>. The results of these <code>lstats</code> are never cached, so they will occur on every single request. If you really desire the symlinks security checking you can do something like this:</p> <div class="example"><p><code> DocumentRoot /www/htdocs<br /> <Directory /><br /> <span class="indent"> Options FollowSymLinks<br /> </span> </Directory><br /> <br /> <Directory /www/htdocs><br /> <span class="indent"> Options -FollowSymLinks +SymLinksIfOwnerMatch<br /> </span> </Directory> </code></p></div> <p>This at least avoids the extra checks for the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code> path. Note that you'll need to add similar sections if you have any <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code> or <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> paths outside of your document root. For highest performance, and no symlink protection, set <code>FollowSymLinks</code> everywhere, and never set <code>SymLinksIfOwnerMatch</code>.</p> <h3><a name="htacess" id="htacess">AllowOverride</a></h3> <p>Wherever in your URL-space you allow overrides (typically <code>.htaccess</code> files) Apache will attempt to open <code>.htaccess</code> for each filename component. For example,</p> <div class="example"><p><code> DocumentRoot /www/htdocs<br /> <Directory /><br /> <span class="indent"> AllowOverride all<br /> </span> </Directory> </code></p></div> <p>and a request is made for the URI <code>/index.html</code>. Then Apache will attempt to open <code>/.htaccess</code>, <code>/www/.htaccess</code>, and <code>/www/htdocs/.htaccess</code>. The solutions are similar to the previous case of <code>Options FollowSymLinks</code>. For highest performance use <code>AllowOverride None</code> everywhere in your filesystem.</p> <h3><a name="negotiation" id="negotiation">Negotiation</a></h3> <p>If at all possible, avoid content-negotiation if you're really interested in every last ounce of performance. In practice the benefits of negotiation outweigh the performance penalties. There's one case where you can speed up the server. Instead of using a wildcard such as:</p> <div class="example"><p><code> DirectoryIndex index </code></p></div> <p>Use a complete list of options:</p> <div class="example"><p><code> DirectoryIndex index.cgi index.pl index.shtml index.html </code></p></div> <p>where you list the most common choice first.</p> <p>Also note that explicitly creating a <code>type-map</code> file provides better performance than using <code>MultiViews</code>, as the necessary information can be determined by reading this single file, rather than having to scan the directory for files.</p> <p>If your site needs content negotiation consider using <code>type-map</code> files, rather than the <code>Options MultiViews</code> directive to accomplish the negotiation. See the <a href="../content-negotiation.html">Content Negotiation</a> documentation for a full discussion of the methods of negotiation,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -