在转发到nginx上的节点端口后,哪些解决方案可用于运行php



我正在运行nginx,它监听php内容的端口80。我的nodejs服务器运行在端口3001上。我遇到的问题是,当我将用户发送到端口3001上的nodejs服务器时,我还想运行php代码。从那以后,我了解到我不能在nginx上侦听端口3001来运行php代码,因为nodejs已经在使用该端口,并且不能在两者上使用相同的端口。我的问题是,有什么解决方案可以让我在使用nodejs访问端口为3001的网页时运行php。

我尝试过使用反向代理,但我的问题是,当我在80端口上侦听时,例如在主页上,我的端口3001也存在(区别是xxyy.com和xxyy.com:3001(,我也不想将该信息转发到3001端口。

谢谢,

你是对的-你需要一个代理服务器,但你必须设置特定于路径的转发,而不是整个端口

对于PHP,有几种方法可以运行PHP服务器,但本例使用PHP-FPM(请参阅本例(。您还可以设置另一个专用于PHP的web服务器,并使用与nodeJS服务相同的upstream语法。

有很多方法可以对此进行配置(有关位置和代理参数,请参阅文档(,但这里有一个通过套接字使用PHP-FPM的nginx配置示例:

upstream nodeServer {
server 127.0.0.1:3001;
}
server {
listen                *:80 default_server;
server_name           _; # Listens for ALL hostnames
index                 index.html;
proxy_set_header      Host $host;
proxy_http_version    1.1;
proxy_set_header      Upgrade $http_upgrade;
proxy_set_header      Connection "upgrade";
proxy_set_header      X-Forwarded-Proto $scheme;
proxy_set_header      X-Real-IP $remote_addr;
proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream   error timeout invalid_header;
## runs PHP pages with PHP-FPM
location ~ [^/].php(/|$) {
fastcgi_split_path_info ^(.+?.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param HTTP_PROXY "";
## fastcgi_pass 127.0.0.1:9000; ## uncomment this to connect PHP-FPM via port
fastcgi_pass unix:/var/run/php-fpm.sock; ## uncomment this to connect PHP-FPM via socket
fastcgi_index index.php;
include fastcgi_params;
}
## Forwards requests to the set of nodeJS servers defined in the "nodeServer" upstream
location /js {
proxy_pass          http://nodeServer;
}
## serves static files
location / {
sendfile            on;
try_files           $uri $uri/ /index.html;
}
}

最新更新