Nginx试图下载文件而不是显示

moo*_*e18 7 php nginx amazon-ec2 amazon-web-services

我安装Nginx并有一个子域和域.子域名有php5-fpm和wordpress.它工作正常,并在一个站点 - 可用文件符号链接到启用站点.该域没有php,并且还有一个符号链接的文件.即使在我转到域后重新启动服务器,它也会尝试下载html文件.这是我的网站的可用页面:

       server {
        server_name www.example.us;
        rewrite ^(.*) http://example.us$1 permanent;
    }

    server {
            listen 80;
            server_name example.us;
                    root /var/www/example;
            index index.php;
            autoindex on;
            autoindex_exact_size off;
            include /etc/nginx/security;
    # Logging --
    access_log /var/log/nginx/example.us.access.log; 
    error_log /var/log/nginx/example.us.error.log notice;
            # serve static files directly
            location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
                access_log off;
                expires max;
            }

#            location ~ \.php$ {
#           try_files $uri =404;
#                    fastcgi_pass unix:/var/run/php5-fpm/example.us.socket;
#                    fastcgi_index index.php;
#                    include /etc/nginx/fastcgi_params;
#            }
    }
Run Code Online (Sandbox Code Playgroud)

nginx.conf文件是:

user www-data; 
worker_processes 4; 
pid /var/run/nginx.pid; 

events {
    worker_connections 768;
    # multi_accept on;
}
http {
# Basic Settings
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    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;
# Logging Settings
log_format gzip '$remote_addr - $remote_user [$time_local] '
                '"$request" $status $bytes_sent '
                '"$http_referer" "$http_user_agent" "$gzip_ratio"';
    access_log /var/log/nginx/access.log gzip buffer=32k;
    error_log /var/log/nginx/error.log notice;
# Gzip Settings
    gzip on;
    gzip_disable "msie6";
    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/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Virtual Host Configs
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
Run Code Online (Sandbox Code Playgroud)

Bog*_*tov 6

正如其他人指出的那样,这是导致您遇到麻烦的行:

default_type application/octet-stream;
Run Code Online (Sandbox Code Playgroud)

该行覆盖之前的行,并告诉 Nginx 将任何响应作为通用二进制数据发送。而且,由于浏览器无法对二进制数据执行任何特定操作,因此它们只是将其下载为文件。


删除或注释该行以及上面的行:

#include /etc/nginx/mime.types;
#default_type application/octet-stream;
Run Code Online (Sandbox Code Playgroud)

应该可以工作,因为默认的 Nginx 行为是发送所有内容,text/plain并且浏览器足够智能,可以通过这种方式处理 HTML/CSS/JS。

尽管如此,我不会推荐这样的解决方案,因为您可能需要从服务器提供不同类型的内容,即图片、mp3、二进制文件等。


这是一个更好的解决方案:

首先,检查文件/etc/nginx/mime.types;是否确实存在并且可供运行 nginx 的用户读取。

然后,检查该文件是否确实包含types { ... }声明。该文件随 Nginx 一起打包,并包含 mime 类型到文件扩展名映射的合理默认值。如果文件有问题,您可以从以下要点复制粘贴其内容:

https://gist.github.com/kondratovbr/dd34feba1d63bd468ded4ee70e59ea07

最后,在nginx.conf改变中

default_type application/octet-stream;
Run Code Online (Sandbox Code Playgroud)

default_type text/html;
Run Code Online (Sandbox Code Playgroud)

现在,Nginx 将根据文件扩展名提供每种类型的内容。如果没有提供,某些后端系统/应用程序就是这种情况, - 将尝试提供 HTML。


请注意,您可能想要更改其中一些默认值。例如,使用该设置:

types {
    audio/mpeg mp3;
}
Run Code Online (Sandbox Code Playgroud)

浏览器通常会尝试播放歌曲而不是直接下载它。如果需要,可以通过声明来更改:

types {
    application/octet-stream mp3;
}
Run Code Online (Sandbox Code Playgroud)


Gus*_*tav 5

删除default_type application/octet-stream;.这条线使浏览器认为它是一些二进制数据而不是HTML.

  • @ArminNehzat Nginx提供文件而不是php-fpm,这就是它下载的原因.找出文件直接与Nginx一起发送的原因,而不是通过php-fpm. (3认同)
  • 注释掉 default_type 后我仍然遇到同样的问题,还有其他建议吗? (2认同)
  • 像这样修复它会产生很大的安全风险。为什么你认为这个“默认”被实施了?为每个 mime 定义行为,而不是删除全局级别的默认行为。 (2认同)