2014-05-15 88 views
1

我真的很困惑它是否可能?請幫助我,我有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

+0

您可以通過apache設置代理通過特定路徑到達您計算機上另一個端口(如節點服務器)的請求。但是,這可能是[服務器故障](http://serverfault.com/)的一些原因,因爲它更多的與網絡和服務器管理有關,而不是程序設計。 –

回答

1

您可以將Node.JS和PHP安裝在同一臺主機上,使用Nginx作爲代理例程。

每爲例,與Nginx的,你可以創建兩個virtualhosts:使用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; 
        } 
    } 
    

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

  • +0

    我不確定,它是否會起作用,因爲我從來沒有使用過Nginx,但是很明顯,任何人都可以通過閱讀本文來理解。對不起,我沒有太多的名譽投票給你。 –

    +0

    沒問題,這真是一種享受@VijayAnand – GotchaRob

    相關問題