Nginx - 使用不同的目录来提供文件,同时从URL中删除路径

时间:2015-11-15 13:44:56

标签: nginx

我正在尝试使用不同的目录来从哪里提供文件,但是都没有成功。 translatedNumber = PhoneTranslator.sharedInstance.ToNumber(inputText) 是根目录,但我希望从/var/www/site.app/public提供news,而我需要删除/var/www/news/api部分的网址http://site.app/news/123 ,因为否则会映射到news

根据调试日志判断,它似乎在第一次测试所有位置块时被正确重写,但在完成重写之后,它再次遍历所有这些,并最终提供位置块{{1}的内容}。

这是我的配置文件。

/var/www/news/api/news/123

2 个答案:

答案 0 :(得分:0)

root位置中的/news/指令目前无效。您正在将URI重写为/api.php,然后点击您的\.(hh|php)$位置,该位置从父服务器容器继承root

/api.php需要一个不同的位置,可能是/news/api.php。在这种情况下,您可以构建一个单独的位置块(可能嵌套在/news/中),您可以使用不同的根来放置fastcgi_pass代码。

答案 1 :(得分:0)

所以这不是理想的解决方案,但它确实有效。默认情况下它仍然允许访问api.php,但是我通过检查URL是否包含/news/

来拒绝访问它
server {
    listen 80;
    server_name site.app;
    root /var/www/site.app/public;

    index index.php index.html;

    location / {
        try_files $uri $uri/ index.php?$query_string;
    }

    location = /api.php {
        if ($request_uri !~ ^/news/) {
            return 444;
        }

        root /var/www/site.app/news/;
        try_files $uri $uri/ /api.php?$query_string =408;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location /news/ {
        rewrite ^/news/(.*) /api.php?$1;
    }

    location ~ \.(hh|php)$ {
        try_files $uri /index.php =407;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
相关问题