1

I was trying to find out the difference between a socket as a unix file and a socket that is a ip and port.

If I switch something like uwsgi to use a file instead of a port and ip, how do I then access uwsgi as a URL in my browser, since no port is now specified for my flask application.

J.Zil
  • 1,123
  • 3
  • 21
  • 29

3 Answers3

3

Since this is the first result on Google for how to bind a flask UWSGI app to a unix socket, here is the answer to the question in the title.

From flask:

app.run(host="unix:///tmp/app.sock")

or

FLASK_APP=app.py flask run --host=unix:///tmp/app.sock

If using gunicorn:

gunicorn -w 4 package:app --bind unix:/tmp/app.sock
I.Am.A.Guy
  • 181
  • 1
  • 4
2

You can run uwsgi to listen to a TCP/IP port, for example uwsgi --http :80 , if you change it to listen to a Unix socket (for ex: uwsgi --socket :8000 ) then as you said, this is local only, no web service (TCP/IP) is exposed to the Internet and you have to put a web server like nginx in front of uwsgi as 'proxy' that can get the http requests from the outside world (see https://gist.github.com/evildmp/3094281 for a django example instead of Flask)

LinuxDevOps
  • 1,774
  • 9
  • 14
  • 3
    This doesn't actually answer the question of using a Unix socket. – larsks Oct 29 '16 at 15:04
  • @larsks It does answer the question. It specifies exactly what you need (nginx or similar) in the middle to access it from a browser. – Benny Mackney Jun 17 '21 at 05:22
  • 1
    It does not provide any example of using a unix socket. The examples in this answer show how to use network sockets. A unix socket is an entry in the filesystem, not a network port. – larsks Jun 17 '21 at 12:33
0

Browsers don't support making requests to Unix sockets. Here's a feature request that was opened with Chromium, but they responded that they would not be adding support for that use case.