Django 网站卡在默认的 Nginx 欢迎页面

问题描述 投票:0回答:1

所以我尝试在 Linode 上托管我客户的网站,并使用 django 制作了该网站。我使用 Nginx 和 Gunicorn 进行托管,但卡在默认的 Nginx 页面中。以下是我的 Nginx 配置:


server {
    listen 80;
    server_name my.ip.address scopeplus.org www.scopeplus.org;
    error_log /var/log/nginx/error.log debug;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /root/scope_website/scope;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

以下是我的gunicorn.service文件:


[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/root/scope_website
ExecStart=/root/venvPath/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          scope_website.wsgi:application

[Install]
WantedBy=multi-user.target

以下是我的gunicorn.socket文件:


[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

我的settings.py文件位于以下路径:

root@localhost:~/scope_website/scope_website# pwd
/root/scope_website/scope_website
root@localhost:~/scope_website/scope_website# ls
asgi.py  __init__.py  __pycache__  settings.py  urls.py  wsgi.py

以下是Ubuntu终端的journalctl输出:


Jan 20 14:24:33 localhost gunicorn[7619]: Forbidden (CSRF cookie not set.): /
Jan 20 14:24:33 localhost gunicorn[7619]:  - - [20/Jan/2024:14:24:33 +0000] "POST / HTTP/1.0" 403 2869 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
Jan 20 14:45:50 localhost gunicorn[7617]:  - - [20/Jan/2024:14:45:50 +0000] "GET / HTTP/1.0" 200 8692 "-" "Mozilla/5.0 zgrab/0.x"
Jan 20 14:46:09 localhost gunicorn[7617]:  - - [20/Jan/2024:14:46:09 +0000] "GET / HTTP/1.0" 200 8692 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
Jan 20 15:03:38 localhost gunicorn[7618]: Not Found: /.env
Jan 20 15:03:38 localhost gunicorn[7618]:  - - [20/Jan/2024:15:03:38 +0000] "GET /.env HTTP/1.0" 404 2810 "-" "Mozilla/5.0 (Linux; U; Android 4.4.2; en-US; HM NOTE 1W Build/KOT49H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 UCBrowser/11.0.5.850 U3/0.8.0 Mobile Safari/534.30"
Jan 20 15:03:38 localhost gunicorn[7619]: Forbidden (CSRF cookie not set.): /
Jan 20 15:03:38 localhost gunicorn[7619]:  - - [20/Jan/2024:15:03:38 +0000] "POST / HTTP/1.0" 403 2869 "-" "Mozilla/5.0 (Linux; U; Android 4.4.2; en-US; HM NOTE 1W Build/KOT49H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 UCBrowser/11.0.5.850 U3/0.8.0 Mobile Safari/534.30"
Jan 20 15:08:22 localhost gunicorn[7617]:  - - [20/Jan/2024:15:08:22 +0000] "GET / HTTP/1.0" 200 8692 "-" "Mozilla/5.0 zgrab/0.x"
Jan 20 15:08:32 localhost gunicorn[7619]:  - - [20/Jan/2024:15:08:32 +0000] "GET / HTTP/1.0" 200 8692 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
Jan 20 15:16:19 localhost gunicorn[7618]: Not Found: /.env
Jan 20 15:16:19 localhost gunicorn[7618]:  - - [20/Jan/2024:15:16:19 +0000] "GET /.env HTTP/1.0" 404 2810 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
Jan 20 15:16:20 localhost gunicorn[7619]: Forbidden (CSRF cookie not set.): /
Jan 20 15:16:20 localhost gunicorn[7619]:  - - [20/Jan/2024:15:16:20 +0000] "POST / HTTP/1.0" 403 2869 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
Jan 20 15:18:54 localhost gunicorn[7619]:  - - [20/Jan/2024:15:18:54 +0000] "GET / HTTP/1.0" 200 8692 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Mobile/15E148 Safari/604.1"
Jan 20 15:31:10 localhost gunicorn[7618]:  - - [20/Jan/2024:15:31:10 +0000] "GET / HTTP/1.0" 200 8692 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"
Jan 20 15:31:30 localhost gunicorn[7618]: Not Found: /robots.txt
Jan 20 15:31:30 localhost gunicorn[7618]:  - - [20/Jan/2024:15:31:30 +0000] "GET /robots.txt HTTP/1.0" 404 2828 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
Jan 20 15:31:31 localhost gunicorn[7618]: Not Found: /sitemap.xml
Jan 20 15:31:31 localhost gunicorn[7618]:  - - [20/Jan/2024:15:31:31 +0000] "GET /sitemap.xml HTTP/1.0" 404 2831 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
Jan 20 15:38:30 localhost gunicorn[7619]:  - - [20/Jan/2024:15:38:30 +0000] "GET / HTTP/1.0" 200 8692 "-" "Mozilla/5.0 zgrab/0.x"
Jan 20 15:38:48 localhost gunicorn[7617]:  - - [20/Jan/2024:15:38:48 +0000] "GET / HTTP/1.0" 200 8692 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"

对于信息过多,我深表歉意,只是因为我是网络开发新手,所以这对我来说是新事物。我对我的域进行了快速 nslookup,结果是在终端中打印出正确的 IP 地址。我该如何继续?

另外,我已经允许端口 80 上的流量,并且我的 Nginx 错误日志为空

python django nginx gunicorn web-hosting
1个回答
0
投票

1。 Nginx 配置:

  • 确保 Nginx 配置中的 root 指令指向 Django 项目静态文件的正确位置 - 常见结构是
    /path/to/your/django/project/static
    ,但这取决于您的项目结构。
  • 运行
    python manage.py collectstatic
    后,您应该手动检查
    /root/scope_website/scope/static
    目录以确保您的静态文件在那里。
  • 有时,默认的 Nginx 服务器块会干扰,只需检查
    /etc/nginx/sites-enabled/
    /etc/nginx/sites-available/
    中是否有默认文件,然后考虑删除或修改它。

2。 Gunicorn 和 Socket 配置:

  • 您的套接字文件应该仔细检查,您的 Gunicorn 套接字文件是否已生成(
    /run/gunicorn.sock
    )?使用
    ls -l /run/gunicorn.sock
    查看文件是否存在并具有正确的权限。
  • 当您实际尝试执行服务请求时,请查看 Gunicorn 的日志以查找任何设置错误。

3. Django 配置:

  • 通过在
    DEBUG = True
    中设置
    settings.py
    来暂时启用 Django 的调试模式以获取详细的错误消息,因为它对于生产环境来说是不安全的
  • 您应该进行中间件检查(对于 CSRF 处理,确保其全部包含在
    settings.py
    中)
  • 检查您的 Django 应用程序是否可以连接到其数据库。

安全考虑: 用户权限:出于安全原因,不建议以 root 身份运行 Gunicorn。考虑创建一个新用户来运行您的应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.