我是Nginx的初学者。 我用Ubuntu 12.04租了一点VPS来testing。 我租了没有域名,所以我直接使用IP请求。 对于这个问题,我们假设IP是209.208.26.89 。 从官方ppa安装最新的稳定的Nginx。
安装Nginx并检查工作正常后(使用浏览器浏览http://209.208.26.89 ),我从/ etc / nginx / sites-enabled中删除了默认configuration。 我首先使用了这个configuration:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /home/www-data/public-www; index index.html index.htm; #index.html is inside /home/www-data/public-www server_name 209.208.26.89; #remember, no domain location /example { root /home/www-data/public-www/example; index hello.html; #hello.html is inside /home/www-data/public-www/example try_files $uri $uri/ =404; } }
所有目录和权限都正确设置。
当我访问http://209.208.26.89/example我迎来了一个404。奇怪的是,访问http://209.208.26.89/将生成文件hello.html。
这是怎么发生的? 我将该文件设置为位置/example !
不过,我尝试了这个:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /home/www-data/public-www; index index.html index.htm; server_name 209.208.26.89; #remember, no domain location /example { alias /home/www-data/public-www/example; #HERE'S THE CHANGED LINE index hello.html; try_files $uri $uri/ =404; } }
使用此configuration, http:// 209.208.26.89/example将正确生成hello.html。 但是http:// 209.208.26.89将产生相同的文件。
为了说清楚,我想要做的是:在请求http:// 209.208.26.89/example Nginx应该为我/home/www-data/public-www/example/example.html服务。 我做了我的第二个configuration,它的缺点是服务器也在 http:// 209.208.26.89 。 但我的第二个要求是“http:// 209.208.26.89 /”没有任何服务(应该返回404)。
我确信我错过了一些显而易见的事情,但没有任何指导或文件似乎指向了正确的方向。 任何人都可以帮我理解发生了什么?
我相信这是由这一行引起的:
root /home/www-data/public-www;
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
你可以删除它,或指定其他地方。