用于在主目录中显示内容的Nginx配置

时间:2016-08-31 23:56:48

标签: nginx

当您输入/~user时,要求是在本地用户的主目录中显示内容。

www.blahblahblah.com/~user

我创建了一个带有主目录的本地用户,并配置了Nginx:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm test.html;

    # Make site accessible from http://localhost/
    server_name localhost;

    location ~* /\~(.*)/$ {
        root /home/$1/;
    }

    location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
    }
}

但我仍然没有找到404错误。

1 个答案:

答案 0 :(得分:0)

问题在于连接根和URI以获取请求文件名,因此您要求nginx提供root /home/user/~user/

解决方案是使用rewrite ... break来清理URI。您可以扩展正则表达式以捕获这两个元素,然后在rootrewrite ... break语句中使用命名捕获:

location ~* /\~(?<username>\w+)(?<userpath>/.*)$ {
    root /home/$username;
    rewrite ^ $userpath break;
}