简体   繁体   中英

Nginx + PHP, Node.js and jQuery setup

I would like to implement some live elements to an existing PHP app. So i installed Node.js and set it on to listen on port let s say 3000 as the Nginx server is currently serving my PHP app on port 80.

SO far so good, node server works great and i can hit it from php with cURL. Now the problem is that i need to open a connection from an html (serverd by the PHP framework on ngix) so at localhost/index.html to a the node server at localhost:3000. So this mean having to make cross domain ajax call and it get messy.. For those who haven't tried it's basically not possible (without ugly hacks) to make an ajax call on a sub domain or a different port on the same server.

Would anyone see a better way to implement this whole setup ?

You are on the right path with Nginx. Since you must stay on the same domain and port, you can redirect a certain folder request to your Node. Something like this:

  location ~ /ajax/* {                                                  
    proxy_pass http://127.0.0.1:8080;                                                                
    break;                                                                                                        
  }

The other solution is to use CORS: http://enable-cors.org/ It works beautifully on all modern browsers except IE.

Even without knowing the full purpose, I wouldn't expose a home-brewed server setting. But if you really need to, you can do it the old fashioned way, via script-tag proxy. ( <script src="http://external.host.com:..." ).

You can append a url parameter request_id to script address and have that script come back with the request id in the form of if(typeof($)==='object' && typeof($.trigger)==='function')$.trigger('requestCompleted',request_id); something like that.

You could set it up to sort-of reverse proxy the request... send the AJAX call to your main domain, say, example.com/?proxy=live&restofrequest. Have nginx rewrite that to the appropriate domain:port for the node.js server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM