nginx 下载文件的静态位置而不是显示响应

lec*_*rio 2 nginx

到目前为止,我已经多次阅读过有关 php 的这个问题。我正在尝试了解 nginx 的基础知识,因为我几乎只使用 apache 或 iis。

我正在运行一个小型 debian 9 服务器并尝试了解 nginx 配置的基础知识。

我基本上没有碰过nginx.conf文件。

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

events {
    worker_connections 768;
}

http {

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

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

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

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

    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
Run Code Online (Sandbox Code Playgroud)

然而 default.conf 文件是由我编辑的

server {

    listen 80 default_server;
    listen [::]:80 default_server;

    server_name 10.20.30.1;

    root /var/www;

    index index.html index.htm index.nginx-debian.html;

}
Run Code Online (Sandbox Code Playgroud)

通过 IP 访问时,我的/var/www文件夹中的所有内容都正确加载了 css 文件。所以包含哑剧类型效果很好。

添加后:

    location /greet {
          return 200 "Hello User!";
    }
Run Code Online (Sandbox Code Playgroud)

到我能够访问的服务器配置http://10.20.30.1/greet,但不是在浏览器中显示消息,而是将其下载为不带扩展名的文件,名为greet,其中包含我的消息。

我发现了很多像我这样的关于 php 的类似问题。在这种情况下,php 现在甚至不应该成为问题。

任何建议都会非常有帮助。

先感谢您!

Ric*_*ith 5

return语句实际上发送具有默认内容类型的文本响应。如果浏览器不理解如何呈现给定的内容类型,它将提供下载该文件。

您可以使用该指令告诉浏览器它是纯文本default_type

例如:

location /greet {
    default_type text/plain;
    return 200 "Hello User!";
}
Run Code Online (Sandbox Code Playgroud)