2017-03-05 13 views
1

嗨,如果我要设置用户家public_htmlhttp://mysite/user1/ 这之前没有~user1nginx的用户公共家里没有〜

如果访问http://mysite/它会去/usr/share/nginx/html/index.html

我设置配置为如下:

server { 
    listen 80 default_Server; 

    # Home directories 
    location ~ ^/(.+?)(/.*)?$ { 
     alias /home/$1/public_html$2; 
    } 
    location/{ 
     root /usr/share/nginx/html; 
     index index.php index.html index.htm; 
    } 

当我访问http://mysite/user1/,它的工作原理如我所料。

但现在http://mysite/变成404。 我对正则表达式和规则搜索序列不是很熟悉 如何让用户home和root都工作?

+0

好吧,我会帮助你,但我的问题是。如果你访问'http:// mysite /'你想访问'/ usr/share/nginx/html/index.html',那么你想在访问'http:// mysite/user1 /'的时候访问什么? –

+0

'http:// mysite/user1 /'将会是'/ home/user1/public_html/index.html','http:// mysite /'将会是'/ usr/share/nginx/html/index.html' – CSJ

回答

1

URI /的内部URI映射到URI /index.html,它也与您的正则表达式匹配。假设服务器上的唯一子目录是用户,则可以在正则表达式中强制使用第二个/

location ~ ^/(.+?)/(.*)?$ { 
    alias /home/$1/public_html/$2; 
} 

或者,使/index.html URI显式的(通过移动root语句和添加一个空的location块):

root /usr/share/nginx/html; 
... 
location = /index.html {} 

更多见this document

+0

令人惊讶的是,它的工作原理,非常感谢:D – CSJ

相关问题