在主管中运行uwsgi出错了,但在命令行中运行良好

时间:2017-07-08 06:11:36

标签: uwsgi supervisord

我在supervisord下运行uwsgi时遇到了一些问题。

主要的问题是,使用命令行,我运行uwsgi --ini /home/satori/mysite/src/mysite_uwsgi.ini,一切顺利,但是当我在supervisorctl下运行它时,当我上传一个非ascii字符名称的文件时,服务器返回500。

不会发生如果我在命令行中运行,则文件已正确上传。

我认为问题与我的主管配置文件有关,可能是一些环境问题。我的配置文件位于/etc/supervisord.conf

这是我的mysite_uwsgi.ini文件,它位于/home/satori/mysite/src/mysite_uwsgi.ini

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /home/satori/mysite/src
# Django's wsgi file
module          = mysite.wsgi
# the virtualenv (full path)
#home            = /home/ubuntu/mysite/bin

#plugins         = python34

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /home/satori/mysite/src/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

这是supervisord.conf中的相关设置

[program:mysite]
command=uwsgi --ini /home/satori/mysite/src/mysite_uwsgi.ini
autostart=true
autorestart=true
user=root
log_stderr=true
stdout_logfile=/var/log/mysite.log

这是500错误:

UnicodeEncodeError at /upload/view/
'ascii' codec can't encode characters in position 37-38: ordinal not in range(128)

models.py

# encoding: utf-8
import os
from django.db import models
from django.conf import settings


def get_upload_path(instance, filename):
    filename = ''.join([ch for ch in filename if ord(ch) < 128])
    return os.path.join("user_%d" % instance.owner.id, filename)


class Picture(models.Model):
    """This is a small demo using just two fields. The slug field is really not
    necessary, but makes the code simpler. ImageField depends on PIL or
    pillow (where Pillow is easily installable in a virtualenv. If you have
    problems installing pillow, use a more generic FileField instead.
    """
    file = models.ImageField(upload_to=get_upload_path)
    slug = models.SlugField(max_length=50, blank=True)
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

    def __str__(self):
        return self.file.name

    @models.permalink
    def get_absolute_url(self):
        return ('upload-new', )

    def save(self, *args, **kwargs):
        self.slug = self.file.name
        super(Picture, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        """delete -- Remove to leave file."""
        self.file.delete(False)
        super(Picture, self).delete(*args, **kwargs)

1 个答案:

答案 0 :(得分:0)

您应该将此添加到您的uwsgi.ini文件

  

env = PYTHONIOENCODING = UTF-8

此外,不确定这是否是您所做的,但我使用 str 而不是 unicode 。我使用的是一个需要 str 的插件,在这种情况下你可以写:

def __str__(self):
    return YourModel.__unicode__(self)