nginx位置索引指令不起作用

问题描述 投票:3回答:2

我是nginx的新手,我无法确定为什么我的nginx配置无法按预期工作。我想要做的就是让每个web根(/)请求的index.php上的nginx优先化index.html。

这是我的nginx配置:

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

events {
    worker_connections 768;
    multi_accept on;
}

http {
    ##
    # Basic Settings
    ##

    server {
        location / {
            index   index.html index.php;
        }

        location ~ \.php$ {
            fastcgi_pass  localhost:9000;
            fastcgi_param SCRIPT_FILENAME
                          $document_root$fastcgi_script_name;
            include       fastcgi_params;
        }
    }

    sendfile on;
    tcp_nopush on;
    tcp_nodelay off;
    keepalive_timeout 15;
    keepalive_requests 100000;
    types_hash_max_size 2048;
    client_body_in_file_only clean;
    client_body_buffer_size 32K;

    client_max_body_size 300M;
    server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ----------------- cut ---------------

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

哪里是我的错误?编写这个nginx配置的正确方法是什么?

nginx
2个回答
6
投票

惯例:

您应该在虚拟主机文件中保留位置和服务器声明(/etc/nginx/conf.d/*.conf;/etc/nginx/sites-enabled/*;,正如您可以从nginx conf中看到的那样)。 /etc/nginx/conf.d/*.conf;中的文件通常符号链接到/etc/nginx/sites-enabled/*;中的文件,以便“启用”

有些事要尝试

请参阅我的blog post,其设置与您的设置类似。

尝试在index index.html index.html index.php块之外移动location {}文件指令


5
投票

如果您明确请求/index.html,是否提供服务?如果没有,您可能想要在root /path/to/root;块中添加一个显式的server {}。还要验证index.html是否具有正确的权限。

这将有助于排除故障:如果找不到根index.html,它将强制404。如果发生这种情况,至少可以检查日志以查看它是否正在寻找:

location = / {
  index   index.html;
}

此外,确保在更改配置时执行nginx -s reload

© www.soinside.com 2019 - 2024. All rights reserved.