2012-08-24 131 views
3

我已阅读过文档,扫描的示例,但我不知道为什么这不起作用。我对nginx完全陌生,所以如果这是一个愚蠢的简单答案,请轻松点我。Nginx忽略位置指令?

user www-data; 
worker_processes 4; 
pid /var/run/nginx.pid; 

events { 
     worker_connections 768; 
     # multi_accept on; 
} 

http { 

     ## 
     # Basic Settings 
     ## 

     sendfile on; 
     tcp_nopush on; 
     tcp_nodelay on; 
     keepalive_timeout 65; 
     types_hash_max_size 2048; 
     # server_tokens off; 

     # server_names_hash_bucket_size 64; 
     # server_name_in_redirect off; 

     include /etc/nginx/mime.types; 
     default_type application/octet-stream; 

     ## 
     # Logging Settings 
     ## 

     access_log /var/log/nginx/access.log; 
     error_log /var/log/nginx/error.log; 

     ## 
     # Gzip Settings 
     ## 

     gzip on; 
     gzip_disable "msie6"; 

     # gzip_vary on; 
     # gzip_proxied any; 
     # gzip_comp_level 6; 
     # gzip_buffers 16 8k; 
     # gzip_http_version 1.1; 
     # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; 

     ## 
     # Virtual Host Configs 
     ## 

     #include /etc/nginx/conf.d/*.conf; 
     #include /etc/nginx/sites-enabled/*; 

    server { 
       listen  80; 
       server_name localhost domain.com; 

       root /home/TMlabs/server_files; 
       index index.html index.htm; 

       location = /othersite { 
        root /home/TMlabs/server_files/location2; 
        index index.html index.htm; 
       } 

       location/{ 
        root /home/TMlabs/server_files/location1; 
        index index.html index.htm; 
       } 

     } 

如果我需要包括更多,让我知道,我会转储整个nginx.conf。不过,这很基本。

我想要实现的所有功能都是将domain.com映射到位置/(正在工作)中指定的根,并将domain.com/othersite映射到该指令中指定的根。出于某种原因,当我导航到domain.com/othersite而不是/ home/TMlabs/server_files/location2文件夹中的index.html文件时,它会返回/home/TMlabs/server_files/location1/index.html文档。我试过去除了等号并使用可用于匹配的不同运算符,但似乎没有任何工作。这可能是一些非常基本的东西,我误解了这些东西。我也吮吸正则表达式。任何人都在照顾开导?

编辑:我认为发生的事情是,它实际上完全忽略了我的指令,只是简单地使用/ usr/share/nginx/www作为文档根目录。我没有在任何地方看到这个配置;我错过了什么?

+0

我想你可能过于简化您的示例配置为必须有别的事情上那里得到这一结果。请张贴更详细的信息。其余加入 – Dayo

+0

。我正在从一个模板开始工作,这就是其余部分。 –

+0

在上面发布的完整配置中,您错过了http块的右括号。 此外索引指令是继承的,所以不需要在每个位置块中重复它。而=/othersite意味着只有特定的/别人的url匹配,而不是任何可能不是你想要的子页面。 – cobaco

回答

2

=前缀是指指定的位置应该请求的URL完全匹配。如果查找错误日志,您会看到Nginx尝试提供文件 [...]/location2/otherside,而不是[...]/location2/index.{html,htm}。 (为简单起见省略全部路径)

index指令是精确匹配位置内的noop。

要解决这个问题,你可以将文件重命名为otherside,并有此配置

location = /otherside { 
    root /home/TMlabs/server_files/location2; 
} 

或使用alias指令

location = /otherside { 
    alias [...]/location2/index.html; 
} 

PS:Nginx的会揣摩的Content-Type由URL的扩展的响应。由于/otherside没有扩展名,因此将使用application/octet-stream导致浏览器到try and download the content

您可以包括.html扩展,或使用default_type指令内location

default_type "text/html"; 
+0

感谢您的澄清!和额外的花絮。我的问题最终变得更简单,我发现找到了error.log文件。 (文件路径损坏。)但这些点确实回答我最初的问题,并且非常有帮助!谢谢Dan,哈哈! –

0

以下应该做的伎俩:

server { 
     listen  80; 
     server_name localhost domain.com; 

     root /home/TMlabs/server_files; 
     index index.html index.htm; 

     location/{ try_files location1/$uri location1/$uri/; } 
     location /othersite { try_files location2/$uri location2/$uri/; } 
} 
+0

这没有奏效:/ –