Mastering Nginx

(Ron) #1

The NGINX HTTP Server


[ 136 ]

# if there's a comment, insert it
if $comm

$lines << "#{$user}:#{pass}:#{$comm}\n"

else

$lines << "#{$user}:#{pass}\n"

end

# write out the new file, creating it if necessary

File.open($file, File::RDWR|File::CREAT) do |f|

$lines.each { |l| f << l}

end

Save this file as http_auth_basic.rb and give it a filename (-f), a user (-u), and


a password (-p), and it will generate entries appropriate to use in NGINX's auth_


basic_user_file directive:


$ ./http_auth_basic.rb -f htpasswd -u testuser -p 123456


To handle scenarios where a username and password should only be entered if


not coming from a certain set of IP addresses, NGINX has the satisfy directive.
The any parameter is used here for this either/or scenario:


server {

server_name intranet.example.com;

location / {

auth_basic "intranet: please login";

auth_basic_user_file conf/htpasswd-intranet;

allow 192.168.40.0/24;

allow 192.168.50.0/24;

deny all;
Free download pdf