POST の引数を取る FastAPI を Nginx で動かす方法です。
メインプログラムは、次の通りとします。
/usr/local/fastapi/mail_post.py
完成形
次のスクリプトが正常に動くようにします。
http POST http://127.0.0.1/mail/ to='user01@example.com
uvicorn を Systemd で動かす
設定ファイル
/etc/systemd/system/uvicorn.service
[Unit]
Description=The uvicorn process
Before=nginx.service
After=network.target remote-fs.target nss-lookup.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/usr/local/fastapi
ExecStart=/usr/bin/uvicorn mail_post:app --reload
ExecReload = /bin/kill -s HUP $MAINPID
ExecStop = /bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
起動
sudo systemctl start uvicorn
動作確認
http POST http://127.0.0.1:8000/mail/ to='user01@example.com
Nginx の設定
リバースプロキシーの設定
/etc/nginx/sites-available/default
server {
(省略)
location /mail {
proxy_pass http://127.0.0.1:8000/mail; # /mail → /mail
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
(省略)
}
設定の確認
sudo nginx -t
Nginx の再起動
sudo systemctl restart nginx
動作確認
http POST http://127.0.0.1/mail/ to='user01@example.com