Nginx-子目录的单独WebRoot

时间:2019-04-30 14:40:21

标签: nginx

我正在尝试为下面的nginx.conf文件中的每个子目录设置一个单独的Web根。

我认为这是正确的方法,但是当我从每个回显$ _SERVER ['DOCUMENT_ROOT']时,我会收到相同的结果(/ var / www / htdocs)。

任何人都可以看到我出了问题的地方,并指出正确的方向吗?

server { 
  listen 80; 
  listen [::]:80; 

  server_name testing.local;

  root /var/www/htdocs/desktop;
  index index.html index.php;
  autoindex on;

  location /tablet {
      root /var/www/htdocs/tablet;
      index index.html index.php;
      try_files $uri $uri/ /index.php?$args;

      location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass phpfpm:9000;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
      }
  }

  location /mobile {
    root /var/www/htdocs/mobile;
    index index.html index.php;
    try_files $uri $uri/ /index.php?$args;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass phpfpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

  }

  location ~ \.php$ {
      try_files $uri =404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass phpfpm:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
  }
}

1 个答案:

答案 0 :(得分:0)

通过将root的值与URI串联来构造文件的路径,因此在您的配置中,您两次在路径名中指定了mobiletablet!有关详细信息,请参见this document

^~指令的location修饰符对于提高优先级是必需的,以便嵌套位置可以处理PHP文件。有关详细信息,请参见this document

例如:

location ^~ /mobile {
    root /var/www/htdocs;
    ...
    location ~ \.php$ {
        ...
    }
}