I've been trying to use unix sockets for a while but I'm quite lost without any documentation... So far, Nginx is configured correctly, the socket is there in Python, and I see a socket file in the correct directory, but Nginx still gives a 502 Bad Gateway.
Here's my code:
if __name__ == "__main__":
import socket
sock = socket.socket(socket.AF_UNIX)
sock.bind('/tmp/api.sock')
app.run(sock=sock)
When starting, it only says:
[2017-11-24 21:00:15 +0000] [22978] [INFO] Starting worker [22978]
instead of the usual:
[2017-11-24 21:00:30 +0000] [22979] [INFO] Goin' Fast @ http://127.0.0.1:8000
[2017-11-24 21:00:30 +0000] [22979] [INFO] Starting worker [22979]
Any possibility we could get some documentation on how to use this feature?
@davidtgq Maybe, it's better to use gunicorn for this?
@davidtgq I got this to work and I'm doing just like you showed in your example.
I had a quick look at the code, and it looks like the log message is missing because host and port is not set in app.run.
https://github.com/channelcat/sanic/blob/820d8c7bf5260b13ffb6a83ee4747bf87fe7d073/sanic/app.py#L809-L813
Are you sure the socket is readable and writable for Nginx?
Yup, looks like the problem was just the write permission. chmod 777 made it work. This was the permission it previously had:
srwxr-xr-x 1 root root 0 Nov 24 21:00 /tmp/api.sock
I still have to figure out what user nginx uses (I read from various sources www-data or daemon) and how to use os.chown(), but closing this for now since the issue is smaller than I thought it was. Thanks for the help.
e: for the benefit of anyone coming from google: Use ps -ef | grep nginx to find out what user nginx's worker is running as (it was www-data for me) and do shutil.chown('path/to/socket', 'www-data'). That's all!
Most helpful comment
Yup, looks like the problem was just the write permission.
chmod 777made it work. This was the permission it previously had:srwxr-xr-x 1 root root 0 Nov 24 21:00 /tmp/api.sockI still have to figure out what user nginx uses (I read from various sources
www-dataordaemon) and how to useos.chown(), but closing this for now since the issue is smaller than I thought it was. Thanks for the help.e: for the benefit of anyone coming from google: Use
ps -ef | grep nginxto find out what user nginx's worker is running as (it waswww-datafor me) and doshutil.chown('path/to/socket', 'www-data'). That's all!