apache优化

1.开启gzip功能
ln -s /usr/local/zlib/lib/libz.so /usr/lib/
LoadFile /usr/lib64/libz.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
<ifmodule mod_deflate.c>
DeflateCompressionLevel 6
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/php
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE image/svg+xml
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI .(?:html|htm)$ no-gzip dont-varySetEnvIfNoCase
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary
</IfModule>

<ifmodule mod_headers.c>
<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
<FilesMatch ".(xml|txt)$">
Header set Cache-Control "max-age=18000, public, must-reva lidate"
</FilesMatch>
<FilesMatch ".(html|htm|php)$">
Header set Cache-Control "max-age=3600, must-reva lidate"
</FilesMatch>
</ifmodule>

2.开启expires缓存
LoadModule expires_module modules/mod_expires.so

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A86400
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/css A2592000
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType text/plain A604800
ExpiresByType application/x-shockwave-flash A604800
ExpiresByType video/x-flv A604800
ExpiresByType application/pdf A604800
ExpiresByType text/html A90000
<FilesMatch ".(gif|jpe?g|png)$">
Header set Cache-Control "max-age=604800"
Header unset ETag
</FilesMatch>
</IfModule>

3.开启模式配置
Include conf//extra/httpd-mpm.conf
vim /usr/local/apache/conf/extra/httpd-mpm.conf
<IfModule mpm_event_module>
StartServers 5
MinSpareThreads 100
MaxSpareThreads 200
ThreadsPerChild 50
ServerLimit 1000
MaxRequestWorkers 1000
MaxConnectionsPerChild 10000
</IfModule>

4.禁止目录遍历
Options Indexes(删除) FollowSymLinks

5.隐藏版本号
Include conf//extra/httpd-default.conf
vim httpd-default.conf
serversignature off
ServerTokens Prod

6.优化apache
vim httpd-default.conf
timeout 60 apache 写出数据到客户端连接的时间长度
keepalive on httpd 进程对每个请求的链接是否保持长链接
maxkeepaliverequests 100 最多保持多少个活动的长链接
keepalivetimeout 5 连接的保持时间,超过时间就回收
HostnameLookups Off 避免针对每个访问者的 DNS 域名的反向查询

7.防盗链
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !benet.com/.$ [NC]
RewriteCond %{HTTP_REFERER} !benet.com/.
$ [NC]
RewriteCond %{HTTP_REFERER} !www.benet.com/.$ [NC]
RewriteCond %{HTTP_REFERER} !www.benet.com/.
$ [NC]
RewriteRule .*.(gif|jpg|swf)$ http://www.benet.com/about/nolink.png [R,NC,L]

8.错误页面全部修改
ErrorDocument 402 /error-page.html
ErrorDocument 403 /error-page.html
ErrorDocument 404 /error-page.html
ErrorDocument 500 /error-page.html
ErrorDocument 501 /error-page.html
ErrorDocument 502 /error-page.html

9.禁止资源目录解析PHP程序
<Directory ~ “ /var/www/bbs/uploads”>
Options FollowSymLinks
AllowOverride None
Order allow, deny
Allow from all
php_flag engine off 添加这行
</Directory>

猜你喜欢

转载自blog.51cto.com/zuoshou/2119831