NGINX 无法正确提供图像文件

NGINX 无法正确提供图像文件

我已经建立了一个由 Nginx 提供服务的 django 网站,一切都运行正常,直到最近图像停止显示。

我尝试使用检查这种奇怪发展的可能原因curl,然后意识到 Content-Type 未被识别为Content-Type: image/jpeg返回Content-Type: text/html; charset=utf-8

这种行为看起来很奇怪,因为我已经包括mime 类型在我的nginx.confcurl文件。下面是命令的示例响应

user@server:~$ curl -I https://domain.name/media/upload/image.jpg
HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Sun, 29 May 2022 00:45:53 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 11392
Connection: keep-alive
X-Frame-Options: DENY
Vary: Cookie
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
Set-Cookie: csrftoken=T9Z3jrp4dzOAINxo6JzOUyjIGwGYHoc37TZaYsIOmHHyrQUw30vI6ETIAcy66Wnr; expires=Sun, 28 May 2023 00:45:53 GMT; Max-Age=31449600; Path=/; SameSite=Lax

这是我的完整nginx.conf文件

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

内容/etc/nginx/sites-enabled/app

# /etc/nginx/sites-enabled

server {
    server_name my_server_IP my_server_NAME;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/user/app;
    }

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

    listen 443 ssl; 
    ssl_certificate /path/to/certfullchain.pem; 
    ssl_certificate_key /path/to/certprivkey.pem; 
    include /etc/cert-provider/options-ssl-nginx.conf; 
    ssl_dhparam /etc/cerrt-provider/ssl-dhparams.pem; 






}

server {
    if ($host = www.domain.name {
        return 301 https://$host$request_uri;
    } 


    if ($host = domain.name) {
        return 301 https://$host$request_uri;
    } 


    listen 80;
    server_name my_server_IP my_server_NAME;
    return 404; 




}

笔记:我正在使用 gunicorn 为该网站提供服务

答案1

我可以通过添加与我的媒体文件匹配的新位置指令来解决这个问题。在这种情况下,我的文件被上传到媒体

我通过将以下内容添加到我的服务器块来修复此问题

  location /media/ {
        root /home/user/app;
    }

现在我的新/etc/nginx/sites-enabled/app面貌是这样的。

# /etc/nginx/sites-enabled

server {
    server_name my_server_IP my_server_NAME;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/user/app;
    }
    location /media/ {
        root /home/user/app;
    }

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

    listen 443 ssl; 
    ssl_certificate /path/to/certfullchain.pem; 
    ssl_certificate_key /path/to/certprivkey.pem; 
    include /etc/cert-provider/options-ssl-nginx.conf; 
    ssl_dhparam /etc/cerrt-provider/ssl-dhparams.pem; 






}

server {
    if ($host = www.domain.name {
        return 301 https://$host$request_uri;
    } 


    if ($host = domain.name) {
        return 301 https://$host$request_uri;
    } 


    listen 80;
    server_name my_server_IP my_server_NAME;
    return 404; 




}

相关内容