【问题标题】:How to deliver a a file with no extension with Nginx?如何使用 Nginx 传递没有扩展名的文件?
【发布时间】:2018-08-29 02:25:41
【问题描述】:

我所有的静态 HTML 文件都没有扩展名,这意味着它们不是index.html,它们只是index。我如何告诉 Nginx 访问它们并将它们作为 HTML 文件提供?我有以下配置。

server {

        listen 80;
        listen [::]:80;

        listen 443 ssl;
        listen [::]:443 ssl;

        include snippets/snakeoil.conf;

        root /home/davidgatti/Documents/example.com;

        index home

        server_name example.loc;

        location / {
                default_type text/html;

                try_files $uri $uri $uri/ =404;
        }
}

现在 Nginx 发回一个404

【问题讨论】:

  • nginx 随文件发送的内容类型是什么?如果文件名被解释为不同的类型,您可以将 types {} 添加到您的位置以删除类型数组。
  • 我更新了问题。它现在发回 404。
  • 收到 404 响应时尝试访问的文件的完整路径是什么,访问日志中对应的条目是什么?错误日志中有条目吗?

标签: html nginx configuration


【解决方案1】:

我终于弄明白了:

  • index /home; 需要一个斜线
  • try_files 需要$uri.html 不知道为什么,但是没有它就行不通。

    #

    服务器配置

    # 服务器{

    listen 80;
    listen [::]:80;
    
    listen 443 ssl;
    listen [::]:443 ssl;
    
    #
    #   Use the a default SSL for development.
    #
    include snippets/snakeoil.conf;
    
    #
    #   Tell which folder to server.
    #
    root /home/davidgatti/Documents/Retireryte/YOUR_SITE_NAME/_output;
    
    #
    #   Set the default file.
    #
    index /home;
    
    #
    #   Tell Nginx which url is this configuration for.
    #
    server_name login.example.loc;
    
    #
    #   Default configuration.
    #
    location / {
    
        #
        #   Since we deliver files with no extension, we need to tell
        #   Nginx what those files are.
        #
        default_type text/html;
    
        #
        #   First attempt to serve request as file, then
        #   as directory, then fall back to displaying a 404.
        #
        try_files $uri $uri.html $uri/ =404;
    
    }
    

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多