繁体   English   中英

使用Node.js路由欺骗index.php

[英]Spoof index.php with Node.js route

我想在node.js中重写我的php应用程序。 我的一个问题是我们有一些用其他语言编写的遗留客户端应用程序直接指向php文件。 是否可以在快速路线中欺骗php文件?

我尝试过以下方法:

app.get('/index.php/', function(req, res){
    res.end('test');
});

但输入{my domain} /index.php/给了我

不能GET /index.php

我喜欢的是一个名为legacy.js的路由文件,然后随着旧版应用程序的更新,我可以逐个删除路由。

欢呼任何帮助,

知更鸟

几个建议

建议1

由于路径定义中的尾部斜杠,您从上面的路线获得404。 改成:

app.get('/index.php', function (req, res, next) {
  res.send('PHP route called!');
});

建议2

为什么不将nginx / apache设置为节点的反向代理,而不是试图让Node处理你的PHP文件? 例如,使用nginx ,我们可以同时运行PHP脚本和节点后端服务器:

upstream node {
    server localhost:3000;
}

server {
    listen 8080;
    server_name localhost;

    root /path/to/root/directory;
    index index.php;

    # Here we list base paths we would like to direct to PHP with Fast CGI
    location ~* \/tmp|\/blog$ { {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    # Here we set a reverse proxy to upstream node app for all routes
    # that aren't filtered by the above location directives.
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://node;
        proxy_redirect off; 
    }
}

这允许您在同一个域上运行PHP和节点,而不必担心每个PHP脚本都要执行子进程 - 更不用说这会对您的计算机造成内存影响。

暂无
暂无

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

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