prerender basicAuth config

大兔子大兔子 提交于 2020-01-03 17:25:21

问题


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 an 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');

I would appreciate your help. Thanks and happy christmas :)

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:boss@123.123.123.123: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

2016/05/05 UPDATE

I have now moved on supervisor for running my nodejs 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:boss@123.123.123.123:3000/https://my-website.com/$2 [P,L]

wasn't adding correct Basic Auth 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:


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.



来源:https://stackoverflow.com/questions/27640331/prerender-basicauth-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!