[Bug]: Default nginx config breaks routing and gives 404 to routes other than /
Error Message and Logs
After 2 hours of debugging I noticed that we add a default nginx configuration to static sites. This broke the routing of my react app and I couldn't hit any of the routes except the base / route. Removing the nginx config fixes the issue. I noticed the issue started around v4.0.0-400.
Steps to Reproduce
- Deploy a React SPA as a static site.
- Check "is static site" off and then on again so that default nginx config is loaded.
- Redeploy.
- Go to a path like /x on the app. Nginx gives 404. Go to home page of React app at / and it is fine. Click on a link that routes to /x, it is fine again.
- Go to Coolify > Remove custom nginx configuration > Redeploy
- Paths work as expected on the React app.
Example Repository URL
No response
Coolify Version
v4.0.0-400
Are you using Coolify Cloud?
No (self-hosted)
Operating System and Version (self-hosted)
No response
Additional Information
No response
Adding priority to my router fixes the issue (possibly UI rules were being considered after my backend routes).
traefik.http.routers.<router-name>.priority=10
@semihsezer you need to pick nixpacks build pack and is static, not just static build pack with nginx
Reason is nginx is trying to find the files statically, whereas if built it gets served correctly.
https://coolify.io/docs/applications/vite as example
It only works on nginx when it actually fails to find the file and sends request to /
P.s the priority thing works the other way, the lower the number the lower the priority, it is based on the length of the rule by default.
Hi @djsisson thanks for your response! I am using Nixpacks like you desecibed - let me know if that's not the case.
The reason why I reported it was because this was working fine for 2 months and I did not change any configuration on my side.
@semihsezer yeah sorry I misread, I think a recent change broke this, since all requests need to go to index.html
https://github.com/coollabsio/coolify/commit/2c845461c95700920b794c7b5eb43c9eb546a532#diff-c5b79badffca7f1b143d696d6fe5c78647926f51993f09a3bcf0feb051aad49eL4059
Can you show nginx conf again and add the index.html back in for your conf
Or try something like this
server {
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html =404;
}
# Handle 404 errors
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
internal;
}
# Handle server errors (50x)
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
internal;
}
}
I haven't tested this but you basically want all manual requests to go to index.html
@djsisson thanks for your response - I leave nginx blank to make it work and it just goes with the default. I would have to check this through ssh, which I can do if that helps. I see that Andras put a fix in already, that is so quick!
So looking at the fix, I should just not check "is static" so that it behaves like an SPA?