Understanding Request Processing Lifecycle
1. Understanding Connection Acceptance Phase
| Phase | Description | Key Components | Notes |
|---|---|---|---|
| Listen Directive | Configures the IP address and port to listen for incoming connections | <VirtualHost>, Listen |
Can be specified multiple times for different addresses/ports |
| AcceptFilter | Controls how the server accepts new connections (e.g., none, async) |
AcceptFilter |
Available on FreeBSD and some Unix-like systems |
| MaxRequestWorkers | Maximum number of simultaneous requests that can be handled | MaxRequestWorkers |
Defaults to 150; affects performance under load |
Example: Basic Listen Configuration
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
Listen 80
Listen 443
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
</VirtualHost>
Note: The
Listen directive must be specified before any <VirtualHost> blocks to ensure proper binding.
Warning: Binding to
0.0.0.0 allows connections from any IP address; use with caution in production environments.
2. Processing Request Headers
| Header | Description | Example Value | Notes |
|---|---|---|---|
| Accept | Specifies the media types that are acceptable for the response | text/html,application/json |
Used for content negotiation |
| Authorization | Contains credentials for HTTP authentication | Basic dXNlcjpwYXNz |
Used for basic or digest authentication |
| Content-Type | Indicates the media type of the resource | application/json |
Required for POST/PUT requests with body content |
| Host | Specifies the domain name of the server and port number if different from default | example.com:8080 |
Mandatory in HTTP/1.1; used for virtual hosting |
| User-Agent | Contains information about the client agent | Mozilla/5.0 (Windows NT 10.0; Win64; x64) |
Used for logging and analytics |
| Referer | Indicates the address of the previous web page from which a link to the currently requested page was followed | https://example.com/page1 |
May be omitted or spoofed for privacy reasons |
| Cookie | Contains stored cookies from the client | sessionid=abc123; theme=dark |
Used for session management and tracking |
| Accept-Encoding | Indicates which content encoding (e.g., gzip) is acceptable for the response | gzip, deflate |
Used for compression of response bodies |
| Cache-Control | Directs caching mechanisms in both requests and responses | no-cache |
Controls caching behavior for both client and server |
| X-Forwarded-For | Identifies the original IP address of a client connecting through a proxy or load balancer | 192.168.1.100 |
Used for logging and access control in proxy environments |
Example: Setting Custom Headers in Apache
<IfModule mod_headers.c>
Header set X-Custom-Header "value"
Header set Access-Control-Allow-Origin "*"
</IfModule>
Note: Apache processes headers in a specific order during the request lifecycle. The
mod_headers module allows you to set, modify, or remove headers at various stages of processing.
Warning: Setting headers like
Content-Type or Host can interfere with normal operation if not done carefully. These headers are often managed automatically by the server.
3. Mapping URI to Filesystem
| URI Component | Mapping Rule | Notes |
|---|---|---|
| DocumentRoot | /var/www/html |
Base directory for document files |
| DirectoryIndex | index.html index.php |
Files to serve when directory is requested |
| Alias | /images /var/www/images |
Map URI to different filesystem path |
| ScriptAlias | /cgi-bin /usr/lib/cgi-bin |
Map URI to executable scripts |
Example: DirectoryIndex Configuration
<Directory "/var/www/html">
DirectoryIndex index.html index.php
</Directory>
Example: Alias Directive
Alias /images /var/www/images
<Directory "/var/www/images">
Require all granted
</Directory>
Note: The
Alias directive maps a URL path to a filesystem path, which is different from Directory which only affects directory-level configuration.
4. Applying Directory and Location Directives
| Directive | Scope | Description | Example |
|---|---|---|---|
| Directory | Filesystem path | Applies configuration to a specific directory and its subdirectories | |
| Location | URL path | Applies configuration to a specific URL path regardless of filesystem location | |
| Files | File pattern | Applies configuration to files matching a pattern | |
| FilesMatch | Regex pattern | Applies configuration to files matching a regex | |
| IfModule | Module availability | Conditionally applies directives based on module presence | |
Example: Combining Directory and Location Directives
<Directory "/var/www/html">
Options -Indexes
AllowOverride None
</Directory>
<Location "/private">
Require user admin
</Location>
Note: The
Directory directive matches the filesystem path, while Location matches the URL path. These can be used together to control access at both levels.
Processing Order
- Server configuration is processed in order of directive definition
- More specific directives take precedence over general ones
Locationdirectives are processed afterDirectoryones
5. Running Access Control Checks
| Directive | Description | Default | Notes |
|---|---|---|---|
| Allow | Allows access to specific hosts or IP addresses | None | Must be used within <Directory>, <Location>, or <Files> blocks |
| Deny | Denies access to specific hosts or IP addresses | None | Must be used within <Directory>, <Location>, or <Files> blocks |
| Order | Determines the order in which Allow and Deny directives are processed |
Allow, Deny |
Values: Allow,Deny, Deny,Allow, Order Deny,Allow |
Example: Basic Access Control
<Directory "/var/www/html">
Order Deny,Allow
Deny from all
Allow from 192.168.1.0/24
</Directory>
Example: Using Allow and Deny Together
<Location "/admin">
Order Allow,Deny
Allow from 10.0.0.0/8
Deny from 10.0.0.100
</Location>
Note: The
Order directive controls whether Allow or Deny is processed first. In older versions of Apache, Order Deny,Allow was the default and allowed access by default unless explicitly denied.
Warning: The
Allow and Deny directives are deprecated in Apache 2.4+. Use <Require> instead.
6. Executing Authentication Handlers
| Handler | Description | Configuration Directive | Notes |
|---|---|---|---|
| authn | Authentication handler for verifying user credentials | AuthName, AuthType |
Used in conjunction with Require to enforce access control |
| authz | Authorization handler for checking permissions | Require |
Determines whether authenticated users have access to resources |
| mod_authn_file | Authentication using a file-based password database | AuthUserFile |
Supports htpasswd-style files for storing credentials |
| mod_authn_dbm | Authentication using a DBM-style password database | AuthDBMUserFile |
Alternative to mod_authn_file, supports DBM formats |
| mod_authn_anon | Anonymous authentication support | Anonymous |
Allows access without credentials for specific directories |
Example: Basic Authentication Setup
<Directory "/var/www/secure">
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/apache2/htpasswd
Require valid-user
</Directory>
Note: Authentication handlers are executed in the order specified by
AuthOrder or default module loading sequence. Ensure that required modules like mod_authn_file are loaded.
Warning: Never store passwords in plain text. Use
htpasswd utility to generate secure password files.
Authentication Handler Workflow
- User requests a protected resource
- Server checks for authentication directives in the config
- Handler verifies user credentials using configured backend (e.g., file, DBM)
- If valid, authorization handler checks permissions
- Access is granted or denied based on
Requirerules
7. Processing Authorization Rules
| Directive | Description | Values | Notes |
|---|---|---|---|
| Allow | Allows access to specific hosts or IP addresses | from all |
Can be used in <Directory>, <Files>, or <Location> blocks |
| Deny | Denies access to specific hosts or IP addresses | from all |
Can be used in <Directory>, <Files>, or <Location> blocks |
| Require | Defines authorization rules using all, user, group, or ip |
all, user, group, ip |
Available in Apache 2.4+ |
| AuthType | Specifies the authentication method to use | Basic, Digest, Form |
Used in conjunction with AuthName and AuthUserFile |
| AuthName | Defines the realm name for authentication | String | Used in conjunction with AuthType |
| AuthUserFile | Specifies the file containing user credentials | Path to file | Used with AuthType Basic or Digest |
Example: Basic Authorization
<Directory "/secure">
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
Example: IP-Based Access Control
<Directory "/admin">
Require ip 192.168.1
Require ip 10.0.0
</Directory>
Note: In Apache 2.4+,
Allow and Deny are deprecated in favor of Require.
Warning: Misconfiguring authorization rules can lead to unintended access or denial of legitimate users.
8. Executing Content Handlers
| Handler | Description | Configuration | Notes |
|---|---|---|---|
| handler | Defines the content handler for a specific file type or URI | SetHandler |
Used in .htaccess or httpd.conf |
| mod_rewrite | Enables URL rewriting using regular expressions | RewriteEngine On |
Can be used with mod_setenvif for conditional logic |
| mod_actions | Allows defining custom actions for specific HTTP methods or URIs | Action |
Can execute external scripts or internal redirects |
Example: Using SetHandler to execute a script as content
<Files "*.php">
SetHandler "proxy:fcgi://127.0.0.1:9000"
</Files>
Example: Using mod_rewrite to redirect a URL
RewriteEngine On
RewriteRule ^old-page\.html$ /new-page.html [R=301,L]
Note: Content handlers are processed in the order they appear in the configuration files, and can be overridden by more specific directives.
9. Applying Output Filters
| Filter Name | Description | Usage | Notes |
|---|---|---|---|
| mod_filter | Enables dynamic filtering of output content | <IfModule mod_filter.c> |
Available in Apache 2.4+ |
| Filter | Applies a filter to response content | FilterProvider myFilter gzip |
Can be applied to specific content types |
Example: Enabling GZIP Compression
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/json
</IfModule>
Example: Custom Filter Configuration
<IfModule mod_filter.c>
FilterProvider myFilter gzip
FilterChain myFilter
</IfModule>
Note: Output filters are applied after content is generated but before it's sent to the client.
10. Generating Response Headers
| Directive | Description | Default | Notes |
|---|---|---|---|
| Header | Sets a response header field | None | Can be used multiple times to set different headers |
| AddOutputFilterByType | Applies an output filter based on content type | None | Used to modify response headers or body |
| SetEnvIf | Sets environment variables based on request headers | None | Can be used to conditionally set response headers |
Example: Setting a Custom Header
<IfModule mod_headers.c>
Header set X-Custom-Header "value"
</IfModule>
Example: Conditional Header Based on User Agent
<IfModule mod_setenvif.c>
SetEnvIf User-Agent "MSIE" is_ie
</IfModule>
<IfModule mod_headers.c>
Header set X-Browser "IE" env=is_ie
</IfModule>
Note: The
Header directive can be used in various contexts (server config, virtual host, directory, .htaccess) and supports various actions like set, append, unset.
Warning: Be cautious when modifying response headers as incorrect configurations can lead to security vulnerabilities or break client functionality.
11. Sending Response Body
| Directive | Description | Default | Notes |
|---|---|---|---|
| OutputBufferSize | Size of the output buffer in bytes | 8192 |
Controls how much data is buffered before sending to client |
| EnableSendfile | Enables use of sendfile() system call for file transfers | on |
Improves performance for static file serving |
| BufferedLogs | Enables buffering of log output | off |
Can improve performance in high-volume logging scenarios |
Note: Increasing
OutputBufferSize can improve performance for large responses, but may increase memory usage.
Warning: Disabling
EnableSendfile may reduce performance for static file delivery.
12. Logging Request Completion
| Directive | Description | Default | Notes |
|---|---|---|---|
| LogFormat | Defines the format of log entries | %h %l %u %t "%r" %>s %b |
Customizable using tokens |
| ErrorLog | Path to the error log file | logs/error_log |
Can be set to syslog for system logging |
| CustomLog | Path to the access log file | logs/access_log |
Can be configured to use different formats or log files |
Example: Configuring Custom Log Format
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined_with_referer
CustomLog logs/access_log combined_with_referer
</VirtualHost>
Note: The
LogFormat directive allows for flexible customization of log entries, including timestamps, request methods, response codes, and user agent strings.