Nginx-反向代理未调用PHP脚本 [英] Nginx - PHP scripts not being called from reverse proxy

查看:428
本文介绍了Nginx-反向代理未调用PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面提供/etc/nginx/sites-available/default文件的摘要

I give below an execerpt of of /etc/nginx/sites-available/default file

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-返回世界您好
  • bonjour.php-返回bonjour
  • index.php - returns hello World
  • bonjour.php - returns bonjour

现在是问题所在:

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

If I browse to https://example.com/rproxy it promptly returns hello world - the expected result.

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

However, if I browse to https://example.com/rproxy/bonjour.php (or even https://example.com/rproxy/index.php) I get a 404 error.

我了解这里发生了什么.我的Nginx配置导致Nginx的example.com实例尝试在本地查找所有*.php文件(即在example.com上),而当我要查找的文件实际上在example.org:8144上时会失败.

I understand what is happening here. My Nginx configuration is causing the example.com instance of Nginx to attempt to find all *.php files locally (i.e. on example.com) which fails when the file I am seeking is in fact on example.org:8144.

我想象有一种相对简单的方法来告诉Nginx何时不尝试执行PHP文件-实际上是在rproxy上.但是,我对Nginx混淆的知识太有限,以至于我无法弄清楚如何更改配置.可能会告诉我如何更改配置以防止发生这种情况的人,我将非常负有责任.

I imagine that there is a relatively simple way to tell Nginx when NOT to attempt to attempt to execute a PHP file - when it is in fact on rproxy. However, my knowledge of Nginx confugration is too limited for me to be able to figure out just how I alter the configuration. I'd be most obliged to anyone who might be tell me how to change the configuration to prevent this from happening.

我应该在这里澄清一下内容:

I should clarify something here:

我需要能够在两个服务器 example.comexample.org上运行PHP脚本.

I need to be able to run PHP scripts on Both SERVERS example.com and example.org.

这里有一个非常简单的解决方法-对于代理服务器example.org上的php脚本,我使用另一个扩展名(例如php5).但是,这很容易导致无法预料的问题.

There is a very easy workaround here - I use a different extension, say php5, for php scripts on the proxied server, example.org. However, that is easily liable to lead to unforseen problems.

推荐答案

对于nginx,正则表达式位置比前缀位置具有更高的优先级.但是

For nginx regexp locations have bigger priority than prefix locations. But

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

If the longest matching prefix location has the "^~" modifier then regular expressions are not checked.

所以尝试替换

location /rproxy/ {

使用

location ^~ /rproxy/ {

这篇关于Nginx-反向代理未调用PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆