Apache (as proxy) and Node.JS on Ubuntu
My friend asks me to prepare a Node.JS support on his server.
Here is hello.js:
#!/usr/bin/env nodejs
var https = require('https');
var fs = require('fs');
var options = { key: fs.readFileSync('/etc/letsencrypt/live/xxxxx/privkey.pem'), cert: fs.readFileSync('/etc/letsencrypt/live/xxxxx/cert.pem'), ca: fs.readFileSync('/etc/letsencrypt/live/xxxxx/chain.pem')
};
https.createServer(options, function (req, res) { res.writeHead(200); res.end("Hello world! Node.js is working correctly on HTTPS!!\n");
}).listen(8000);
console.log('Server running at );Here is what is added in Appache site configuration:
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *> Require all granted
</Proxy>
<Location /nodejs> ProxyPass ProxyPassReverse
</Location>"sudo node show hello.js" shows that everything is ok. I'm almost sure that problem is in Apache part of this problem. But what?
UPDATED: Some new details. Maybe someone will see a mistake. Apache .conf files:
<VirtualHost *:80> ServerAdmin ServerName example.com ServerAlias DocumentRoot /var/www/example.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined RewriteEngine on RewriteCond %{SERVER_NAME} =example.com [OR] RewriteCond %{SERVER_NAME} = RewriteRule ^ [END,NE,R=permanent] ProxyRequests Off ProxyPreserveHost On ProxyVia Full <Proxy *> Require all granted </Proxy> <Location /nodejs> ProxyPreserveHost On ProxyPass ProxyPassReverse </Location> <Directory "/var/www/example.com"> AuthType Basic AuthName "Restricted Content" AuthUserFile /etc/apache2/.htpasswd Require valid-user Options FollowSymLinks AllowOverride All </Directory>
</VirtualHost>
********
<IfModule mod_ssl.c>
<VirtualHost *:443> ServerAdmin ServerName example.com ServerAlias DocumentRoot /var/www/example.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined Include /etc/letsencrypt/options-ssl-apache.conf SSLCertificateFile /etc/letsencrypt/live/ SSLCertificateKeyFile /etc/letsencrypt/live/ <Directory "/var/www/example.com"> AuthType Basic AuthName "Restricted Content" AuthUserFile /etc/apache2/.htpasswd Require valid-user </Directory>
</VirtualHost>
</IfModule>I tried on server and here are two results:
sudo wget --user ime --password geslo This WORKS (I get index.html with right response from Node.js app).
sudo wget --user ime --password geslo This DON'T WORK (I get "nodejs" file with a code for folder contents).
Any idea?
31 Answer
Try this in apache configuration
<Location "/"> ProxyPreserveHost On ProxyPass ProxyPassReverse
</Location>Don't forget to enable proxy and proxy_http for this you can use the command
sudo a2enmod proxy && sudo a2enmod proxy_http
After this, you have to restart your apache server.
2