uWSGI + nginx + systemd

This chapter provides an example for configuring uWSGInginxsystemd for a Pyramid application.

在下面,您可以找到几乎可以生产的配置。”几乎“因为有些 uwsgi 参数可能需要调整以满足您的需要。

这里显示了一个示例SystemD配置文件:

 1# /etc/systemd/system/pyramid.service
 2
 3[Unit]
 4Description=pyramid app
 5
 6# Requirements
 7Requires=network.target
 8
 9# Dependency ordering
10After=network.target
11
12[Service]
13TimeoutStartSec=0
14RestartSec=10
15Restart=always
16
17# path to app
18WorkingDirectory=/opt/env/wiki
19# the user that you want to run app by
20User=app
21
22KillSignal=SIGQUIT
23Type=notify
24NotifyAccess=all
25
26# Main process
27ExecStart=/opt/env/bin/uwsgi --ini-paste-logged /opt/env/wiki/development.ini
28
29[Install]
30WantedBy=multi-user.target

备注

为了使用 --ini-paste-logged parameter (and have logs from an application), PasteScript 是必需的。To install, run:

pip install PasteScript

uwsgi可以配置在 .ini 文件,例如:

1# development.ini
2# ...
3
4[uwsgi]
5socket = /tmp/pyramid.sock
6chmod-socket = 666
7protocol = http

保存文件并运行以下命令以启动进程:

systemctl enable pyramid.service
systemctl start pyramid.service

Verify that the file /tmp/pyramid.sock 创建。

Here are a few useful commands:

systemctl restart pyramid.service # restarts app
journalctl -fu pyramid.service # tail logs

接下来,我们需要在nginx中配置一个虚拟主机。下面是一个配置示例:

 1# myapp.conf
 2
 3upstream pyramid {
 4    server unix:///tmp/pyramid.sock;
 5}
 6
 7server {
 8    listen 80;
 9
10    # optional ssl configuration
11
12    listen 443 ssl;
13    ssl_certificate /path/to/ssl/pem_file;
14    ssl_certificate_key /path/to/ssl/certificate_key;
15
16    # end of optional ssl configuration
17
18    server_name  example.com;
19
20    access_log  /opt/env/access.log;
21
22    location / {
23        proxy_set_header        Host $http_host;
24        proxy_set_header        X-Real-IP $remote_addr;
25        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
26        proxy_set_header        X-Forwarded-Proto $scheme;
27
28        client_max_body_size    10m;
29        client_body_buffer_size 128k;
30        proxy_connect_timeout   60s;
31        proxy_send_timeout      90s;
32        proxy_read_timeout      90s;
33        proxy_buffering         off;
34        proxy_temp_file_write_size 64k;
35        proxy_pass http://pyramid;
36        proxy_redirect          off;
37    }
38}

A better explanation for some of the above nginx directives can be found in the cookbook recipe nginx + pserve + supervisord .