Search code examples
djangoajaxnext.jsaxiosuwsgi

NextJS can't connect to uwsgi directly


I'm trying to build an application with django on backend-side and nextjs on frontend-side. Backend is ran by uwsgi.

uwsgi --http 0.0.0.0:3000 -w project.wsgi --master --harakiri=60 --pidfile=uwsgi.pid

When I'm trying to run npm run build I'm getting errors (I use axios but tried fetch too)

  cause: Error: socket hang up
      at Socket.socketOnEnd (node:_http_client:524:23)
      at Socket.emit (node:events:531:35)
      at endReadableNT (node:internal/streams/readable:1696:12)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    code: 'ECONNRESET'
  }

Looks like backend doesn't work... But when I run just common wget http://my-backend-url/api/v1/same-url-which-produced-errors everything works fine.

I've seen this error when I used connection between docker-containers and on one computer as well

Last time I managed it by using nginx as a proxy for uwsgi. But now I just want to understand how to fix it and how to make my app without nginx

PS The strange thing what I see in the uwsgi-logs is a stack of same requests (my frontend doesn't do this)

[pid: 64|app: 0|req: 13/13] 172.18.0.5 () {32 vars in 462 bytes} [Tue Jun 18 22:01:42 2024] GET /api/v1/page/?lang=en-US => generated 1755 bytes in 10 msecs (HTTP/1.1 200) 7 headers in 206 bytes (1 switches on core 0)

Ten and more times


Solution

  • Just add a parameter --http-keepalive to uwsgi run script