"File not found" 在配置 nginx 中更改 root 时

"File not found" when change root in config nginx

我想为我的域配置自动创建子域:

example.com -> source in /www/source/
abcd.example.com -> source in /www/source/abcd/

我使用的是这个配置:

server {
        listen 80; 
            server_name   ~^(.*)\.example\.com$; 
            # If a directory doesn't exist...
            if (!-d /www/source/) {
                rewrite . example.com redirect;
            }    
            # Sets the correct root
            root /www/source/;
            location ~* \.php$ {
                fastcgi_index   index.php;
                fastcgi_pass    127.0.0.1:9000;
                include         fastcgi_params;
                fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
            }
        }

/www/source/abcd/中,我有index.php。但它不是运行。当我去 abcd.example.com 我得到 "File not found." 我将 index.php 替换为 index.html,然后 运行 正确。

如何解决?

使用命名捕获,因为数字捕获可能超出范围。

例如:

server_name  ~^(?<subdomain>.*)\.example\.com$; 
root /www/source/$subdomain;

有关更多信息,请参阅 this document