nginx位置索引指令不起作用

Kam*_*mal 3 nginx

我是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/*;
}
Run Code Online (Sandbox Code Playgroud)

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

Bra*_*ncy 6

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

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

location = / {
  index   index.html;
}
Run Code Online (Sandbox Code Playgroud)

此外,请务必nginx -s reload在更改配置时执行此操作.

  • @AndreyIzman根据http://nginx.org/en/docs/http/ngx_http_index_module.html`index`在以下上下文中工作:`http`,`server`,`location` (2认同)

fid*_*per 6

惯例:

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

有些事要尝试

请参阅我的博客文章,其中有一个类似于您的设置.

尝试index index.html index.html index.phplocation {}块之外移动files指令