Is anyone having the issue with Error 504?

Is anyone having the issue with 504 Gateway time-out or Is it just my network on the server?
This doesn’t happened in localhost on my pc.

I have get this error when requested the page through Token Authentication(after /login ).
My project is running on Nginx and I tried to config the proxy server but it doesn’t work.

Much appreciated if anyone can share how to fix the problem.

1 Like

I’m having the exact same problem with Vapor and nginx

/api/users/ (ok)
/api/users/id (504 error)

It works on localhost though :thinking:

Hello @omaralbeik ,

After I rechecked my api both on localhost at my pc and Nginx on the cloud server.
For my case, It work properly.

But When I protect with CSRF tokens in Chapter 20 “Web Authentication” below code . For my case ,It happened “504” only on the cloud server ,but not on the localhost at my pc .

let protectedRoutes = authSessionRouters.grouped(RedirectMiddleware(path:“/login”))
protectedRoutes.get(“acronyms”,“create”,use: createAcronymHandler)

So far I tried to debug from Nginx log file . But still don’t catch any cause .

In my case, I had a User object that owns Todo children, and I use Token auth.
the problem is getting a Todo by its id is working only on localhost, but not Nginx
After a few hours of debugging, I fixed it by getting the children from the authenticated user and query over them.

This works:

This does not:

Still, I have no idea why this is the case :sweat_smile:

Is Nginx definitely forwarding the authentication headers?


Hello ,
Did you mean forward “Bearer Token” to proxy header?
My Nginx.conf is look like below.
Very appreciated if you could share about how to add the authentication header into the Sever block .

try_files $uri @proxy;
location @proxy {
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080;
proxy_redirect http:// https://;

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

After I run command " update vapor " then rebuild the project.
There are no issue regarding the error 504 .

Thanks vapor team to fix the problem.

@thammaporn Thank you for sharing the solution - much appreciated!

Hi all – Sorry, I think I’m missing something here. What was the solution to this issue? I’m having exactly the same problem. Is there an nginx config I need to change?

I have in my boot router:

let tokenAuthGroup = userRoutes.grouped(tokenAuthMiddleware, guardAuthMiddleware)

tokenAuthGroup.get(use: list)
tokenAuthGroup.get(User.parameter, use: get)

both work locally on my mac, but only list works on the server behind nginx.

The get function is:

func get(_ req: Request) throws → Future<User.Public> {
return try

Thanks so much!

Sorry, I wasn’t following the ‘update vapor’ comment from before. I ran ‘vapor update -y’ and that seems to have fixed the issue. FYI - I initially had an error when attempting the update, so I removed the Package.resolved file and then removed the .build directory. Then ‘vapor update -y’ ran with no error. I then reissued ‘swift build -c release’ and restarted my vapor app.

Hope this helps someone.

Thanks all!