【问题标题】:Change nginx root on different requests根据不同的请求更改 nginx root
【发布时间】:2014-09-26 10:00:47
【问题描述】:

我正在尝试配置 nginx。如果我输入http://localhost/dav,它应该在location /dav 指令中使用配置的root (c:/wt-nmp/www/app-dav/frontend/web) 中的index.php 脚本。但是当我调试 nginx 时,它仍然使用在server 配置中配置的root

在调试中它看起来像:

  1. 它使用location /dav 并将root 设置为新值并更改为index.php?dav
  2. 现在它使用location ~ \.php$,但$document_root 仍然指向c:/wt-nmp/www/m24/web,但我认为它应该在location /dav 中进行更改,如第1 点所述。

我的问题是当我输入http://localhost/dav时如何配置nginx使用来自c:/wt-nmp/www/app-dav/frontend/web的脚本。

我认为这段代码应该是这样工作的:

  1. rootserver 指令设置为 .../m24/web
  2. 当我输入 url http://example.com/dav 时,指令 location /dav 应该将 root 更改为 .../m24/app-dav/frontend/web,并且应该执行该位置的 php 脚本。
  3. 但是当我调试这个应用程序时,来自.../m24/web 的脚本仍然被执行。为什么location /dav 中的root 指令不会改变$document_root 的值。 $document_root 仍然指向 .../m24/web

我的配置中最重要的部分如下所示:

server {
    ...

    root "c:/wt-nmp/www/m24/web";
    index index.html index.htm index.php;

    location /dav {
        root "c:/wt-nmp/www/m24/app-dav/frontend/web";
        # The $uri gets mangled by nginx, however Yii urlmanager requires REQUEST_URI to route
        set $original_uri $uri?$args;
        # Try the actual uri, then uri as a directory, then send it to yii
        try_files $uri $uri/ /index.php?$args;
    }

    location / {
        #set original_uri to use as the request_uri later on  
        #The $uri gets mangled by nginx, however Yii urlmanager requires REQUEST_URI to route  
        set $original_uri $uri?$args;
        #Try the actual uri, then uri as a directory, then send it to yii  
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass                  php_farm;
        include                       nginx.fastcgi.conf;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param REQUEST_URI     $original_uri; 
    }
}

【问题讨论】:

  • 你可能会在server fault得到更好的回应。
  • 完成。我应该删除这个问题吗?
  • IMO 对这个网站来说实际上并不是题外话,只是想更多的人能够在 SF 上回答。

标签: php nginx root


【解决方案1】:

你应该有 2 个位置指令,一个用于/dav 下的 php 文件,另一个用于/dav 下的静态文件:

location ~ ^/dav/.*\.php$ {                 
        root "c:/wt-nmp/www/app-dav/frontend";
        try_files $uri =404; 
        include     nginx.fastcgi.conf;
        fastcgi_pass    php_farm;
}

location ~ ^/dav/ {
         root "c:/wt-nmp/www/app-dav/frontend";
}

这样文件将从c:/wt-nmp/www/app-dav/frontend/dav 提供。所以你可能还想rename c:/wt-nmp/www/app-dav/frontend/web c:/wt-nmp/www/app-dav/frontend/dav,因为 URL 路径必须与根子目录匹配!

另外,请确保您使用最新版本的WT-NMP - portable Nginx Mysql Php development stack for Windows `

【讨论】:

  • 感谢您的回复。我认为这不会解决我的问题。我编辑了我的帖子,以便为您提供更多我尝试实现的信息。
猜你喜欢
  • 2021-12-24
  • 2014-01-30
  • 2015-10-04
  • 2016-02-03
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2016-03-30
  • 2017-11-27
相关资源
最近更新 更多