Monitor and record the running of Apache website servers

Source: Internet
Author: User
Article Title: monitors and records the running of Apache website servers. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source. Introduction to logs under Apache

Apache provides tools to extensively record all aspects of runtime information. Such as conditional logging, log loops, and IP addresses. Many third-party modules and tools used to check the status of your Apache server and analyze its logs.

Default Apache Log File

Apache provides many detection and log tools to track the correct running of servers. The default Apache configuration provides two log files, which are placed in the log directory under the installation directory.

The access_log file (corresponding to the access. log File in windows) contains information about the requests that have been processed by the server, such as the request URL, Client IP address, and whether the request has been completed successfully. The error_log file (corresponding to the error. log File in windows) contains information related to the error situation and different events in the server lifecycle.

Create log format
LogFormat "% h % l % u % t \" % r \ "%> s % B" common
LogFormat "% h % l % u % t \" % r \ "%> s % B"
\ "% {Referer} I \" \ "% {User-agent} I \" "combined

The LogFormat command allows you to tell Apache what aspects of the request you want to record. You still need to attach commands to tell Apache where to record the information, which will be described in the next chapter. The following example shows the two most popular formats: Common log format and integrated log format. When Apache receives a request, it uses the corresponding request attribute to replace each domain prefixed with %. If you are using a common log format, each input in your log file looks like this:
192.168.200.4-someuser [12/Jun/2005: 08: 33: 34
+ 0500] "GET/example.png HTTP/1.0" 200 1234

If you are using the Integrated log format, each input in your log file looks like this:
192.168.200.4-someuser [12/Jun/2005: 08: 33: 34
+ 0500] "GET/example.png HTTP/1.0" 200 1234
Http://www.example.com/index.html & quot; Mozilla/5.0
(Windows; U; Windows NT 5.1; en-US; rv: 1.7.7 )"

The following table describes some of the most important fields despite the detailed indexing of the log format provided by the attachment:
# % H: The current IP address or domain name of the client (for example, a browser) when sending a connection request to the server (HostNameLookups must be enabled ).
# % U: indicates the user ID recorded when the HTTP authentication is used.
# % T: the time when the server received the connection request.
# % R: The text information in the original connection request sent by the client, including the HTTP method used.
# %> S: The Return status code after the server responds to the browser. 200 indicates that the request is successful ..
# % B: The content (in bytes) of the returned object of a single request sent by the server to respond to the browser. The header bytes are not counted.
The integrated log format expands two additional domains based on the Common log format. Defined:
# % {Referer} I: Connection Request packet header, containing the document association information pointing to the current page.
# % {User-agent} I: The User agent connects to the request packet header, which contains information about the customer's browser.

Create a custom log file
CustomLog logs/access_log common
TransferLog logs/sample. log

You may want to create a new log file other than Apache. The following example uses CustomLog to create a new log file and save the information defined by a previously defined log format, that is, the common file mentioned in the previous chapter. You can also replace the nickname with the definition of the format itself. An additional, simpler command is Transferlog, which only accepts the definition provided by the last LogFormat command.

Redirect logs to an external program
TransferLog "| bin/rotatelogs/var/logs/apachelog
86400"


You can also use CustomLog or TransferLog to redirect the log output to an external program instead of a file. To do this, you must first start with the output character "|", followed by the program that receives standard log input information. This example uses the rotatelogs program that comes with Apache, which will be introduced later.

When an external program is used, it will be run as the user who starts httpd. If the server is started by the super administrator, it will be a super administrator, fully ensuring the security of this program. In addition, when entering a file path on a non-Unix platform, be careful to ensure that only the forward slash is used, even if the platform may allow the use of the backslash. In general, it is a good idea to always use a forward slash in the configuration file.

Conditional log requests

SetEnvIf Request_URI "(\. gif | \. jpg) $" image
CustomLog logs/access_log common env =! Image
SetEnvIf Remote_Addr 192 \. 168 \. 200 \. 5 specialmachine
CustomLog logs/special_access_log common env = specialmachine

You can decide whether to record a request based on the variable environment. This variable can be set in advance based on many parameters, such as the IP address of the client or the existence of a header in the request. As shown in this example, the CustomLog command can accept a variable environment as the third parameter. If a variable environment exists, it will be recorded, otherwise it will not. If this variable environment is "! "Negative at the beginning, no variable environment will be recorded. This example will show you how to avoid recording images in GIF and JPEG formats in logs, and how to request a separate log file from a specific IP address. For another example, see the next section.

Who is connecting to your website?
SetEnvIfNoCase Referer www \. example \. com internalreferral
LogFormat "% {Referer} I-> % U" referer
CustomLog logs/referer. log referer env =! Internalreferral

You can record the Referer value to check who is connected to your website. the Referer variable is located in the header of the Connection Request Packet sent by the user, the data packet header also contains the URL of the target website accessed by the user. This method can be used to record the vast majority of website visitors. You can also exclude visitors from a specific website (www \. example \. com) from the log file.
Use module parameters (mod_status) to monitor Apache servers

SetHandler server-status
Order Deny, Allow
Deny from all
Allow from 192.168.0


There are many functional modules that can be used in the Apache server, including built-in and plug-ins. The status and performance of these modules are recorded through the mod_status parameter, the recorded content includes "which modules are involved in the website response service, which modules are idle, and the server enabling/disabling time. Number of connection requests and visitors being processed (ExtendedStatus mark must be specified)-This module records a great impact on the Performance of High-load website servers ". The module status statistics recorded in this example can be viewed on the http://www.example.com/server-statuspage in a browser.

[1] [2] Next page

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.