Deploy¶
There are several options for sanic server deployment:
- Standalone server
- Running a pool of backend servers behind Nginx, HAProxy or another reverse proxy server. Several instances of sanic can be managed with systemd (Nginx+systemd)
Running sanic servers behind nginx makes several advantages:
- At first, Nginx is the perfect frontend server. It may prevent many attacks based on malformed HTTP protocol etc.
- Second, running several sanic instances behind nginx allows to utilize all CPU cores.
- Third, nginx serves static files much faster than built-in sanic static file support.
Template configuration files for nginx are presented below. For more information, you can visit sanic.readthedocs.io.
Configuration file for Nginx¶
upstream aiohttp {
server 127.0.0.1:5141 fail_timeout=0;
server 127.0.0.1:5142 fail_timeout=0;
}
server {
listen 5140;
client_max_body_size 4G;
server_name 127.0.0.1;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://aiohttp;
}
}
Configuration file for systemd¶
Config should be located in directory /etc/systemd/system/
[Unit]
Description=Luna Backport3 on port %i
After=network.target
[Service]
Restart=always
RestartSec=5
SyslogIdentifier=luna-backport3-%i
WorkingDirectory=/var/lib/luna/current/luna-backport3/luna_backport3
# Run from luna-configurator
ExecStart=/var/lib/luna/current/luna-backport3/venv/bin/python3.7 /var/lib/luna/current/luna-backport3/luna_backport3/run.py --port=%i --luna-config=http://127.0.0.1:5070/1 --log_suffix=%i
# Run from config file
# ExecStart=/var/lib/luna/current/luna-backport3/venv/bin/python3.7 /var/lib/luna/current/luna-backport3/luna_backport3/run.py --port=%i --log_suffix=%i --config=/var/lib/luna/current/luna-backport3/luna_backport3/configs/config.conf
[Install]
WantedBy=multi-user.target
Start 2 instances on ports 5141, 5142:
sudo systemctl start luna-backport3\@{5141..5142}.service
