nginx 子目录作为根目录以及其他文件夹

Cha*_*les 2 nginx

根 ( http://www.example.com/ ) 将指向 /var/www/wordpress

我怎样才能让每个人都可以在浏览器中查看?

working -- http://www.example.com
error -- http://www.example.com/wordpress2
error -- http://www.example.com/htmlsite
Run Code Online (Sandbox Code Playgroud)

这是结构:

first wordpress: /var/www/wordpress
second wordpress: /var/www/wordpress2
static html page: /var/www/htmlsite

server {
root /var/www/wordpress;
index index.php;
...

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

location /wordpress2 {
  root /var/www/wordpress2;
  try_files $uri $uri/ /index.php$is_args$args;
}

location /htmlsite {
  root /var/www/htmlsite;
  try_files $uri $uri/ =404;
}

}
Run Code Online (Sandbox Code Playgroud)

如果我这样做root /var/www;,那么 /wordpress2 和 /wordpress3 可以工作:

server {
root /var/www;
index index.php;
...

location / {
  try_files $uri $uri/ =404;
}

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

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

location /htmlsite {
  root /var/www/htmlsite;
  try_files $uri $uri/ =404;
}

}
Run Code Online (Sandbox Code Playgroud)

Ter*_*nen 6

这应该按照您想要的方式工作。

由于 URI 附加到root指令中指定的目录,我们只需要指定一次。只需为每个位置分别指定`try_files'。

server {
    root /var/www/wordpress;
    index index.php;
    ...

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

    location /wordpress2 {
        root /var/www;
        try_files $uri $uri/ /wordpress2/index.php$is_args$args;
    }

    location /htmlsite {
        root /var/www;
        try_files $uri $uri/ =404;
    }
}
Run Code Online (Sandbox Code Playgroud)