用于查找index.html的Nginxconfiguration

我正试图完成以下行为:

在文件结构中

/var/html/jpmelos.com/ - index.html - public/ - index.html 

如果我访问http://jpmelos.com ,我想获取public/index.html如果文件存在,如果它不存在,我想提示用户input用户名和密码,并获得index.html 。 同样的行为,如果达到http://jpmelos.com/index.html 。 公用文件夹先检查并匹配,然后用密码保护文件夹。

我将这个configuration文件包含在http块中:

 server { listen 80; server_name jpmelos.com; location / { root /var/html/jpmelos.com/public; index index.html; try_files $request_uri @redirect; } location @redirect { auth_basic on; auth_basic_user_file /htpasswd/jpmelos; root /var/html/jpmelos.com; index index.html; } } 

当我到达http://jpmelos.com/index.html是正确的,我得到的文件public/index.html 。 但是当我到达http://jpmelos.com ,它返回一个403 Forbidden 。 我运行Ubuntu 15.10,Nginx 1.11.1。

这是令人困惑的,因为可以从http://jpmelos.com/index.html返回文件,我们知道这是不被禁止的。 并且configuration正确地设置index index.html ,并且文件在那里。

那么,我错过了什么?


另外,一个侧面的问题:如果我改变root /var/html/jpmelos.com/public; alias /var/html/jpmelos.com/public; ,它不再匹配,并返回404 Not Found 。 这是为什么?

我修复了我的configuration,达到了我想要的:

 server { listen 80; server_name jpmelos.com; root /var/html/jpmelos.com/public; index index.html; autoindex off; location / { try_files $uri $uri/ @redirect; } location @redirect { auth_basic on; auth_basic_user_file /htpasswd/jpmelos; root /var/html/jpmelos.com; } } 

将第一个根指令移到server块,只是因为这是一个很好的做法。 然后,也移动index指令,以避免重复。 然后,更改try_files指令。 这将尝试$uri文件,然后使用index指令的$uri 目录 ,然后redirect。

redirect后,请设置auth设置,并将root更新为新的值,仅用于该location块。