在同一台机器上运行Node.js应用程序和PHP

我真的很困惑,这是否有可能? 请帮助我,我有Node.js应用程序,说node_app,运行在X端口,PHP应用程序,说my_app,运行在Apache的默认80端口。 我只有一个域名。 我的问题是,如果用户点击domain.com/my_app ,它应该在80年代的端口运行PHP应用程序。 如果用户访问domain.com/node_app ,则应该在X端口运行节点应用程序。 另一个重要的限制是最终用户不应在URL栏中看到任何端口号

您可以将Node.JS和PHP安装在同一台主机上,例如使用Nginx作为代理。

例如,用Nginx,你可以创build两个虚拟主机:

  • 默认虚拟主机使用PHP(FPM或不)谁指向exemple.tld
  • 第二个虚拟主机到另一个node.exemple.tld

第一个VH就是这样(用PHP-FPM):

server { listen 80; ## listen ipv4 port 80 root /www; index index.php index.html index.htm; # Make site accessible from exemple.tld server_name exemple.tld; location / { try_files $uri $uri/ /index.php; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 and using HHVM or PHP # location ~ \.(hh|php)$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_keep_conn on; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ~ /\.ht { deny all; } } 

第二个VH和NodeJS:

 server { listen 80; server_name node.exemple.tld; 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; access_log off; # Assuming that NodeJS listen to port 8888, change if it is listening to another port! proxy_pass http://127.0.0.1:8888/; proxy_redirect off; # Socket.IO Support if needed uncomment #proxy_http_version 1.1; #proxy_set_header Upgrade $http_upgrade; #proxy_set_header Connection "upgrade"; } # IF YOU NEED TO PROXY A SOCKET ON A SPECIFIC DIRECTORY location /socket/ { # Assuming that the socket is listening the port 9090 proxy_pass http://127.0.0.1:9090; } } 

正如你所看到的,这是可能的,而且很容易做!