nginx 在 ubuntu 18.04 LTS 上对 运行 django 的 uwsgi 给出 502 错误
nginx gives 502 error with uwsgi for running django on ubuntu 18.04 LTS
注意:我是 django 及其部署的新手。
根据this guide中提到的步骤通过uwsgi和nginx部署了django - 除了emperor-vassal配置并且没有任何虚拟环境。
Side note: The site comes up using python3 manage.py 0.0.0.0:8800
但是,似乎 nginx 在套接字中面临权限问题,并在浏览器中给出 502 错误网关错误。
nginx错误日志显示如下错误:
2020/07/08 21:05:40 [crit] 3943#3943: *3 connect() to unix:///home/ubuntu/deploymenttst/MySite/MySite.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.12.12, server: 192.168.12.12, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///home/ubuntu/deploymenttst/MySite/MySite.sock:", host: "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
在其中一个终端 windows 中,uwsgi 进程使用以下命令成功启动:
uwsgi --ini MySite_uwsgi.ini
nginx 已在每次配置更改时停止、启动和重新启动多次,否则也是如此。
MySite项目目录的uid:gid已经设置为www-data:www:data using sudo chown -R www-data:www:data *
为什么我仍然收到坏网关 502 错误,其中由于权限问题无法联系上游 django 应用程序?
通过将项目放置到 /tmp 目录解决了这个问题。
来自 www-data 用户的 运行nginx 无法访问内部目录 MySite,因此无法访问套接字或放置在那里的文件,尽管已分配给用户 www-data。
现在,我的另一个问题是关于 nginx 权限问题的原因,尽管向 www-data 提供了目录的 uid 和 gid,但可能是什么问题?
Note: My user named ubuntu is a sudoer.
注意:我是 django 及其部署的新手。
根据this guide中提到的步骤通过uwsgi和nginx部署了django - 除了emperor-vassal配置并且没有任何虚拟环境。
Side note: The site comes up using
python3 manage.py 0.0.0.0:8800
但是,似乎 nginx 在套接字中面临权限问题,并在浏览器中给出 502 错误网关错误。
nginx错误日志显示如下错误:
2020/07/08 21:05:40 [crit] 3943#3943: *3 connect() to unix:///home/ubuntu/deploymenttst/MySite/MySite.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.12.12, server: 192.168.12.12, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///home/ubuntu/deploymenttst/MySite/MySite.sock:", host: "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
在其中一个终端 windows 中,uwsgi 进程使用以下命令成功启动:
uwsgi --ini MySite_uwsgi.ini
nginx 已在每次配置更改时停止、启动和重新启动多次,否则也是如此。
MySite项目目录的uid:gid已经设置为www-data:www:data using
sudo chown -R www-data:www:data *
为什么我仍然收到坏网关 502 错误,其中由于权限问题无法联系上游 django 应用程序?
通过将项目放置到 /tmp 目录解决了这个问题。
来自 www-data 用户的 运行nginx 无法访问内部目录 MySite,因此无法访问套接字或放置在那里的文件,尽管已分配给用户 www-data。
现在,我的另一个问题是关于 nginx 权限问题的原因,尽管向 www-data 提供了目录的 uid 和 gid,但可能是什么问题?
Note: My user named ubuntu is a sudoer.