我有一个基于Django框架的网站。我通过Nginx webserver(uWSGI,Django,Nginx)运行网站。我想用Accept-Ranges标题在我的网站上传输mp3文件。我想用Nginx提供我的mp3文件。我需要我的API看起来像这样
http://192.168.1.105/stream/rihanna
这必须返回部分下载的mp3文件(Accept-Ranges)。 我的mp3文件存储在:/ home / docker / code / app / media / data / 当我使用这些配置运行服务器并浏览到192.168.1.105/stream/rihanna时,Django返回404。
我的Nginx conf:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:/home/docker/code/app.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on, default_server indicates that this server block
# is the block to use if no blocks match the server_name
listen 80 default;
include /etc/nginx/mime.types;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
autoindex on;
sendfile on;
sendfile_max_chunk 1024m;
internal;
#add_header X-Static hit;
alias /home/docker/code/app/media/; # your Django project's media files - amend as required
}
location /static {
alias /home/docker/code/app/static/; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/docker/code/uwsgi_params; # the uwsgi_params file you installed
}
}
我的views.py:
def stream(request, offset):
try:
mp3_path = os.getcwd() + '/media/data/' + offset + '.mp3'
mp3_data = open(mp3_path, "r").read()
except:
raise Http404()
response = HttpResponse(mp3_data, content_type="audio/mpeg", status=206)
response['X-Accel-Redirect'] = mp3_path
response['X-Accel-Buffering'] = 'no'
response['Content-Length'] = os.path.getsize(mp3_path)
response['Content-Dispostion'] = "attachment; filename=" + mp3_path
response['Accept-Ranges'] = 'bytes'
我希望Nginx提供此文件。我真的需要启用Accept-Ranges。
我的设置.py:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
DEBUG = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '192.168.1.105', '0.0.0.0']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'beats.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'beats.wsgi.application'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_URL = os.path.join(BASE_DIR, 'media/')
# Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
我的问题是:它不起作用,Django返回404错误网页。
答案 0 :(得分:1)
首先,我没有将Nginx配置文件复制到可用站点路径。因此,它没有用。
在我的views.py中:
response = HttpResponse(mp3_data, content_type="audio/mpeg", status=206)
response['X-Accel-Redirect'] = mp3_path
这两行必须更改为:
response= HttpResponse('', content_type="audio/mpeg", status=206)
response['X-Accel-Redirect'] = '/media/data/' + file_name + '.mp3'