注意:我是django及其部署的新手。
根据this guide中提到的步骤,通过uwsgi和nginx部署了django-除了皇帝从属配置之外,没有任何虚拟环境。
旁注:该网站使用
python3 manage.py 0.0.0.0:8800
但是,看来nginx面临套接字中的权限问题,并在浏览器中出现502错误的网关错误。
nginx错误日志显示以下错误:
2020/07/08 21:05:40 [crit] 3943#3943:* 3 connect()到unix:///home/ubuntu/deploymenttst/MySite/MySite.sock失败(13:权限被拒绝)连接到上游,客户端:192.168.12.12,服务器:192.168.12.12,请求:“ GET / HTTP / 1.1”,上游:“ uwsgi:// unix:///home/ubuntu/deploymenttst/MySite/MySite.sock: ”,主机:“ 192.168.12.12:8400”
配置如下:
在项目的settings.py文件中,配置设置为(默认wsgi除外):
DEBUG = False
ALLOWED_HOSTS = [ "192.168.12.12" ]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
/etc/nginx/sites-available/MySite_nginx.conf
中的MySite_nginx.conf文件具有以下配置条目:
# MySite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///home/ubuntu/deploymenttst/MySite/MySite.sock; # for a file socket
#server 127.0.0.1:8008;
}
# configuration of the server
server {
# the port your site will be served on
listen 8400;
# the domain name it will serve for
server_name 192.168.12.12; # 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 {
alias /home/ubuntu/deploymenttst/MySite/media; # your Django project's media files - amend as required
}
location /static {
#alias /home/ubuntu/deploymenttst/MySite/main/static; # your Django project's static files - amend as required
#alias /home/ubuntu/deploymenttst/MySite/register/static; # your Django project's static files - amend as required
alias /home/ubuntu/deploymenttst/MySite/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/ubuntu/deploymenttst/MySite/uwsgi_params; # the uwsgi_params file you installed
}
}
它已符号链接到/etc/nginx/sites-enabled/MySite_nginx.conf。
uwsgi_params文件在包含以下条目的项目目录中创建:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
MySite_uwsgi.ini文件中的内容如下:
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/ubuntu/deploymenttst/MySite
# Django's wsgi file
module = MySite.wsgi
# the virtualenv (full path)
#home = /home/ubuntu/deploymenttst/MySite/
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 20
# the socket (use the full path to be safe
socket = /home/ubuntu/deploymenttst/MySite/MySite.sock
# ... with appropriate permissions - may be needed
chmod-socket = 666
uid = www-data
gid = www-data
# clear environment on exit
vacuum = true
静态文件已使用以下命令收集在项目目录中的静态目录中:
python3 manage.py collectstatic
在一个终端窗口中,使用以下命令成功启动uwsgi进程:
uwsgi --ini MySite_uwsgi.ini
nginx已在每次更改配置后停止,启动和重新启动多次。
MySite项目目录的uid:gid已使用sudo chown -R www-data:www:data *
设置为www-data:www:data
为什么我仍然遇到网关502错误,由于权限问题而无法联系上游Django应用?
答案 0 :(得分:0)
通过将项目放置到/ tmp目录解决了该问题。
从www-data用户运行的nginx尽管已分配给用户www-data,但无法访问内部目录MySite,因此也无法访问套接字或放置在其中的文件。
现在,我的另一个问题是关于nginx权限问题的原因,尽管向www-data提供了目录的uid和gid,但可能是什么问题?
注意:我的用户 ubuntu 是一个sudoer。