basicAuth configprerender basicAuth config

855 Views Asked by At

I'm running a prerender server and everything is okay but now I want to set some security using basicAuth.

In my console, I have exported username a password

export BASIC_AUTH_USERNAME=hugo
export BASIC_AUTH_PASSWORD=boss

In my server.js, I have added this line:

server.use(prerender.basicAuth());

But the question is now, how do I configure my express server to call prerender with correct user/pass. I have this:

var prerender = require('prerender-node').set('prerenderServiceUrl', 'http://123.123.123.123:3000/');
prerender.set('protocol', 'https');

I don't find any docs, I'm expecting something like:

prerender.set('user', 'hugo');
prerender.set('pass', 'boss');
app.use(prerender);

Solution

Like often, solution is simple when you have clearly understood problem...

Instead of:

var prerender = require('prerender-node').set('prerenderServiceUrl', 'http://123.123.123.123:3000/');

Simply:

var prerender = require('prerender-node').set('prerenderServiceUrl', 'http://hugo:[email protected]:3000/');

But new problem

Exported environment variables (BASIC_AUTH_USERNAME and BASIC_AUTH_PASSWORD) are accessible when launching my script like so:

node server.js

But as I want them to run all the time, I do a:

forever start server.js

And in this case environment variables are no longer accessible... I will open a new question since it's related but not about first question!

But hopefully new solution

Even if I don't like it because I'd prefer setting my env variable only once, using forever like so did the trick:

BASIC_AUTH_USERNAME=hugo BASIC_AUTH_PASSWORD=boss forever start server.js

I have now moved on supervisor for running my Node.js background servers, here is my prerender section (note how environment variables are passed)

[program:prerender]
environment =
    BASIC_AUTH_USERNAME=hugo,
    BASIC_AUTH_PASSWORD=boss
command=bash -c "ulimit -n 10000;exec nodejs /home/hugo/prerender/server.js"
process_name=prerender
numprocs=1
autostart=true
autorestart=true
user=hugo
stdout_logfile=/home/hugo/supervisor-prerender-info.log
stdout_logfile_maxbytes=1MB
stderr_logfile=/home/hugo/supervisor-prerender-error.log
stderr_logfile_maxbytes=1MB

I had strange problem in .htaccess redirection since

RewriteRule ^(?!.*?(\.js|...|\.woff))(.*) http://hugo:[email protected]:3000/https://my-website.com/$2 [P,L]

Wasn't adding correct Basic Authorization request headers so prerender was rejecting snapshot (401), I had to activate Apache headers module (sudo a2enmod headers) and add this line above previous one:

RequestHeader set Authorization "Basic aHVnbzpib3Nz"

Code aHVnbzpib3Nz is generated by command line:

echo -n 'hugo:boss' | base64
1

There are 1 best solutions below

0
On

You can create a bash script, called init.sh:

export BASIC_AUTH_USERNAME=hugo 
export BASIC_AUTH_PASSWORD=boss
echo "Running prerender"
forever start server.js

Then give execution permissions:

chmod +x ./init.sh

An after that, you can run the script using ./init.sh or put the in an start up script (systemd, sysv) so it will be executed when computer boot.