Nginx 托管 uwsgi django 应用程序:只能通过 curl 访问,不能通过浏览器访问
Nginx hosted uwsgi django app: only accessible via curl, not by browser
我正在使用 uwsgi 和 nginx 托管 Django 应用程序。
如果我调用 curl -v my_ip:port
它会连接。如果我将浏览器指向 my_ip:port
它 returns 一个 ERR_CONNECTION_REFUSED
并且连接不会显示在日志中。
uWSGI:
文件:data_collection_project.ini
[uwsgi]
project = data_collection_project
base = /data_nfs/data_collection_project
chdir = %(base)
# /%(project)
home = /home/rootadmin/.virtualenvs/data_collection
#plugins = python
#module = data_collection_project.wsgi:application
module = data_collection_project.wsgi:application
master = true
processes = 2
socket = %(base)/%(project).sock
chmod-socket = 666
nginx:
文件:/etc/nginx/sites-available
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///data_nfs/data_collection_project/data_collection_project.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
listen 8433;
# the domain name it will serve for
server_name my_ip; # 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 /data_nfs/data_collection_project/media; # your Django project's media files - amend as requir$ }
location /static {
alias /data_nfs/data_collection_project/static; # your Django project's static files - amend as requi$ }
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /data_nfs/data_collection_project/uwsgi_params; # the uwsgi_params file you installed
}
}
据我所知,这应该可以让外部浏览器通过 my_ip
访问该网站,但事实并非如此。我不明白为什么。我错过了什么?
编辑: 如果我关闭 uwsgi
然后尝试调用 ip:port
,则会在 /var/log/nginx/error.log
connect () to unix:///...sock failed. (111:connection refused)
这个错误是有道理的,因为 uwsgi
没有处理套接字。这似乎意味着 nginx
正在工作,并且在我执行 curl my_ip:port
?
时确实将请求传递给套接字
我不是 100% 确定,但根据您的编辑,nginx 似乎正确通过。也许是 Django 的 ALLOWED_HOSTS
?我会按照以下步骤操作:
- 确保
data_collection_project.ini
是使用的 ini(可以尝试命令行参数而不是 ini 文件作为测试之一)。
- 将
deamonize
添加到您的 data_collection_project.ini
以使 uwsgi 记录到文件。
- 检查是否不是 Django 导致您的连接中断。
我正在使用 uwsgi 和 nginx 托管 Django 应用程序。
如果我调用 curl -v my_ip:port
它会连接。如果我将浏览器指向 my_ip:port
它 returns 一个 ERR_CONNECTION_REFUSED
并且连接不会显示在日志中。
uWSGI:
文件:data_collection_project.ini
[uwsgi]
project = data_collection_project
base = /data_nfs/data_collection_project
chdir = %(base)
# /%(project)
home = /home/rootadmin/.virtualenvs/data_collection
#plugins = python
#module = data_collection_project.wsgi:application
module = data_collection_project.wsgi:application
master = true
processes = 2
socket = %(base)/%(project).sock
chmod-socket = 666
nginx:
文件:/etc/nginx/sites-available
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///data_nfs/data_collection_project/data_collection_project.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
listen 8433;
# the domain name it will serve for
server_name my_ip; # 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 /data_nfs/data_collection_project/media; # your Django project's media files - amend as requir$ }
location /static {
alias /data_nfs/data_collection_project/static; # your Django project's static files - amend as requi$ }
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /data_nfs/data_collection_project/uwsgi_params; # the uwsgi_params file you installed
}
}
据我所知,这应该可以让外部浏览器通过 my_ip
访问该网站,但事实并非如此。我不明白为什么。我错过了什么?
编辑: 如果我关闭 uwsgi
然后尝试调用 ip:port
,则会在 /var/log/nginx/error.log
connect () to unix:///...sock failed. (111:connection refused)
这个错误是有道理的,因为 uwsgi
没有处理套接字。这似乎意味着 nginx
正在工作,并且在我执行 curl my_ip:port
?
我不是 100% 确定,但根据您的编辑,nginx 似乎正确通过。也许是 Django 的 ALLOWED_HOSTS
?我会按照以下步骤操作:
- 确保
data_collection_project.ini
是使用的 ini(可以尝试命令行参数而不是 ini 文件作为测试之一)。 - 将
deamonize
添加到您的data_collection_project.ini
以使 uwsgi 记录到文件。 - 检查是否不是 Django 导致您的连接中断。