我的朋友要求我在他的服务器上准备一个 Node.JS 支持。
这是 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/');
以下是 Appache 站点配置中添加的内容:
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”显示一切正常。我几乎可以肯定问题出在 Apache 部分。但是是什么呢?
已更新:一些新细节。也许有人会发现错误。Apache .conf 文件:
<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>
我在服务器上尝试过,得到两个结果:
sudo wget --user ime --password geslo https://www.example.com:8000
这有效(我从 Node.js 应用程序获得了具有正确响应的 index.html)。
sudo wget --user ime --password geslo https://www.example.com/nodejs
这不起作用(我得到了带有文件夹内容代码的“nodejs”文件)。
任何想法?
答案1
在 apache 配置中尝试这个
<Location "/">
ProxyPreserveHost On
ProxyPass http://localhost:8080/
ProxyPassReverse http://localhost:8080/
</Location>
不要忘记启用代理和 proxy_http,为此你可以使用以下命令
sudo a2enmod 代理 && sudo a2enmod proxy_http
此后,您必须重新启动 apache 服务器。