» »

Apache in Node.JS

Apache in Node.JS

dunda ::

Strežnik Ubuntu 18.04, nameščen Apache kot proxy in Node.JS.
Pripravil sem HTTPS HelloWorld primerček in očitno deluje (je tudi lepo v "sudo pm2 show hello", PM2 logi ne vpisujejo ničesar posebnega).
#!/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/');



Apache ima narejeno preusmeritev:
   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>

Ko pa naložim stran, se mi izpiše vsebina mape. Da sem bolj natančen - če pokličem "www.example.com/nodejs" se izpiše vsebina mape. Če pa direktno pokličem "www.example.com/nodejs/hello.js" pa se izpiše vsebina kode za hello.js. Kaj je narobe?
https://www.kacnje.eu
  • spremenil: dunda ()

srus ::

14. vrstica:

}).listen(8000);

Verjetno bi moralo biti 8080 in ne 8000.

crniangeo ::

Convictions are more dangerous foes of truth than lies.

dunda ::

srus je izjavil:

14. vrstica:

}).listen(8000);

Verjetno bi moralo biti 8080 in ne 8000.


Res je napaka - vendar samo v tej temi, pri sebi sem to hitro popravil :)
Vseeno pa čestitke za ostro oko.
https://www.kacnje.eu

dunda ::

crniangeo je izjavil:

https://serverfault.com/questions/40522...


Hm, sem sicer dodal še tole, vendar ni pomagalo.
https://www.kacnje.eu

dunda ::

Hja, sedaj pa res ne vem več. Mogoče bo z novimi informacijami komu kapnilo. Slutim na neko neumnost pri teh nastavitvah za Apache.

V .conf datotekah imam takole:
<VirtualHost *:80>
    ServerAdmin admin@example.com
    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 admin@example.com
    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>


Ker mi v brskalniku ni delovalo, sem poskusil na samem strežniku in dobil naslednja rezultata:
sudo wget --user ime --password geslo https://www.example.com:8000

DELA (pač vrne index.html, v katerem je pravilni odziv Node.js aplikacije).



sudo wget --user ime --password geslo https://www.example.com/nodejs

NE DELA (vrne datoteko "nodejs" datoteko, v kateri je koda za seznam datotek v mapi).
https://www.kacnje.eu

radiokills ::

Manjka ti DirectoryIndex
torej v virtual host sekcijo dodaj
DirectoryIndex hello.js

dunda ::

Ne pomaga. Pravzaprav ne deluje niti če pokličem:
sudo wget --user ime --password geslo https://www.example.com/nodejs/hello.js
https://www.kacnje.eu

celebro ::

Proxy imaš nastavljen na http (port 80), dostopaš pa preko https (port 443), za katerega si v apache configu nastavil, naj ti samo servira vsebino /var/www/example.com.

Verjetno niti nočeš servirat source-a node applikacije (torej ne rabiš directory sekcije)? Oziroma, če bo apache serviral statiko mimo node-a, ga usmeriš na mapo s statičnimi asseti, ne pa na na source root. Trenutno je verjetno node server source viden na https://www.example.com/hello.js

Če node server ni dosegljiv od zunaj, je brez veza da ga imaš na https, apache ti terminira https iz klientov.

dunda ::

Hvala. Sem stvari naredil malo drugače.
https://www.kacnje.eu


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

apache virtual host in name serverji

Oddelek: Izdelava spletišč
8982 (670) čuhalev
»

Apache 2.4

Oddelek: Omrežja in internet
14965 (681) Mesar
»

Apache SSL težave

Oddelek: Programiranje
171950 (1402) hobbit
»

.htaccess rewrite za poddomeno

Oddelek: Izdelava spletišč
71374 (1294) alessio2
»

dve spletni strani??

Oddelek: Izdelava spletišč
312306 (1855) MRB0rYS

Več podobnih tem