nginx 位置索引指令不起作用

程序猿 阅读:61 2024-02-27 23:08:18 评论:0

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

请您参考如下方法:

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

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

location = / { 
  index   index.html; 
} 

另外,一定要做 nginx -s reload更改配置时。


标签:nginx
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号