基于主机名的动态nginx域根路径?

Xeo*_*oss 12 php nginx virtualhost

我正在尝试使用基本的 master/catch-all vhost 配置来设置我的开发 nginx/PHP 服务器,以便我可以根据需要创建无限的___.framework.loc

server {
        listen 80;
        index index.html index.htm index.php;

        # Test 1
        server_name ~^(.+)\.frameworks\.loc$;
        set $file_path $1;
        root    /var/www/frameworks/$file_path/public;

        include /etc/nginx/php.conf;
}
Run Code Online (Sandbox Code Playgroud)

但是,对于此设置,nginx 会响应 404 错误。我知道 nginx 和 PHP 正在工作并且有权限,因为localhost我使用的配置工作正常。

server {
        listen 80 default;
        server_name localhost;
        root /var/www/localhost;
        index index.html index.htm index.php;

        include /etc/nginx/php.conf;
}
Run Code Online (Sandbox Code Playgroud)

我应该检查什么才能找到问题?这是他们正在加载的 php.conf 的副本。

location / {
        try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {

        try_files $uri =404;

        include fastcgi_params;
        fastcgi_index index.php;

        # Keep these parameters for compatibility with old PHP scripts using them.
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        # Some default config
        fastcgi_connect_timeout        20;
        fastcgi_send_timeout          180;
        fastcgi_read_timeout          180;
        fastcgi_buffer_size          128k;
        fastcgi_buffers            4 256k;
        fastcgi_busy_buffers_size    256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_intercept_errors    on;
        fastcgi_ignore_client_abort off;
        fastcgi_pass 127.0.0.1:9000;

}
Run Code Online (Sandbox Code Playgroud)

Duk*_*ion 13

Nginx config 不是程序,而是声明。当您使用这样的配置时:

server {
        server_name ~^(.+)\.frameworks\.loc$;
        ...
        set $file_path $1;
        root    /var/www/frameworks/$file_path/public;
}
Run Code Online (Sandbox Code Playgroud)

无法确保您的set指令会在root.

但是map我喜欢使用指令有一个技巧。它依赖于map之前评估的事实location

http {
  map $http_host $rootpath {
    ~^(.?<mypath>+)\.frameworks\.loc$  $mypath;
    default                            /      ;
  }
  ....
  root /var/www/frameworks/$rootpath
}
Run Code Online (Sandbox Code Playgroud)


Mic*_*ton 12

为什么不使用:

server_name *.frameworks.loc;
root /var/www/frameworks/$http_host/public;
Run Code Online (Sandbox Code Playgroud)