Skip to content

Instantly share code, notes, and snippets.

@ffeast
Created September 5, 2017 16:53
Show Gist options
  • Save ffeast/d21bb4751cc994f3ba48300458496f9f to your computer and use it in GitHub Desktop.
Save ffeast/d21bb4751cc994f3ba48300458496f9f to your computer and use it in GitHub Desktop.
nginx version: nginx/1.4.6 (Ubuntu)
# 1. passwords
both password are ‘123’ passed through openssl passwd
# cat /etc/nginx/default.htpasswd
# default:ZE3exsFczUZUY
# cat /etc/nginx/test.htpasswd
# test:ZE3exsFczUZUY
# 2. config files
# /etc/nginx/nginx.conf
http {
auth_basic global;
auth_basic_user_file /etc/nginx/default.htpasswd;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
# /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name test.com;
auth_basic sitenamerealm;
auth_basic_user_file /etc/nginx/test.htpasswd;
location / {
add_header Content-Type text/plain;
root /usr/share/nginx/html;
}
}
# 3. hosts
grep test.com /etc/hosts
127.0.0.1 test.com
# 4. tests
curl -D - http://test.com
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 05 Sep 2017 16:01:36 GMT
Content-Type: text/html
Content-Length: 203
Connection: keep-alive
WWW-Authenticate: Basic realm="sitenamerealm"
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>
curl -D - http://test:123@test.com/
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 05 Sep 2017 16:09:40 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Mar 2014 11:46:45 GMT
Connection: keep-alive
ETag: "5315bd25-264"
Content-Type: text/plain
Accept-Ranges: bytes
<!DOCTYPE html>
<html>
...
curl -D - http://localhost
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 05 Sep 2017 16:01:43 GMT
Content-Type: text/html
Content-Length: 203
Connection: keep-alive
WWW-Authenticate: Basic realm="global"
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>
curl -D - http://default:123@localhost/
HTTP/1.1 200 OK
curl -D - http://test:123@localhost/
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 05 Sep 2017 16:11:07 GMT
Content-Type: text/html
Content-Length: 203
Connection: keep-alive
WWW-Authenticate: Basic realm="global"
<html>
<head><title>401 Authorization Required</title></head>
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment