【问题标题】:Nginx trys to download file instead of displayingNginx 尝试下载文件而不是显示
【发布时间】:2013-04-20 04:34:00
【问题描述】:

我安装了 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;
#            }
    }

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/*;
}

【问题讨论】:

  • 好吧,现在我更困惑了。我在 AWS 上运行一个实例。我关闭了实例,我仍然可以下载文件。所以,这不应该发生。

标签: nginx amazon-web-services amazon-ec2 php


【解决方案1】:

删除default_type application/octet-stream;。这行代码让浏览器认为它是一些二进制数据而不是 HTML。

【讨论】:

  • 我在注释掉default_type后仍然有同样的问题,还有其他建议吗?
  • 在我的例子中,问题是没有安装 php。安装后,我必须让 php-fpm 监听正确的主机/端口。在 /etc/php5/fpm/pool.d/www.conf 中将以下行从:listen = /var/run/php5-fpm.sock 更改为:listen = 127.0.0.1:9000 使用 sudo /etc 重新启动 php-fpm /init.d/php5-fpm 重启,一切正常。祝你好运,并查看此链接以获取更多帮助 wildlyinaccurate.com/solving-502-bad-gateway-with-nginx-php-fpm
  • @ArminNehzat Nginx 提供文件而不是 php-fpm,这就是它被下载的原因。找出为什么文件是直接使用 Nginx 而不是通过 php-fpm 发送的。
  • 这样修复会带来很大的安全风险。为什么你认为这个“默认”被实施了?为每个 mime 定义行为,而不是在全局级别删除默认行为。
【解决方案2】:

正如其他人所指出的,这是给您带来麻烦的一行:

default_type application/octet-stream;

这一行覆盖它之前的行,并告诉 Nginx 将任何响应作为通用二进制数据发送。而且,由于浏览器不能对二进制数据做任何特定的事情,它们只是将其作为文件下载。


删除或评论该行以及上面的行:

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

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

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


这里有一个更好的解决方案:

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

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

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

最后,在nginx.conf 更改

default_type application/octet-stream;

default_type text/html;

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


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

types {
    audio/mpeg mp3;
}

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

types {
    application/octet-stream mp3;
}

【讨论】:

    【解决方案3】:

    还可以尝试清除 that site 的缓存。我在 Firefox 中遇到了这个问题,但回滚到旧配置不起作用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-26
      • 2023-03-25
      • 2011-04-11
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多