Search code examples
nginxdeploymentsanic

Deploy Sanic server next to Nginx


I'm trying to deploy a Sanic app next to Nginx. I want Nginx to handle:

  1. File serving (my SPA and other assets)
  2. Certbot/letsencrypt ssl (can do without)

And I want Sanic to handle my API endpoints.

I know how to handle each separately. However, I don't know how to make them run next to each other. As far as I know, you can't have two services listening on the same TCP port. If that's the case, should I just make Nginx act as a reverse proxy to Sanic? If so, how would you go about it?

Any guidance would be appreciated.


Solution

  • This is my preferred way to run Sanic, behind nginx as you described. Then just proxy to Sanic which is listening on some other port.

    server {
       ...
        location /api/ {
            proxy_pass http://sanic-app:1234/;
        }
    }