uWSGI + nginx + systemd

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

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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# /etc/systemd/system/pyramid.service

[Unit]
Description=pyramid app

# Requirements
Requires=network.target

# Dependency ordering
After=network.target

[Service]
TimeoutStartSec=0
RestartSec=10
Restart=always

# path to app
WorkingDirectory=/opt/env/wiki
# the user that you want to run app by
User=app

KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

# Main process
ExecStart=/opt/env/bin/uwsgi --ini-paste-logged /opt/env/wiki/development.ini

[Install]
WantedBy=multi-user.target

注解

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

pip install PasteScript

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

1
2
3
4
5
6
7
# development.ini
# ...

[uwsgi]
socket = /tmp/pyramid.sock
chmod-socket = 666
protocol = 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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# myapp.conf

upstream pyramid {
    server unix:///tmp/pyramid.sock;
}

server {
    listen 80;

    # optional ssl configuration

    listen 443 ssl;
    ssl_certificate /path/to/ssl/pem_file;
    ssl_certificate_key /path/to/ssl/certificate_key;

    # end of optional ssl configuration

    server_name  example.com;

    access_log  /opt/env/access.log;

    location / {
        proxy_set_header        Host $http_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;

        client_max_body_size    10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout   60s;
        proxy_send_timeout      90s;
        proxy_read_timeout      90s;
        proxy_buffering         off;
        proxy_temp_file_write_size 64k;
        proxy_pass http://pyramid;
        proxy_redirect          off;
    }
}

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