我正在使用gunicorn和Nginx将传入请求路由到我的Django Rest Framework API.
gunicorn在端口8001上运行,Nginx在端口8000上运行.根据以下配置文件,Nginx配置为将请求转发到gunicorn:
server {
listen 8000;
server_name ec2-ww.xx.yy.zz.compute-1.amazonaws.com; # public address of my server,redacted for stack overflow
access_log /vol/logs/ftv.access.log;
location ^~ /static/ {
alias /vol/server/ftv/static/;
autoindex on;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
#access_log off;
expires 30d;
}
location / {
proxy_pass_header Server;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_set_header Host $host:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://ec2-ww.xx.yy.zz.compute-1.amazonaws.com:8001/;
}
}
我遇到的问题是DRF的HyperlinkedModelSerializer返回指向8001端口而不是8000端口的url.这是(我想)因为Django Rest Framework不了解gunicorn或Nginx,只是看到请求从端口8001进入,因此它基于该端口形成了URL超链接.
我必须在我的Nginx conf文件或settings.py(或两者)中缺少某些配置选项,但是(对我而言)令人惊讶的是,以前没有有人问过/回答过这个问题-我一直在搜索.非常感谢DRF或Nginx专家的任何帮助!
最佳答案