【问题标题】:Rewrite within location with a new root/alias - Nginx使用新的根/别名在位置内重写 - Nginx
【发布时间】:2014-05-19 00:05:32
【问题描述】:

我的文件夹结构如下:

/www
    /api.domain.tld
    /app.domain.tld

API包含自己的系统,APP通过HTTP实现API。

我想为 app.domain.tld 创建一个 Nginx 服务器,其中还包含一个 API 的“虚拟目录”。

您可以通过以下方式联系 API:http://api.domain.tld/method/api.json

但如果 API 也可以这样联系就太好了:http://app.domain.tld/api/method/api.json 无需将内容复制到 APP 中,但将这两个“系统”分开。

我现在拥有的:

server {
    listen 80;
    root /var/www/app.domain.tld;
    index index.php index.html;


    server_name app.domain.tld;


    location ^~ /api {
        alias /var/www/api.domain.tld;


        location ~ \.php$ {
            try_files $uri = 404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }


        rewrite ^/api/([a-z]+)/api.json$ /api.php?method=$1 last;
    }


    location....
    location....
    location....


    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

不幸的是,这现在可以按预期工作。

重写根本不起作用。我可以得到 api.domain.tld/index.php 但是当它需要使用重写时,它就不起作用了。

我已经尝试了几件事。我得到 404 或 403 这个错误: [路径]的目录索引被禁止

有人能想出一个更好的解决方案吗?

问候

【问题讨论】:

    标签: nginx location rewrite alias root


    【解决方案1】:

    您应该更改 SCRIPT_FILENAME 路径:

    server {
        listen 80;
        root /var/www/app.domain.tld;
        index index.php index.html;
    
        location ~ ^/api/(.+\.php)$ {
            alias /www/api.domain.tld/public/$1;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include fastcgi_params;
        }    
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-21
      • 1970-01-01
      • 2013-12-16
      • 2019-06-24
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      相关资源
      最近更新 更多