Mastering Nginx

(Ron) #1

The NGINX HTTP Server


[ 134 ]

deny all;

}

This configuration will allow access to the /stats URI from the localhost only.


To restrict access to authenticated users, the auth_basic and auth_basic_user_file


directives are used as follows:


server {

server_name restricted.example.com;

auth_basic "restricted";

auth_basic_user_file conf/htpasswd;

}

Any user wanting to access restricted.example.com would need to provide


credentials matching those in the htpasswd file located in the conf directory


of NGINX's root. The entries in the htpasswd file can be generated using any
available tool that uses the standard UNIX crypt() function. For example,


the following Ruby script will generate a file of the appropriate format:


#!/usr/bin/env ruby

# setup the command-line options
require 'optparse'

OptionParser.new do |o|

o.on('-f FILE') { |file| $file = file }

o.on('-u', "--username USER") { |u| $user = u }

o.on('-p', "--password PASS") { |p| $pass = p }

o.on('-c', "--comment COMM (optional)") { |c| $comm = c }

o.on('-h') { puts o; exit }

o.parse!

if $user.nil? or $pass.nil?
Free download pdf