简体   繁体   English

Nginx/Php : 代理包含 php 脚本

[英]Nginx/Php : Proxy included php script

Let's say my website is located in /srv/http/www and is available at www.mydomain.com .假设我的网站位于/srv/http/www并且可以在www.mydomain.com

I'm trying to have a subdomain devel.mydomain.com to be a place where I can test features before releasing them.我试图让一个子域devel.mydomain.com成为我可以在发布之前测试功能的地方。 The features' code is in /srv/http/www-devel功能代码位于/srv/http/www-devel

The idea is that, when trying to access devel.mydomain.com all ressources available in /srv/http/www-devel are considered, and all non available ressources are retrieved in /srv/http/www as a fallback.这个想法是,当尝试访问devel.mydomain.com时,会考虑/srv/http/www-devel中的所有可用资源,并在/srv/http/www中检索所有不可用的资源作为后备。 Consequently, just moving all files in in /srv/http/www-devel to /srv/http/www (and potentially overwritting existing files) will deploy my features.因此,只需将/srv/http/www-devel所有文件移动到/srv/http/www (并可能覆盖现有文件)即可部署我的功能。

The following nginx config works great for almost every ressources :以下 nginx 配置几乎适用于所有资源:

server {
    listen 80;
    listen 443 ssl;
    server_name devel.mydomain.com;
    access_log              /var/log/nginx/20-devel.access.log;
    error_log               /var/log/nginx/20-devel.error.log;
    ssl_certificate         ...;
    ssl_certificate_key     ...;

    index                   index.php index.html;
    root                    /srv/http/www-devel;

    location ~ /\.          { deny all;                           }
    location = /robots.txt  { access_log off; log_not_found off;  }
    location = /favicon.ico { access_log off; log_not_found off;  }

    location /              { try_files $uri @fallback;           }
    location @fallback      { root /srv/http/www;                 }

    location ~ \.php$ {
        fastcgi_pass          unix:/run/php-fpm/php-fpm.sock;
        fastcgi_index         index.php;
        include               fastcgi.conf;
    }
}

Still the issue is in the processing of php files.问题仍然是在处理 php 文件中。 If I put in /srv/http/www-devel a php script that requires some ressources, and if those ressources are not available in /srv/http/www-devel , the php interpretor will not go look in /srv/http/www .如果我在/srv/http/www-devel放入一个需要一些资源的 php 脚本,并且如果这些资源在/srv/http/www-devel中不可用,则 php 解释器将不会在/srv/http/www查找/srv/http/www How can I enable this behaviour at php level ?如何在 php 级别启用此行为?

Edit: Also I noticed that the location ~ \\.php$ directive doesn't go fetch php pages in the devel directory.编辑:我还注意到location ~ \\.php$指令不会在 devel 目录中获取 php 页面。 How can I enable the fallback in the directive as well ?我怎样才能在指令中启用回退?

One way to use fallback directories in PHP is the include path .在 PHP 中使用回退目录的一种方法是包含路径

If you load files with like that:如果你像这样加载文件:

require_once 'src/Foo/Bar.php';

and you have configured your include path so:并且您已经配置了包含路径,因此:

set_include_path('/srv/http/www-devel:/srv/http/www');

then PHP will first try /srv/http/www-devel/src/Foo/Bar.php and then /srv/http/www/src/Foo/Bar.php然后 PHP 将首先尝试/srv/http/www-devel/src/Foo/Bar.php然后是/srv/http/www/src/Foo/Bar.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM