NextJS on Apache

220 views Asked by At

After build my NextJs application and run, it works well on apache using the follow .htaccess. However I'm having problem with dynamic routes (something like: posts/[id]), for these cases it is returning chunk erros.

RewriteEngine On

DirectoryIndex disabled

RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://127.0.0.1:4001/ [P,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:4001/$1 [P,L]

1

There are 1 answers

0
bknights On

Even when you use mod_rewrite for this you should still include a ProxyPassReverse directive.

from the Apache docs:

RewriteCond "%{REQUEST_FILENAME}"       !-f
RewriteCond "%{REQUEST_FILENAME}"       !-d
RewriteRule "^/(.*)" "http://old.example.com/$1" [P]
ProxyPassReverse "/" "http://old.example.com/"

Discussion: In each case, we add a ProxyPassReverse directive to ensure that any redirects issued by the backend are correctly passed on to the client.

Consider using either ProxyPass or ProxyPassMatch whenever possible in preference to mod_rewrite.