Apache server settings and optimization 2

Source: Internet
Author: User
Tags dns names
ErrorLogvarloghttpd-error.logLogLevelwarnLogFormat % h % l % u % t % r %; s % B % {Referer} I % {User-Agent} combinedLogFormat % h % l % u % t % r %; s % bcommonLogFormat % {Referer} I-; % UrefererLogFormat % {User-agent} iagent #

ErrorLog/var/log/httpd-error.log LogLevel warn LogFormat "% h % l % u % t" % r "%; s % B "% {Referer} I" "% {User-Agent}" "combined LogFormat" % h % l % u % t "% r" %; s % B "common LogFormat" % {Referer} I-; % U "referer LogFormat" % {User-agent} I "agent #

ErrorLog/var/log/httpd-error.log
LogLevel warn
LogFormat "% h % l % u % t" % r "%>; s % B" % {Referer} I "" % {User-Agent} "" combined
LogFormat "% h % l % u % t" % r "%>; s % B" common
LogFormat "% {Referer} I->; % U" referer
LogFormat "% {User-agent} I" agent
# CustomLog/var/log/httpd-access.log common
# CustomLog/var/log/httpd-referer.log referer
# CustomLog/var/log/httpd-agent.log agent
CustomLog/var/log/httpd-access.log combined

The system log format is defined here.ServerError Log. ErrorLog and LogLevel are used to define different error log files and their record content.
For system access logs, the CustomLog parameter is used by default to define the log location, and the combined parameter is used by default to specify to put all access logs in one file, however, you can also store different types of access logs in different log records by specifying different record types in CustomLog. Common indicates the access record of a single page request, and referer indicates the reference record of each page. The number of requests contained in a page can be seen, and agent indicates the type record of the client, obviously, you can define the existing combinedSetComment out the lines and use common, referer, and agent as the CustomLog parameters to specify log records for different types of logs.
Obviously, LogFormat is used to define different types of logs for recording. Here, macro definitions starting with % are used to record different contents. If the file specified by these parameters uses a relative path, it is the path relative to ServerRoot.

ServerSignature On
In some cases, for example, when the requested webpage does not exist,ServerThe error document will be generated. By default, because the ServerSignature option is enabled, the last line of the error document will containServerName, Apache version, and other information. Some administrators prefer not to display this information externally.SetIs Off, orSetIs Email, and the last line will be replaced with the Email prompt for ServerAdmin.

Alias/icons/"/www/icons /"
Options Indexes MultiViews
AllowOverride None
Order allow, deny
Allow from all

The Alias parameter is usedServerThe real location in the file system is directly mapped. General documents will be queried in DocumentRoot. However, the paths defined by Alias will be directly mapped to the corresponding directory, instead of querying under DocumentRoot. Therefore, Alias can be used to map the paths of some public files. For example, it saves the icons paths of common icons. In this way, in addition to using symbolic connections, directories outside the document root directory (DocumentRoot) can also be accessed by using Alias ing. After the ing path is defined, you must use the Directory statement.SetAccess restriction.

ScriptAlias/cgi-bin/"/www/cgi-bin /"
AllowOverride None
Options None
Order allow, deny
Allow from all

ScriptAlias is also used for URL path ing, but unlike Alias, ScriptAlias is used to map the path of CGI programs. All files in this path are defined as CGI programs, execute them to get results, insteadServerDirectly return its content. By default, the CGI program uses the cgi-bin directory as the virtual path.

# Redirect old-URI new-URL
The Redirect parameter is used to override the URL. When the browser accessesServerWhen a nonexistent resource on,ServerReturn a new URL to the browser, telling the browser to obtain resources from the URL. This is mainly usedServerAfter changing the location of the document, and hope to use the old URL to access, to maintain compatibility with the previous URL.

IndexOptions FancyIndexing
AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip
AddIconByType (TXT,/icons/text.gif) text /*
AddIconByType (IMG,/icons/image2.gif) image /*
AddIconByType (SND,/icons/sound2.gif) audio /*
AddIconByType (VID,/icons/movie.gif) video /*
AddIcon/icons/binary.gif. bin. exe
AddIcon/icons/binhex.gif. hqx
AddIcon/icons/tar.gif. tar
AddIcon/icons/world2.gif. wrl .wrl.gz. Fig. vrm. iv
AddIcon/icons/compressed.gif. Z. z. tgz. gz. zip
AddIcon/icons/a.gif. ps. ai. eps
AddIcon/icons/layout.gif. html. shtml. htm. pdf
AddIcon/icons/text.gif. txt
AddIcon/icons/c.gif. c
AddIcon/icons/p.gif. pl. py
AddIcon/icons/f.gif.
AddIcon/icons/dvi.gif. dvi
AddIcon/icons/uuencoded.gif. uu
AddIcon/icons/script.gif. conf. sh. shar. csh. ksh. tcl
AddIcon/icons/tex.gif. tex
AddIcon/icons/bomb.gif core
AddIcon/icons/back.gif ..
AddIcon/icons/hand.right.gif README
AddIcon/icons/folder.gif ^ DIRECTORY ^
AddIcon/icons/blank.gif ^ BLANKICON ^
DefaultIcon/icons/unknown.gif
# AddDescription "GZIP compressed document". gz
# AddDescription "tar archive". tar
# AddDescription "GZIP compressed tar archive". tgz
ReadmeName README
HeaderName HEADER
IndexIgnore .?? **~ * # HEADER * README * rcs cvs *, v *, t
When the URL of an HTTP request is a directory,ServerReturns the index file in this directory. However, if a directory does not contain the default index fileServerWhen the list of directory files is displayed, the list of files in the directory is displayed. In order to make the list of files understandable, it is not just a simple list, you need the precedingSetParameters. If the IndexOptions FancyIndexing option is used, you can setServerVarious icons are referenced for different types of documents in the generated directory list. The following AddIconByEncoding, AddIconByType, and AddIcon are used to determine which icons are used based on MIME encoding, type, and file suffix. If you cannot determine the icon used in the document, use the default icon defined by DefaultIcon.
Similarly, AddDescription can be used to add different descriptions for different types of documents. And,ServerStill in the directory, query the files defined by ReadmeName and HeaderName (automatically add the. html extension, if not found, then use the. txt extension for search). If these files are found, the content of these files is first displayed before the file list, to make the list of common directories more understandable.
IndexIgnoreServerIgnore the corresponding file when listing the file list. The file name is defined in mode configuration.

AddEncoding x-compress Z
AddEncoding x-gzip gz

AddEncoding is used to tell the compressed MIME type, so that the browser can perform the decompression operation.

AddLanguage en. en
AddLanguage fr. fr
AddLanguage de. de
AddLanguage da. da
AddLanguage el. el
AddLanguage it. it
LanguagePriority en fr de

An HTML document can be used to prepare multiple languages at the same time. For example, a file1.html document can be used to prepare different versions such as file1.html.en1_file1.html. fr. Each Language suffix must be defined using AddLanguage. This wayServerUsers in different countries can negotiate with the browser to send different language versions. The Semantic agepriority defines the priority levels of different languages, so that when the browser does not have special requirements, the requests for file1.html are returned in different languages in sequence. There are not many practical applications of this internationalization capability.
# AddType application/x-httpd-php. phtml
# AddType application/x-httpd-php-source. phps

The AddType parameter can specify the MIME type for a file with a specific suffix.SetWill overwriteSet.

# AddHandler cgi-script. cgi
AddHandler is used to specify a non-static processing type. It is used to define a document as a non-static file type and needs to be processed before returning the processing result to the browser. For exampleSetIs a file ending with. cgiSetThe cgi-script type.ServerThe CGI program will be started for processing. If you want to execute the CGI program outside the path defined by AliasScript, you need to use this parameter.SetThen, the file ending with. cgi will be executed as a CGI program. The CGI program must be allowed in the configuration file,. htaccess in this directory, and. htaccess in its parent directory. This must be set through the Options ExecCGI parameter.

# AddType text/html. shtml
# AddHandler server-parsed. shtml

Another type of dynamic processing is server-parsed.ServerYou can analyze the tags on the webpage in advance and change the tags to the correct HTML tags. Because server-parsed needs to process text/html documents, the corresponding .shtml must be of the text/html type.
However, to support SSI, you must first in the configuration file (or. in htaccess), the Options using des allows the documents in this directory to be of the SSI type, or use Options using desnoexec to execute a common SSI flag, but do not execute external programs referenced in it.
Another way to specify the server-parsed type is to use XBitBackSetOption.SetOn,ServerCheck all text/HTML documents (documents with the suffix of .html). If the file property has the execution bit "x ",ServerThink it isServerAnalysis document, requiredServer. We recommend that you use AddHandler.SetAnd XBitBackSetIt is Off because XBitBack performs additional checks on all HTML documents, reducing the efficiency.

# AddHandler send-as-is asis
# AddHandler imap-file map
# AddHandler type-map var

The AddHandler mentioned above is used to support ApacheServerAsis, map, and var processing capabilities

# Action media/type/cgi-script/location
# Action handler-name/cgi-script/location

Because Apache provides limited processing functions internally, you can use ActionServerDefine an external program as a dynamic file type that can be processed. These external programs are the same as the Standard CGI program. They process the input data and then output results of different MIME types. For example, to define an operation that executes wri2txt for the special suffix wri and then returns the result, you can use:
Action windows-writer/bin/wri2txt
AddHandler windows-writer wri
Furthermore, you can directly use Action to define a pre-processing operation for a MIME type. This requires the Action parameter in the first format in the example.SetMethod. This waySetIn this way, no additional AddHandler is required to associate the processing operation with the file suffix. Instead, Action is used to directly process MIME-type files. However, if the document suffix does not have a formal MIME type, you must first define a MIME type.

# MetaDir. web
# MetaSuffix. meta

Meta Information is pre-sent to the customer's browser before the document is sent to the customer. Therefore, the browser can access the Meta information through the HEAD request without actually returning all the document data through GET.ServerGenerally, some standard HTTP header information is sent to the browser. To add additional information, you need to use MetaDir to define the directory where Meta data is stored, metaS uffix is used to specify the file suffix that contains Meta data.

# ErrorDocument 500 "The server made a boo.
# ErrorDocument 404/missing.html
# ErrorDocument 404/cgi-bin/missing_handler.pl
# ErrorDocument 402
Http://some.other _ server.com/subscription_info.html
If the requested webpage does not exist or you do not have the access permission,ServerAn error code will be generated, and a webpage with an incorrect logo will be responded to the customer's browser. ErrorDocument is usedSetWhen an error occurs, it should respond to the content in the client browser. The first parameter of ErrorDocument is the error serial number, and the second parameter is the response data, which can be simple text or local webpage, local CGI programs and web pages on remote hosts.

BrowserMatch "Mozilla/2" nokeepalive
BrowserMatch "MSIE 4.0b2;" nokeepalive downgrade-1.0 force-response-1.0
BrowserMatch "RealPlayer 4.0" force-response-1.0
BrowserMatch "Java/1.0" force-response-1.0
BrowserMatch & quot; JDK/1.0 & quot; force-response-1.0

The BrowserMatch command is a specific client program,SetSpecial parameters to ensure compatibility with earlier browsers and support new features of new browsers.

# SetHandler server-status
# Order deny, allow
# Deny from all
# Allow from. your_domain.com
# SetHandler server-info
# Order deny, allow
# Deny from all
# Allow from. your_domain.com
# Deny from all
# ErrorDocument 403 http://phf.apache.org/phf_abuse_log.cgi

UsedSetResource Access ManagementSetMainly for directories and filesSetHowever, you can also control access to different URLs.SetIn this way, you do not have to worry about whether ScriptAlias and Alias will routeSetOut of the controlled directory. The URL-specific control statement is the Location statement.ServerIn addition, it can not find the corresponding file, butServerSpecial Feature URL provided by Alibaba Cloud. Http: // servername/server-status is used to report the current ApacheServerHttp: // servername/server-info for reporting ApacheServer. RelatedSetThe ExtendedStatus parameter also allows youServerOutput more detailed reports.

# ProxyRequests On
# Order deny, allow
# Deny from all
# Allow from. your_domain.com
# ProxyVia On
# CacheRoot "/www/proxy"
# CacheSize 5
# CacheGcInterval 4
# CacheMaxExpire 24
# CacheLastModifiedFactor 0.1
# CacheDefaultExpire 1
# NoCache a_domain.com another_domain.edu joes. garage_sale.com

ApacheServerIt has the proxy function, but this requires loading the mod_proxy module. This can be determined using the IfModule statement. If the mod_proxy module exists, use ProxyRequests to enable Proxy Support. The following Directory is usedSetAccess Permissions for Proxy FunctionsSetAnd usedSetBuffer ParametersSet.

Virtual Host
# NameVirtualHost 12.34.56.78: 80
# NameVirtualHost 12.34.56.78
# ServerAdmin webmaster@host.some_domain.com
# DocumentRoot/www/docs/host. some_domain.com
# ServerName host. some_domain.com
# ErrorLog logs/host. some_domain.com-error_log
# CustomLog log logs/host. some_domain.com-access_log common

DefaultSetThe content in the file is usedSetName the basic VMServer. NameVirtualHost specifies the IP address used by the VM. This IP address corresponds to multiple DNS names. If Apache uses the Listen parameter to control multiple ports, then you can add the port number here to further differentiate different connection requests for different ports. Afterwards, the VirtualHost statement is used to specify the IP address specified by NameVirtualHost as the parameter, and the corresponding virtual host is defined for each name.Set.
A vm is deployed on a WebServerCan provide Web Services for multiple independent domain names, and each domain name is completely independent, including a completely independent document directory structure andSetIn this way, domain names are completely independent, not only the content accessed by each domain name is completely independent, but also the webpage content provided by other domain names cannot be accessed using another domain name.
The concept of a VM is very useful, because although an organization can hook its own web page with other domain namesServerBut the use of independent domain names and root URLs is more formal, easy for everyone to accept. Traditionally, you must set up one on your ownServerIn order to achieve the purpose of a separate domain name, however, it is necessary to maintain a separateServerMany small organizations lack adequate maintenance capabilities, and the more appropriate way is to lease others for maintenance.Server. There is no need to provide a separate agencyServerYou can use the virtual host capabilityServerProvides Web Services for multiple domain names, and different services do not interfere with each other.Server.
There are two ways to set the virtual host. One is based on the HTTP 1.0 standard and one needs to have multiple IP addressesServerAnd then configure DNSServerTo assign different domain names to each IP address before configuring the Apache configuration file.ServerDifferent Web documents are returned for different domain names. Because this requires additional IP addresses, each domain name to provide services must use a separate IP address. Therefore, this method has many problems. You can bind multiple IP addresses to a network interface. in Linux, you need to use the alias parameter of ifconfig for this configuration, but this will affect network performance.

The HTTP 1.1 Standard specifiesServerDuring communication,ServerThe name of the host requested by the browser. Therefore, you can use this new feature to easily set virtual hosts. This method does not require additional IP addresses, but requires support from new browsers. This method has become a standard method for creating virtual hosts. To create a non-IP-based Vm, multiple domain names are indispensable because each domain name corresponds to a VM to be served. Therefore, you need to change the DNSServerForServerMultiple c name options are added, such:
Linux in a 192.168.1.64
Vhost1 in cname linux
Vhost2 in cname linux
BasicSetAll options are set for linux Hosts. To set virtual hosts for vhost1 and vhost2, you must use the VirtualHost statement to define different options. In the statement, you can use most of the options in front of the configuration file, and almost all of them can be redefined.ServerOfSet.

NameVirtualHost 192.168.1.64
DocumentRoot/www/data
ServerName linux.example.org.cn
DocumentRoot/vhost1
ServerName vhost1.example.org.cn
DocumentRoot/vhost2
ServerName vhost2.example.org.cn

Note that the VirtualHost parameter address must be consistent with the address defined by NameVirtualHost, and all values must be strictly consistent. ApacheServerThis is the virtual host defined for this IP address.

After NameVirtualHost is defined, access to this IP address is processed by different virtual hosts, and access to other IP addresses, such as 127.0.0.1, to apply the default options defined earlier.

Other acceleration Methods

1. Web pages and CGI pages adopt browser buffering technology. For specific methods, refer to Appendix 1: Using mod_gzip to accelerate Apache
2. Do not run X Windows on your WebServerAnd press Ctrl-Alt-Backspace to close X.
3. In the CGI script:
File I/O: the smaller the number of opened files, the better.
Shell Command: use the full path to call the shell command.
In the Web page Directory, do not set the number of files to more than 1000. The more files, the more time they spend locating them.
4.ServerThe smaller the number of images on the image, the better. Make sure that each image runs through the image compressors.
5. Try to configure as much memory as possible. The memory size has a great impact on system performance.
6. You can use the strace command to debug the call.
When you are not sure aboutSetWhen changing the impact on the process, strace may work. By observing the number of lines output, you can determine the number of calls required for access to a fixed page. For example:
# Ps-ef | grep http
# Strace-p http_pid
Accept (18, 0xFFBEFA50, 0xFFBEFA74, 1) = 3
Fcntl (24, F_SETLKW, 0x001D0E10) = 0
Sigaction (SIGUSR1, 0xFFBEF908, 0xFFBEF988) = 0
Getsockname (3, 0xFFBEFA60, 0xFFBEFA74, 1) = 0
Setsockopt (3, 6, 1, 0xFFBEF9C4, 4, 1) = 0
Read (3, "g e t/c a s e I n c"..., 4096) = 590
Sigaction (SIGUSR1, 0xFFBED800, 0xFFBED880) = 0
Time () = 1043377552
Stat ("/docroot/osc-new/www/caseincident/listCase. php", 0x0022BF58) = 0
Umask (077) = 0
Umask (0) = 077
Setitimer (ITIMER_PROF, 0xFFBEF628, 0x00000000) = 0
Sigaction (SIGPROF, 0xFFBEF4F8, 0xFFBEF578) = 0
Sigprocmask (SIG_UNBLOCK, 0xFFBEF618, 0x00000000) = 0
Getsockopt (6, 65535,819 2, 0xFFBE67B0, 0xFFBE67AC, 0) = 0
Setsockopt (6, 65535,819 2, 0xFFBE67B0, 4, 0) = 0
Setitimer (ITIMER_PROF, 0xFFBEF788, 0x00000000) = 0
Sigaction (SIGUSR1, 0xFFBEF908, 0xFFBEF988) = 0
Read (3, 0x001E5BC0, 4096) (sleeping ...)

Appendix 1. Use mod_gzip to accelerate Apache
Note: This appendix is a full-text reference with a time limit and has not been tested.

We know that mod_gzip is used inServerThe client compresses the content requested by the customer and sends it to an Apache module of the client. Zope on this site uses FastCGI to achieve integration with Apache. Apache is a service program for parsing HTML pages. After mod_gzip is installed, the common static pages can be compressed as well as the Zope document.

I. Installation
Http://www.hyperspacecommunications.com/products/mod_gzip.html
Download the mod_gzip binary code version and source code. If you use the former, copy the file to the libexec directory of Apache Home. After downloading mod_gzip.c, if your Apache is installed in DSO mode, run the following simple command to install it:
#/Www/bin/apxs-I-a-c mod_gzip.c

Ii. Configuration

After installation, add the following configurations to httpd. conf:
# Mod_gzip Configuration
LoadModule gzip_module libexec/mod_gzip.so
AddModule mod_gzip.c

Mod_gzip_on Yes
Mod_gzip_minimum_file_size 1002
Mod_gzip_maximum_file_size 0
Mod_gzip_maximum_inmem_size 60000
Mod_gzip_item_include mime "application/x-httpd-php"
Mod_gzip_item_include mime "text /*"
# Compressing Zope content:
Mod_gzip_item_include file "[^.] * $"
Mod_gzip_item_include mime "httpd/unix-directory"
Mod_gzip_dechunk Yes
Mod_gzip_temp_dir "/tmp"
Mod_gzip_keep_workfiles No
Mod_gzip_item_include file "\. php $"
Mod_gzip_item_include file "\. txt $"
Mod_gzip_item_include file "\. html $"
Mod_gzip_item_exclude file "\. css $"
Mod_gzip_item_exclude file "\. js $"


And add the following rows:

LogFormat "% h % l % u % t \" % r \ "%>; s % B \ "% {Referer} I \" \ "% {User-Agent} I \" mod_gzip: % {mod_gzip_compression_ratio} npct. "mod_gzip

Modify the log file parameter in the corresponding VM configuration to mod_gzip

3. The compression effect mod_gzip has been used to compress. html and those files without dots, with a compression ratio of 70-80%. However, mod_gzip does not compress the image file.

Iv. Summary
Combined with mod_gzip and buffer tools, we can make a low-configuration PC capable of handling various HTML/CGI requests.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.