coolify icon indicating copy to clipboard operation
coolify copied to clipboard

[Bug]: Default nginx config breaks routing and gives 404 to routes other than /

Open semihsezer opened this issue 8 months ago • 1 comments

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

  1. Deploy a React SPA as a static site.
  2. Check "is static site" off and then on again so that default nginx config is loaded.
  3. Redeploy.
  4. 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.
  5. Go to Coolify > Remove custom nginx configuration > Redeploy
  6. 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

semihsezer avatar Mar 30 '25 18:03 semihsezer

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 avatar Mar 30 '25 22:03 semihsezer

@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.

djsisson avatar Mar 31 '25 08:03 djsisson

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.

Image

semihsezer avatar Mar 31 '25 08:03 semihsezer

@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

djsisson avatar Mar 31 '25 08:03 djsisson

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 avatar Mar 31 '25 08:03 djsisson

@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?

semihsezer avatar Mar 31 '25 22:03 semihsezer