Category: Next.js, React and TypeScript

Next.js Server Action: Origin Mismatch Error

Read this guide in Polish

Writing through the form does not work only behind reverse proxy or custom domain and a message about origin mismatch appears. Server Actions verify the origin of the request, and proxy can transmit Host and X-Forwarded-Host incompatible with Origin.

How to identify the source of the problem

Check the headers Origin, Host and X-Forwarded-Host for problematic POST in a safe environment. Compare proxy configuration and list of actual trusted form domains.

Compare the message from the console with the result of the production compilation and the condition after the page has been refreshed.

Repair steps

Improve the transfer of headers in proxy and configure allowed origin only for actual host applications. Verify CSRF and the HTTPS connection, leaving a refusal for requests from untrusted domains.

How to Test Solution

The correct production domain form records the data and the alien origin is still blocked.

Security and restrictions

Do not set wildcard for each domain just to remove the error.