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 https://127.0.0.1:8080/');
Here is what is added in Appache site configuration:
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
<Location /nodejs>
ProxyPass https://127.0.0.1:8080
ProxyPassReverse https://127.0.0.1:8080
</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 [email protected]
ServerName example.com
ServerAlias www.example.com
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} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
<Location /nodejs>
ProxyPreserveHost On
ProxyPass https://www.example.com:8000
ProxyPassReverse https://www.example.com:8000
</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 [email protected]
ServerName example.com
ServerAlias www.example.com
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/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
<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 https://www.example.com:8000
This WORKS (I get index.html with right response from Node.js app).
sudo wget --user ime --password geslo https://www.example.com/nodejs
This DON'T WORK (I get "nodejs" file with a code for folder contents).
Any idea?
Try this in apache configuration
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.