在同一台机器上运行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
您可以将Node.JS和PHP安装在同一台主机上,使用Nginx作为代理例程。
每为例,与Nginx的,你可以创建两个virtualhosts:使用PHP(FPM与否)谁指向exemple.tld
- 默认的虚拟主机。 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;
}
}
正如你所看到的,这是可能的,而且很容易做到!
我不确定,它是否会起作用,因为我从来没有使用过Nginx,但是很明显,任何人都可以通过阅读本文来理解。对不起,我没有太多的名誉投票给你。 –
没问题,这真是一种享受@VijayAnand – GotchaRob
您可以通过apache设置代理通过特定路径到达您计算机上另一个端口(如节点服务器)的请求。但是,这可能是[服务器故障](http://serverfault.com/)的一些原因,因为它更多的与网络和服务器管理有关,而不是程序设计。 –