【问题标题】:Nginx reverse proxy to django application, images styles are not showingNginx 反向代理到 django 应用程序,图像样式未显示
【发布时间】:2021-05-11 03:38:44
【问题描述】:

您好,我有一个完整的 django 应用程序,我将使用 gunicorn 和 nginx 在 aws ec2 上托管它。但是当我使用 gunicorn 或 python3 运行应用程序时,我的应用程序会运行并显示所有带有 css 和样式的图像。但是,每当我尝试通过 proxy_pass 为我的 django 应用程序提供 nginx 时,图像或样式或静态文件夹上的任何内容都无法访问。我的错误日志显示权限问题。但是我已经将应用程序设置为用户和组 nginx。但什么都没有显示。

  • nginx.conf 上的 nginx 配置
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;

    server {
    listen 80;
    server_name example.com;

    root /home/ec2-user/sites/softopark-django-cms;
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /usr/share/nginx/html/softopark-django-cms/static/;
    }
    location /media/  {
        root /usr/share/nginx/html/softopark-django-cms/media/;
    }
    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://0.0.0.0:8000/;
    }

    }
  • 我的 gunicorn 服务文件
[Unit]
Description=test
After=network.target

[Service]
User=nginx
Group=nginx
WorkingDirectory=/usr/share/nginx/html/softopark-django-cms
Environment="/home/ec2-user/Env/cms5-XkwMz5k7/bin"
ExecStart=/home/ec2-user/Env/cms5-XkwMz5k7/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 cms_project.wsgi

[Install]
WantedBy=multi-user.target
  • 谁能帮我解决这个问题?

我需要在 aws 上托管我的 django 应用程序,但使用 nginx 我无法做到这一点。请帮忙。

【问题讨论】:

标签: django amazon-web-services nginx


【解决方案1】:

好吧,我没有找到任何具体的答案,但我在运行 collectstatic 命令后修复了它。
有一件事,我错过了每当我们创建任何 Django 项目并在部署到另一个地方时,我们需要运行 python manage.py collectstatic 以便我们的应用程序可以检测静态文件并更新它们的路径。所以我更新了代码库并将其部署在服务器上,但没有使用 collectstatic,所以我看不到这些图像。
python manage.py collectstatic 完成了这项工作。

【讨论】:

    猜你喜欢
    • 2019-08-09
    • 2021-02-04
    • 2021-05-22
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    相关资源
    最近更新 更多