简体   繁体   English

如何使用 Nginx 将子文件夹作为 root 服务?

[英]How to serve subfolder as root using Nginx?

This is my server block at /etc/nginx/sites-enabled/mydomain ;这是我在/etc/nginx/sites-enabled/mydomain的服务器块;

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name mydomain.com;

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}

Inside my /var/www/html/ folder I have folder1 and folder2 .在我的/var/www/html/文件夹中,我有folder1folder2 I can access both of these at mydomain.com/folder1 and mydomain.com/folder2 .我可以在mydomain.com/folder1mydomain.com/folder2访问这两个。 folder1 is currently being served as mydomain.conf/folder1 so no issues on that one. folder1目前作为mydomain.conf/folder1服务,所以那个没有问题。

However, I want to server folder2 as my main domain.但是,我想将folder2作为我的主域。 So when I access mydomain.com it should serve whatever insides /var/www/html/folder2 .因此,当我访问mydomain.com时,它应该提供/var/www/html/folder2内部的任何内容。

I literally tried every single answer I could find online (and I'm aware there are dozens of similar questions online) yet none of them worked for me.我确实尝试了我可以在网上找到的每一个答案(我知道网上有很多类似的问题),但没有一个对我有用。 Sorry I'm a bit rookie and I appreciate your understanding.对不起,我有点菜鸟,感谢您的理解。

EDIT: Both folder1 and folder2 contain PHP apps.编辑: folder1folder2都包含 PHP 应用程序。

Your first option is to use您的第一个选择是使用

root /var/www/html/folder2;

and put folder1 under the /var/www/html/folder2 .并将folder1放在/var/www/html/folder2下。 If you can't do it for some reason, your second option is to use the following config (I omit the try_files directive from your root location since try_files $uri $uri/ =404 is a default behavior):如果由于某种原因你不能这样做,你的第二个选择是使用以下配置(我从你的根位置省略了try_files指令,因为try_files $uri $uri/ =404是默认行为):

server {
    listen 80;
    root /var/www/html/folder2;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name mydomain.com;

    location / {}

    location /folder1/ {
        root /var/www/html;
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
    }

    location ~ /\.ht {
        deny all;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
}

If you want the /folder1 URL to be workable too, you either change location /folder1/ { ... } to location /folder1 { ... } or add an explicit redirect (this won't needed if you put your folder1 under the /var/www/html/folder2 , in that case this redirect will be issued automatically):如果您希望/folder1 URL 也可以使用,您可以将location /folder1/ { ... }更改为location /folder1 { ... }或添加显式重定向(如果您将folder1放在/var/www/html/folder2 ,在这种情况下,此重定向将自动发出):

location = /folder1 {
    return 301 /folder1/$is_args$args;
}

I prefer the second way since using the first one you made any route started with /folder1 prefix (but different from it, eg /folder10 ) unavailable for your root web app.我更喜欢第二种方式,因为使用第一种方式,您使任何以/folder1前缀开头的路由(但与它不同,例如/folder10 )对于您的根 Web 应用程序不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM