Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx gives an Internal Server Error 500 after I have configured basic auth

Tags:

nginx

I am trying to do basic auth on Nginx. I have version 1.9.3 up and running on Ubuntu 14.04 and it works fine with a simple html file.

Here is the html file:

<!DOCTYPE html> <html lang="en"> <head>   <meta charset="UTF-8">   <title></title> </head> <body>   "Some shoddy text" </body> </html> 

And here is my nginx.conf file:

user  nginx; worker_processes  1;  error_log  /var/log/nginx/error.log warn; pid        /var/run/nginx.pid;   events {     worker_connections  1024; }   http {     include       /etc/nginx/mime.types;     default_type  application/octet-stream;      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                       '$status $body_bytes_sent "$http_referer" '                       '"$http_user_agent" "$http_x_forwarded_for"';      access_log  /var/log/nginx/access.log  main;      sendfile        on;     #tcp_nopush     on;      keepalive_timeout  65;      #gzip  on;      include /etc/nginx/conf.d/*.conf;     server {         listen 80;         server_name 192.168.1.30;         location / {             root /www;             index index.html;             auth_basic "Restricted";             auth_basic_user_file /etc/users;         }     } } 

I used htpasswd to create two users in the "users" file under /etc (username "calvin" password "Calvin", and username "hobbes" password "Hobbes"). It's encrypted by looks like this:

calvin:$apr1$Q8LGMfGw$RbO.cG4R1riIfERU/175q0 hobbes:$apr1$M9KoUUhh$ayGd8bqqlN989ghWdTP4r/ 

All files belong to root:root. The server IP address is 192.168.1.30 and I am referencing that directly in the conf file.

It all works fine if I comment out the two auth lines and restart nginx, but if I uncomment them, then I do indeed get the username and password prompts when I try to load the site, but immediately thereafter get an Error 500 Internal Server error which seems to persist and I have to restart nginx.

Anybody can see what I'm doing wrong here? I had the same behaviour on the standard Ubuntu 14.04 apt-get version of Nginx (1.4.something) so I don't think it's the nginx version.

like image 980
Thomas Browne Avatar asked Aug 05 '15 13:08

Thomas Browne


People also ask

How do I fix a 500 Internal server error?

Clear your browser cache and cookies Check these articles on deleting the cache on an Android phone or iPhone, if you use a mobile device. Alternatively, you can test opening the page from another browser. For instance, if you use Chrome, try Firefox or vice versa.


2 Answers

Not really an answer to your question as you are using MD5. However as this thread pops up when searching for the error, I am attaching this to it.

Similar errors happen when bcrypt is used to generate passwords for auth_basic:

htpasswd -B <file> <user> <pass> 

Since bcrypt is not supported within auth_basic ATM, mysterious 500 errors can be found in nginx error.log, (usually found at /var/log/nginx/error.log), they look something like this:

*1 crypt_r() failed (22: Invalid argument), ...

At present the solution is to generate a new password using md5, which is the default anyway.

Edited to address md5 issues as brought up by @EricWolf in the comments:

md5 has its problems for sure, some context can be found in the following threads

  • Is md5 considered insecure?
  • Is md5 still considered secure for single use authentications?

Of the two, speed issue can be mitigated by using fail2ban, by banning on failed basic auth you'll make online brute forcing impractical (guide). You can also use long passwords to try and fortify a bit as suggested here.

Other than that it seems this is as good as it gets with nginx...

like image 134
Drazen Urch Avatar answered Sep 30 '22 06:09

Drazen Urch


I had goofed up when initially creating a user. As a result, the htpasswd file looked like:

user: user:$apr1$passwdhashpasswdhashpasswdhash... 

After deleting the blank user, everything worked fine.

like image 28
Andy Pippin Avatar answered Sep 30 '22 06:09

Andy Pippin