mod_proxy_ajp - Apache HTTP Server Version 2.4
Modules | Directives | FAQ | Glossary | Sitemap | Report a bug Apache HTTP Server Version 2.4 Apache > HTTP Server > Documentation > Version 2.4 > Modules Apache Module mod_proxy_ajp Available Languages: en | fr | ja Description: AJP support module for mod_proxy Status: Extension Module Identifier: proxy_ajp_module Source File: mod_proxy_ajp.c Compatibility: Available in version 2.1 and later Summary This module requires the service of mod_proxy .
It provides support for the Apache JServ Protocol version 1.3 (hereafter AJP13 ). Thus, in order to get the ability of handling AJP13 protocol, mod_proxy and mod_proxy_ajp have to be present in the server. Warning Do not enable proxying until you have secured your server . Open proxy servers are dangerous both to your network and to the Internet at large. Topics Usage Environment Variables Overview of the protocol Basic Packet Structure Request Packet Structure Response Packet Structure Directives This module provides no directives.
Bugfix checklist httpd changelog Known issues Report a bug See also mod_proxy Environment Variable documentation Usage ¶ This module is used to reverse proxy to a backend application server (e.g. Apache Tomcat) using the AJP13 protocol. The usage is similar to an HTTP reverse proxy, but uses the ajp:// prefix: Simple Reverse Proxy ProxyPass "/app" "ajp://backend.example.com:8009/app" Options such as the secret option of Tomcat (required by default since Tomcat 8.5.51 and 9.0.31) can just be added as a separate parameter at the end of ProxyPass or BalancerMember .
This parameter is available in Apache HTTP Server 2.4.42 and later: Simple Reverse Proxy with secret option ProxyPass "/app" "ajp://backend.example.com:8009/app" secret=YOUR_AJP_SECRET Balancers may also be used: Balancer Reverse Proxy <Proxy "balancer://cluster"> BalancerMember "ajp://app1.example.com:8009" loadfactor=1 BalancerMember "ajp://app2.example.com:8009" loadfactor=2 ProxySet lbmethod=bytraffic </Proxy> ProxyPass "/app" "balancer://cluster/app" Note that usually no ProxyPassReverse directive is necessary.
The AJP request includes the original host header given to the proxy, and the application server can be expected to generate self-referential headers relative to this host, so no rewriting is necessary. The main exception is when the URL path on the proxy differs from that on the backend. In this case, a redirect header can be rewritten relative to the original host URL (not the backend ajp:// URL), for example: Rewriting Proxied Path ProxyPass "/apps/foo" "ajp://backend.example.com:8009/foo" ProxyPassReverse "/apps/foo" "http://www.example.com/foo" However, it is usually better to deploy the application on the backend server at the same path as the proxy rather than to take this approach.
Environment Variables ¶ Environment variables whose names have the prefix AJP_ are forwarded to the origin server as AJP request attributes (with the AJP_ prefix removed from the name of the key). Please be aware that these request attributes are only accessible when directly asking for them using the HttpServletRequest.getAttribute method - Tomcat's implementation of the getAttributeNames method does not list them as those are handled as internal attributes.
Overview of the protocol ¶ The AJP13 protocol is packet-oriented. A binary format was presumably chosen over the more readable plain text for reasons of performance. The web server communicates with the servlet container over TCP connections. To cut down on the expensive process of socket creation, the web server will attempt to maintain persistent TCP connections to the servlet container, and to reuse a connection for multiple request/response cycles.
Once a connection is assigned to a particular request, it will not be used for any others until the request-handling cycle has terminated. In other words, requests are not multiplexed over connections. This makes for much simpler code at either end of the connection, although it does cause more connections to be open at once. Once the web server has opened a connection to the servlet container, the connection can be in one of the following states: Idle No request is being handled over this connection.
Assigned The connection is handling a specific request. Once a connection is assigned to handle a particular request, the basic request information (e.g. HTTP headers, etc) is sent over the connection in a highly condensed form (e.g. common strings are encoded as integers). Details of that format are below in Request Packet Structure. If there is a body to the request (content-length > 0) , that is sent in a separate packet immediately after.
At this point, the servlet container is presumably ready to start processing the request. As it does so, it can send the following messages back to the web server: SEND_HEADERS Send a set of headers back to the browser. SEND_BODY_CHUNK Send a chunk of body data back to the browser. GET_BODY_CHUNK Get further data from the request if it hasn't all been transferred yet. This is necessary because the packets have a fixed maximum size and arbitrary amounts of data can be included the body of a request (for uploaded files, for example). (Note: this is unrelated to HTTP chunked transfer).
END_RESPONSE Finish the request-handling cycle. Each message is accompanied by a differently formatted packet of data. See Response Packet Structures below for details. Basic Packet Structure ¶ There is a bit of an XDR heritage to this protocol, but it differs in lots of ways (no 4 byte alignment, for example). AJP13 uses network byte order for all data types. There are four data types in the protocol: bytes, booleans, integers and strings.
Byte A single byte. Boolean A single byte, 1 = true , 0 = false . Using other non-zero values as true (i.e. C-style) may work in some places, but it won't in others. Integer A number in the range of 0 to 2^16 (32768) . Stored in 2 bytes with the high-order byte first. String A variable-sized string (length bounded by 2^16). Encoded with the length packed into two bytes first, followed by the string (including the terminating '\0').
Note that the encoded length does not include the trailing '\0' -- it is like strlen . This is a touch confusing on the Java side, which is littered with odd autoincrement statements to skip over these terminators. I believe the reason this was done was to allow the C code to be extra efficient when reading strings which the servlet container is sending back -- with the terminating \0 character, the C code can pass around references into a single buffer, without copying. if the \0 was missing, the C code would have to copy things out in order to get its notion of a string.
Packet Size According to much of the code, the max packet size is 8 * 1024 bytes (8K) . The actual length of the packet is encoded in the header. Packet Headers Packets sent from the server to the container begin with 0x1234 . Packets sent from the container to the server begin with AB (that's the ASCII code for A followed by the ASCII code for B). After those first two bytes, there is an integer (encoded as above) with the length of the payload.
Although this might suggest that the maximum payload could be as large as 2^16, in fact, the code sets the maximum to be 8K. Packet Format (Server->Container) Byte 0 1 2 3 4...(n+3) Contents 0x12 0x34 Data Length (n) Data Packet Format (Container->Server) Byte 0 1 2 3 4...(n+3) Contents A B Data Length (n) Data For most packets, the first byte of the payload encodes the type of message. The exception is for request body packets sent from the server to the container -- they are sent with a standard packet header ( 0x1234 and then length of the packet), but without any prefix code after that.
The web server can send the following messages to the servlet container: Code Type of Packet Meaning 2 Forward Request Begin the request-processing cycle with the following data 7 Shutdown The web server asks the container to shut itself d
The rest of this page is on the live site.