Nginx忽略位置指令?

时间:2012-08-24 21:06:44

标签: 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映射到location /(正在运行)中指定的根,并将domain.com/othersite映射到该指令中指定的根。出于某种原因,当我导航到domain.com/othersite而不是/ home / TMlabs / server_files / location2文件夹中的index.html文件时,它会返回/home/TMlabs/server_files/location1/index.html文档。我已经尝试删除equals并使用可用的不同运算符进行匹配,但似乎没有任何效果。它可能是一个非常基本的东西,我误解,对这些东西不熟悉。我也很喜欢正则表达式。有人想开导吗?

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

2 个答案:

答案 0 :(得分: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将尝试通过URL的扩展来计算响应的Content-Type。由于/otherside没有扩展程序,因此会使用application/octet-stream导致浏览器try and download the content

您可以添加.html扩展名,也可以使用default_type内的location指令

default_type "text/html";

答案 1 :(得分: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/; }
}
相关问题