3

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

这是我的 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 配置的正确方法是什么?

4

2 回答 2

7

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

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

location = / {
  index   index.html;
}

另外,请务必nginx -s reload在更改配置时进行。

于 2013-07-06T18:32:10.567 回答
5

习俗:

您应该将位置和服务器声明保留在虚拟主机文件中(/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.php文件指令移到location {}块之外

于 2013-07-07T23:12:29.263 回答