Nginx 同时为用户目录和 Web 根目录提供服务

Nginx 同时为用户目录和 Web 根目录提供服务

我想提供来自每个用户主目录的目录;就像example.com/~user将从中提供服务一样/home/user/www。那里有一些例子(比如https://unix.stackexchange.com/questions/126745/set-up-nginx-to-serve-files-from-subdirectories);这对我来说部分有效。

我的问题是:如果我可以启用用户目录,根目录 (examle.com/) 会给出 404 未找到。根目录和用户目录不能同时启用。

我可能搞乱了配置。这是我的/etc/nginx/sites-enabled/default。我尝试将“用户目录”块放在“根”位置的上方和下方;但结果是一样的。

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

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #[trimmed]...

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

}


server {

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #[trimmed]...

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;
    server_name example.com; # managed by Certbot

    #root
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    #user dir
    location ~ ^/(.+?)(/.*)?$ {
        alias /home/$1/www$2;
        index  index.html index.htm;
        autoindex on;
    }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    #[trimmed]...

}
server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 ;
    listen [::]:80 ;
    server_name example.com;
    return 404; # managed by Certbot
}

有人能帮助我同时提供网络根目录和用户目录吗?

相关内容