如何使用Nginx传送不带扩展名的文件?

时间:2018-08-29 02:25:42

标签: html nginx configuration

我所有的静态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

1 个答案:

答案 0 :(得分:0)

最终我弄清楚了:

  • 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;
    
    }
    

    }