nginx 位置在没有 root 的情况下无法工作,尽管 root 在服务器中

nginx location doesn't work without root, despite root being in server

我有一个 nginx 配置,其根目录在服务器块中指定。根据这样的页面 (https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/),这应该足够了,而无需在 location / block 中放置相同的根。但是除非我在 location / 块中放置一个 root 指令,否则我会收到 404 错误。这是我的服务器块:

server {
    listen          80;
    server_name     mysite.com

    root /usr/local/nginx/sites/mysite;
    index index.php index.html;

    location / {
        root /usr/local/nginx/sites/mysite;
        index  index.php index.html index.htm;
    }

    location ~ \.php$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }

    error_page   500 502 503 504  /50x.html;
}

因此,如果“root /usr/local/nginx/sites/mysite;”在 location / 内,一切正常。但如果不是,就好像服务器块中的相同根指令被忽略了。我在这里错过了什么?

您有一个语法错误。

server {
    listen          80;
    server_name     mysite.com; # <--- Missing semicolon

    root /usr/local/nginx/sites/mysite;
    index index.php index.html;

    location / {
        root /usr/local/nginx/sites/mysite;
        index  index.php index.html index.htm;
    }

    location ~ \.php$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }

    error_page   500 502 503 504  /50x.html;
}

这会导致 nginx 对 root 属性视而不见,因为此类关键字必须首先出现,并以大括号或分号分隔。