Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

Apache Proxy Error: The proxy server received an invalid response from an upstream server (502, AH01102)

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

When Apache, acting as a reverse proxy, shows a 502 page with the status line 502 Proxy Error and the text "The proxy server received an invalid response from an upstream server", it asked the backend application for a response and did not get a usable one. The page carries a Reason: line, and the error log usually holds a matching entry such as AH01102: error reading status line from remote server. Those two lines say whether the backend closed the connection, did not answer, or sent a status line the proxy refuses.

What the error means

The 502 text is Apache's own. Reading modules/http/http_protocol.c at tag 2.4.62, the code builds the error body for status 502 from "The proxy server received an invalid" and "response from an upstream server.". In modules/proxy/proxy_util.c the code builds the status line as "%3.3u Proxy Error" (so 502 Proxy Error), and the body adds "The proxy server could not handle the request" followed by "Reason:" and a message chosen by the proxy code.

The message that matters is the Reason. Reading ap_proxy_http_process_response in modules/proxy/mod_proxy_http.c (the module for HTTP and HTTPS backends, tag 2.4.62), the code gives these Reasons with status 502:

  • Error reading from remote server, after the log line error reading status line from remote server with the backend host and port (log code APLOGNO(01102), shown as AH01102). This is the case when Apache could not read the first line of the response.
  • Corrupt status line returned by remote server: followed by what was received. The code uses it when the first line matches the pattern HTTP/#.# ### and the major version is not 1, or when the line is too long. A first line that does not match that pattern is not refused: the code marks it as an HTTP/0.9 response (its comment), sets the status to 200 and passes the data on, so no 502 results.
  • Unexpected Upgrade:, Malformed connection header and Too many (N) interim responses from origin server, for the respective protocol faults.

Reading the same function, what the code does after logging AH01102 depends on the connection. For a reverse proxy, when the client connection has already served earlier requests and the read did not time out, Apache closes the client connection without sending the page. If the backend connection had also been used before, the code then returns without an error status (its comment calls it a keepalive race); otherwise it returns status 500. In the remaining cases, including a read that timed out, Apache returns the 502 with Error reading from remote server. So the 502 page is one of several outcomes of the same log line, and the error log is the better guide.

The mod_proxy_http documentation describes the keepalive race behind the common case: the variable proxy-initial-not-pooled "avoids the "proxy: error reading status line from remote server" error message caused by the race condition that the backend server closed the pooled connection after the connection check by the proxy and before data sent by the proxy reached the backend." It also warns that "setting this variable downgrades performance".

Likely causes

Cause What you see How to confirm
The backend closed an idle pooled connection that Apache then reused AH01102 on the first request after a quiet period; a retry works Failures follow idle gaps; the backend's keepalive timeout is shorter than the time Apache keeps the connection
A firewall between Apache and the backend dropped the idle connection silently The same pattern, at a fixed idle time The mod_proxy page names this case for disablereuse and keepalive
The backend process crashed, restarted or reset the connection AH01102 with the backend host and port; the backend log shows a restart A direct request to the backend fails or hangs, or its log shows the crash
The backend took longer than the proxy timeout Reason: Error reading from remote server after the same number of seconds each time The failure time equals ProxyTimeout, the timeout setting, or the core Timeout (default 60)
The backend answers with a status line HTTP/ and a major version other than 1, or a very long first line Reason: Corrupt status line returned by remote server: followed by the first bytes A direct curl -v to the same URL shows that status line
A protocol upgrade or interim responses the proxy did not expect Reason: Unexpected Upgrade: or Too many ... interim responses The Reason line names it

Prove the cause before changing anything

All of these only read or send test requests. Run them from the Apache host.

1. Read the exact Reason.

curl -i https://app.example.com/path

The curl manual says -i (--show-headers) shows response headers in the output. Read the status line and the Reason: text in the body.

2. Find the matching log entry.

grep -n 'error reading status line' /var/log/apache2/error.log

The Apache log documentation says the error log's "name and location is set by the ErrorLog directive", so use the path your configuration names. The path above is an example. Note the time and the backend host:port in each entry, and whether entries cluster after idle gaps.

3. Ask the backend directly, bypassing Apache.

curl -sv -o /tmp/backend-test.out http://127.0.0.1:8080/path

The manual says -v makes curl "output verbose information", with lines prefixed > for headers sent and < for headers received, and -o writes the body to a file. A good result shows < HTTP/1.1 200 (or the status your application returns) and a body. A bad result is a refused connection, no reply, a reply after a long delay, or something that is not an HTTP status line.

4. Time the backend.

curl -s -o /tmp/backend-test.out -w '%{time_total}\n' http://127.0.0.1:8080/path

The manual says time_total is "The total time, in seconds, that the full operation lasted". Compare it with the configured timeouts.

5. Read the proxy settings.

grep -Rn -E 'ProxyPass|ProxyTimeout|ttl=|timeout=|disablereuse' /etc/apache2 /etc/httpd

One of the two folders will not exist on your server. The mod_proxy page gives ProxyTimeout the default "Value of Timeout", and the core page gives Timeout the default 60 seconds.

Fix

The change depends on the cause the checks showed.

  • A crashed or restarting backend is repaired on the backend. Apache cannot compensate for a process that is down.
  • A slow backend needs its slowness explained first. ProxyTimeout is, in the module's words, useful "when you have a slow/buggy appserver which hangs", and the timeout parameter of ProxyPass sets the socket wait per worker, but a longer timeout only moves the wait.
  • A refused status line means the backend itself sends it, so it is corrected on the backend. A reply that is not HTTP at all is not refused by the code in Apache 2.4.62; it is passed on as HTTP/0.9 data, so it shows up as odd page content, not as this 502.
  • An idle connection closed by the backend or a firewall is handled by stopping Apache from reusing a connection after the idle time. The mod_proxy page documents these ProxyPass parameters: ttl, after which a connection "will not be used again"; disablereuse=on, which closes the connection after each use and "helps in various situations where a firewall between Apache httpd and the backend server ... tends to silently drop connections"; and keepalive, for a firewall that drops inactive connections. The mod_proxy_http page adds the environment variable proxy-initial-not-pooled quoted above; the Apache documentation does not show how that variable is set, so it is not used here. The step below uses ttl. That choice, and the value in the example, are ours: the mod_proxy page says ttl "can be used to avoid using a connection which is subject to closing because of the backend server's keep-alive timeout", and the disablereuse alternative gives up connection reuse for every request.

The backend's idle limit decides the value. If the backend is Apache with its default KeepAliveTimeout of 5 seconds (the core page's default), a ttl of 4 is a value below it (our example value; the page says only that connections unused for at least ttl seconds "will be closed", so it is not a guarantee).

The mod_proxy page also says: "With the prefork MPM, connections to backends are not pooled (each child process handles one connection at a time). The acquire and ttl parameters, which control connection pool behavior, have no effect when using prefork." Check the MPM first. The MPM documentation says httpd -l lists "every module that is compiled into the server, including the MPM", and apachectl passes its arguments to httpd:

apachectl -l

If the list shows prefork.c, ttl has no effect on that server and this fix does not apply; the idle-connection cause is then handled on the backend or with the other parameters above. The MPM documentation also describes building an MPM as a loadable module, which a list of compiled-in modules would not show; if the list names no MPM, check how your server was built.

First save the file that holds the ProxyPass line. The path is an example:

cp -p /etc/apache2/sites-available/app.example.com.conf /etc/apache2/sites-available/app.example.com.conf.2026-12-01

Disruptive step: the next edit changes how Apache reuses connections to the backend, and the reload that follows applies the new configuration to the live server. A ttl that is too low turns most requests into new backend connections and raises load on both servers.

Add the ttl key to the existing ProxyPass line in that file (the ProxyPass syntax takes [key=value ...] after the URL), keeping your own path and URL:

ProxyPass "/app/" "http://127.0.0.1:8080/app/" ttl=4

Test, and reload only if the test reports Syntax OK:

apachectl configtest
apachectl graceful

The apachectl manual says graceful does not abort open connections and "automatically checks the configuration files as in configtest".

To undo it: restore the saved file, test, and reload.

ls -l /etc/apache2/sites-available/app.example.com.conf.2026-12-01 && cp -p /etc/apache2/sites-available/app.example.com.conf.2026-12-01 /etc/apache2/sites-available/app.example.com.conf
apachectl configtest
apachectl graceful

Example (illustrative): web01 (192.0.2.10) proxies /app/ to an Apache backend on 127.0.0.1:8080 that uses the default KeepAliveTimeout 5. Users on a new browser connection see the 502 page with Reason: Error reading from remote server on the first click after a quiet minute; a second click works. The error log shows error reading status line from remote server 127.0.0.1:8080 at the same moments, and curl -sv straight to the backend returns HTTP/1.1 200. The engineer saves the file, adds ttl=4 (below 5), tests and reloads. The same click after a quiet minute now opens a new backend connection.

Confirm the fix

  1. apachectl configtest printed Syntax OK and the reload ran.
  2. Wait longer than the backend's idle limit and repeat the request that failed. It returns the application's status, not 502.
  3. The error log shows no new error reading status line entries for that backend after the reload. Check entries dated after the reload only.

What to watch afterwards

Risks and limits

  • These commands have not been run or lab-tested.
  • The Reason strings and the branch described are a reading of the source at tag 2.4.62. The Apache documentation does not list them, and a packaged build may differ in details.
  • The module documentation describes the keepalive race and the parameters but gives no value for ttl; the figure 4 above is our example and depends on the backend. The ttl parameter has no effect with the prefork MPM.
  • Only mod_proxy_http is covered. Other proxy modules produce other Reason texts.
  • The Apache documentation does not show how to set proxy-initial-not-pooled; the steps therefore use ttl, which is shown as a ProxyPass parameter.

Sources