Nginx-没有从反向代理调用PHP脚本


Nginx - PHP scripts not being called from reverse proxy

我在下面给出了/etc/nginx/sites-available/default文件的执行程序

server {
        listen 443 ssl;
        server_name example.com;
        root /var/www/html;
        index index.php index.html;
location  / {
             try_files $uri $uri/ = 404;
}
location /rproxy/ {
                  proxy_pass https://example.org:8144/;
}     
location ~ '.php$ {
 try_files $uri = 404;
 fastcgi_split_path_info ^(.+'.php)(/.+)$;
 ....
} 

example.org:8144服务器

有文件和

  • index.php-返回hello World
  • bonjour.php-返回好运

现在的问题是:

如果我浏览到https://example.com/rproxy,它会立即返回hello-world-预期结果。

但是,如果我浏览到https://example.com/rproxy/bonjour.php(甚至https://example.com/rproxy/index.php),我会得到一个404 error

我理解这里发生的事情。我的Nginx配置导致Nginx的example.com实例尝试在本地(即在example.com上)查找所有*.php文件,当我正在查找的文件实际上在example.org:8144上时,该操作失败。

我想有一种相对简单的方法可以告诉Nginx何时不尝试执行PHP文件——当它实际上在rproxy上时。然而,我对Nginx配置的了解太有限,我无法弄清楚如何更改配置。我非常感谢任何可能告诉我如何更改配置以防止这种情况发生的人。


我应该在这里澄清一下:

我需要能够在SERVERSexample.comexample.org上运行PHP脚本。

这里有一个非常简单的解决方法-我在代理服务器example.org上为php脚本使用了一个不同的扩展,比如php5。然而,这很容易导致不可预见的问题。

对于nginx,regexp位置的优先级高于前缀位置。但是

如果最长匹配前缀位置具有"^~"修饰符,则不检查正则表达式。

所以试着取代

location /rproxy/ {

带有

location ^~ /rproxy/ {

您传递的上游服务器不知道您的nginx配置。同样,你的nginx不知道上游应该如何响应请求。他们两个都不会在意对方的配置。

这是一个真正传递脚本名称的开始,但有很多不同的方法可以做到这一点,它们都完全取决于上游的配置方式。此外,如果可以避免的话,不要使用正则表达式;毫无理由地放慢速度是没有意义的。

upstream reverse {
    server example.org:8144;
}
server {
    listen 443 ssl;
    server_name example.com;
    root /var/www/html;
    index index.php index.html;
    location  / {
        try_files $uri $uri/ =404;
    }
    location /rproxy {
        proxy_pass https://reverse/$request_uri;
    }     
    location ~ '.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+'.php)(/.+)$;
        include fastcgi_params;
        fastcgi_pass /blah/tmp.sock;
    }
 }

(不一定聪明)但巧妙的方法是,当你定义你的PHP块回到上游而不是=404时(同样,这是一个可怕的想法,除非你知道你在做什么,但它是可以做到的):

location @proxy {
    proxy_pass https://upstream$request_uri;
}
location ~ '.php$ {
   try_files $uri @proxy;
   fastcgi_split_path_info ^(.+'.php)(/.+)$;
   include fastcgi_params;
   fastcgi_pass /blah/tmp.sock;
}