本文介绍了预渲染basicAuth配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 prerender服务器,一切正常,但现在我想使用 basicAuth .

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

在我的server.js中,我添加了这一行:

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

server.use(prerender.basicAuth());

但是现在的问题是,如何配置我的快速服务器以正确的用户/密码调用prerender.我有这个:

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

解决方案

通常,当您清楚地了解问题时,解决方案很简单...

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

而不是:

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

简单地:

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

带来新问题

像这样启动脚本时,可以访问导出的环境变量(BASIC_AUTH_USERNAME和BASIC_AUTH_PASSWORD):

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 !

提供了全新的解决方案

即使我不喜欢它,因为我更喜欢只设置一次我的env变量,就像这样永远使用它:

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更新

我现在已经在 supervisor 上运行了我的nodejs后台服务器,这是我的prerender部分(请注意环境变量的方式)通过)

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

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

没有添加正确的基本Auth请求标头,因此prerender拒绝快照(401),我不得不激活Apache标头模块(sudo a2enmod标头)并将此行添加到上一个:

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"

代码aHVnbzpib3Nz由命令行生成:

Code aHVnbzpib3Nz is generated by command line :

echo -n 'hugo:boss' | base64

推荐答案

您可以创建一个名为init.sh的bash脚本:

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

然后授予执行权限:

chmod +x ./init.sh

此后,您可以使用./init.sh运行脚本,或将其放入启动脚本(systemd,sysv)中,以便在计算机启动时执行该脚本.

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.

这篇关于预渲染basicAuth配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 05:26