2

I'm new to web development so I appreciate some hand-holding by the smart folks here. I'm trying to switch from gunicorn to Daphne for my Django app on GAE Flex, since I need to use Django Channels.

Previously the site worked fine with gunicorn as entrypoint (in the app.yaml file). I just replaced it with daphne (I'd like to avoid complexity by changing nginx configurations, Unix sockets etc so I guess I'm ok with Daphne serving both HTTP and ws requests). So now my app.yaml looks like:

runtime: python
env: flex

runtime_config:
  python_version: 3

entrypoint: daphne -b 0.0.0.0 -p 8001 my_project_name.asgi:application

I've already made a .asgi file next to my .wsgi file and declared an application there.

In requirements.txt I've ensured the daphne(2.4.1) and asgiref(3.2.3) packages are the latest versions.

Finally when I do 'gcloud app deploy', deployment appears to happen smoothly and in the build logs I can see daphne starting:

Step #1: Step 9/9 : CMD exec daphne -b 0.0.0.0 -p 8001 my_project_name.asgi:application
Step #1:  ---> Running in c6f3762a5ce2

But I'm getting a 502 Bad Gateway error on the site, with "nginx" in the next line.

Question: What am I doing wrong? Is this because Daphne is not serving http requests? Am I supposed to do anything different to make Daphne serve HTTP requests? Right now my http paths are served in Django by the urls.py module and not the routing.py module (which is taking care of only ws requests). Am I supposed to change that in some way?

If you feel that splitting incoming requests and sending http to gunicorn is the only way, please describe those steps. What happens to the entrypoint in the app.yaml file if I do that?

My efforts so far: I see many questions on SO (such as this and this) and other tutorials online on how to use daphne on standalone linux machines but not on app engines like GAE Flex. The only GAE-related post I saw was this, but it's a different problem from mine.

oktested
  • 81
  • 1
  • 7
  • hey @oktested , it seems for me that there is some misconfigurations between Daphne server and your application server. did you test the application locally? can you provide me with logs related to this error, you can find them in [Stackdriver logging](https://cloud.google.com/logging/docs/view/overview) section in Google Cloud? on which port you are running your app locally? – Methkal Khalawi Jan 21 '20 at 13:04
  • Hi @MethkalKhalawi - here are the logs from the gcloud console: To be clear, Daphne is running on port 8001. In the latest build, I changed the host to 127.0.0.1 instead of 0.0.0.0. Also I added a file named nginx.conf to my project folder which declared the upstream server and then did a proxy_pass to it under location /. Django server logs: ` 2020-01-21 13:05:52,608 INFO Starting server at tcp:port=8001:interface=127.0.0.1 2020-01-21 13:05:52,611 INFO Configuring endpoint tcp:port=8001:interface=127.0.0.1 2020-01-21 13:05:52,612 INFO Listening on TCP address 127.0.0.1:8001 ` – oktested Jan 21 '20 at 13:29
  • Nginx error logs: ' [error] 33#33: *341 connect() failed (111: Connection refused) while connecting to upstream, client: 172.217.194.153, server: , request: "GET / HTTP/1.1", upstream: "http://172.17.0.1:8080/", host: "projectoktested.appspot.com" ' – oktested Jan 21 '20 at 13:29
  • I have no idea what those IP addresses beginning with 172 are. I'm assuming 8080 is just the port that GAE listens at by default. The Django logs for the 502 error itself are quite useless - it just says 502, that's all. – oktested Jan 21 '20 at 13:32
  • did you test you application locally? – Methkal Khalawi Jan 21 '20 at 14:41
  • Yes, but that's without Daphne. It works fine locally. I need Daphne only during deployment to the cloud. Also, when I run the same command locally "daphne -b 127.0.0.1 -p 8001 my_project_name.asgi:application" it produces the same logs as on the Django server so it's fine (Starting server, configuring endpoint etc). So I suspect the problem is with the nginx config. How do I change the upstream address in the error logs (172.17.0.1:8080) or the nginx config on GAE Flex, since adding a file called nginx-app.conf doesn't seem to be doing it. – oktested Jan 21 '20 at 14:53
  • do you see any out of memory errors? check the logging. if so add these to your app.yaml `resources: memory_gb: 4` and redeploy your app again – Methkal Khalawi Jan 21 '20 at 15:33
  • Not seeing any out of memory errors. This is the only error I'm seeing. [error] 33#33: *921 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: , request: "GET / HTTP/1.1", upstream: "http://172.17.0.1:8080/", host: "projectoktested.appspot.com" It seems my app doesn't respond to nginx on port 8080. How do I setup the application handlers correctly in order to properly accept and respond to incoming requests? – oktested Jan 22 '20 at 06:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206418/discussion-between-methkal-khalawi-and-oktested). – Methkal Khalawi Jan 22 '20 at 07:59
  • 1
    I suspect you probably have solved the issue by now, but for anyone else who comes along, the 172 IPs are part of private addressing (like 192.168) and are internal IPs for docker/app engine. If Daphne is configured to only listen on 127.0.0.1, then it will reject any external connections (including other docker containers or the host), which appears to be what's happening here. Daphne is also listening on port 8001 which might have also been an issue as nginx is looking for port 8080. – Scott Stevens Feb 06 '21 at 21:51

0 Answers0