I am really confused whether it is possible ? Please help me out, I have Node.js application, say node_app, running on X port, and PHP application, say my_app, running in Apache's default 80 port. I have one domain name only. What my problem is, if user hit domain.com/my_app, it should run the PHP application in 80's port. If the user hits domain.com/node_app, it should run the node application in X port. And one more important constraint is the end-user should not see any port number in URL bar.
1
-
You can have apache setup to proxy requests that come through a specific path to another port on your machine such as a node server. However that is probably something for server fault because it has more to do with networking and server management instead of programming which is what SO is for.Jonathan Kuhn– Jonathan Kuhn2014-05-15 16:28:43 +00:00Commented May 15, 2014 at 16:28
Add a comment
|
1 Answer
You can install Node.JS and PHP in the same host, using Nginx as proxy per exemple.
Per exemple, with Nginx, you could create two virtualhosts :
- Default virtual host using PHP (FPM or not) who points to exemple.tld
- Second virtual host to another node.exemple.tld
First VH is gonna be like this (with 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;
}
}
Second VH with 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;
}
}
As you can see, it's possible, and pretty easy to do!
2 Comments
Vijay Anand
I'm not sure, whether it will work, because I never used Nginx, but it is very clear, anyone can understand just by reading this. Sorry, I don't much reputation to up vote for you.
GotchaRob
No problem, it's a pleasure @VijayAnand