← All writing

5 min readPaymentsHTTPInfrastructure

When a web page has to answer a POST

Our payment page only answered GET, until a merchant's system could only send customers to it with a form POST. Why that happens, why nginx says 405, and the ways to fix it.

Our hosted payment page was a single-page app served by nginx. You opened a URL, nginx sent back index.html, and the app took over. Every link and redirect to it used GET, and that was fine for years.

Then a merchant integrated in a way we hadn’t seen. Their server sent customers to our payment page by submitting an HTML form, which makes the browser send a POST. Their setup couldn’t send a plain GET redirect. Our page answered with an error and the customer never reached checkout.

The fix was a few lines of nginx config. Why it was needed is more interesting.

Why anyone sends a POST to open a page

Opening a page “should” be a GET. But a lot of the web, especially around payments and sign-in, moves people between sites with a POST: an HTML form, often hidden, that submits itself the moment the page loads. A few common reasons:

  • A plain redirect can’t carry a POST. A server can tell a browser “go here” with a redirect, but that always becomes a GET. If the next site expects data in a POST body, the only way to get the browser to send it is a form. Developers integrating hosted payment pages run into this often.
  • Payment gateways ask for it. Many hosted checkouts are documented as “send the customer here with a form POST, or an auto-submitting form.” PayGate’s web payment docs and the hosted payment session model at CommBank both describe it.
  • Card authentication works this way. In 3-D Secure, after the cardholder authenticates with their bank, the bank’s server returns them to the merchant with an HTTP POST carrying the result.
  • Sign-in flows prefer it for secrets. OAuth’s form_post response mode and SAML’s POST binding put tokens in a POST body instead of the URL. Values in a URL can end up in browser history, server logs and referrer headers; query-string tokens are the least safe option.
  • Some systems simply can’t do anything else. Older platforms, or setups with strict outbound rules, sometimes only know how to hand a customer over with a form. That was our merchant.

So “this page must accept a POST” isn’t a strange request. It’s normal for any page that sits on the receiving end of a payment or sign-in handoff.

Why nginx answers 405

nginx serving static files only knows how to read a file for GET and HEAD. POST means “do something with this data,” and a static file can’t, so nginx refuses with 405 Not Allowed. It’s a very common surprise.

A single-page app makes this sharper. Your index.html is a static file, so every route in the app is “a static file” to nginx, and every one of them rejects a POST.

A trap: the page can’t read the POST anyway

Before choosing a fix, there’s a catch worth knowing. JavaScript in the page can’t see the body of the POST that loaded it. The browser sends the form data to the server and never hands it to the page. If the sender puts something important in the POST body, a purely static page has no way to read it. Developers using OAuth’s form_post with a front-end-only app hit exactly this wall.

In our case that didn’t matter: everything our page needed, like the payment session, was already in the URL. The merchant only needed the request to succeed. That kept the fix in nginx.

The fixes, roughly in order of preference

1. Answer POST with the same HTML. Tell nginx that when a POST arrives for the app, it should respond as if it were a GET for index.html. People usually do this with nginx’s error_page directive, catching the 405 and returning the page with a 200 instead:

location /pay/ {
  try_files $uri $uri/ /pay/index.html;
  # A POST can't be served as a static file, so answer it with the app's page instead of 405.
  error_page 405 =200 /pay/index.html;
}

Treat this as a starting point, not a recipe. Guides and forum answers disagree on the details, which change between nginx versions and depend on how your locations are set up. Check it with a real request:

curl -i -X POST https://your-site.example/pay/some-session

You want a 200 with your HTML, not a 405.

2. Hand POSTs to a small server. If the POST body carries data the page needs, nginx alone can’t help. Send POSTs to a tiny backend that reads the body and either renders the page with that data or redirects to the GET version. This is the robust choice for 3-D Secure returns and form_post sign-ins.

3. Ask the sender to use GET. Sometimes the best fix is upstream. But you don’t control your merchants’ servers, and “please change your integration” is a slow conversation when a customer is waiting to pay.

We went with the first: the page now answers a POST to the app’s path with index.html. While we were in the config, we also added explicit locations for a health check and the favicon, so those requests stop falling through to the app.

What I took from it

  • Your page is part of other people’s systems. A payment page isn’t only visited. It’s integrated into merchants’ servers, which have their own constraints. Accepting the common ways people hand customers over is part of being integrable.
  • “Static” has edges. Serving a single-page app as static files is simple until something arrives that isn’t a GET. Knowing what your web server does with other methods is worth five minutes before a merchant finds out for you.
  • Know where the data lives. If a handoff puts data in the POST body, a static page can’t see it. Check that before deciding the fix can live entirely in nginx.
↑↓ move↵ openesc close