1

I've always used Apache 2 for authentication, now I'm using nginx, I have no clue how to include authentication for nginx, do I still use the .htaccess file?

I want to use the authentication module on /var/www/priv directory, how can I use nginx for other modules such as rewrite, I'm asking HOW you use nginx, a typical file with rules of such?

My nginx configuration files contains:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

        server {
            listen 127.0.0.1;
            server_name localhost;

            # this gives me errors "Restarting nginx: [emerg]: unknown log format "main" in /etc/nginx/nginx.conf:61"        

            #access_log /var/log/nginx/localhost.access_log main;
            #error_log /var/log/nginx/localhost.error_log info;

            root /var/www;

            location /priv {
                    auth_basic            "Restricted";
                    auth_basic_user_file  authfile;
                }
            }

}

#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

1 Answer 1

5

Edit your nginx.conf file, and insert the belows in http context:

server {
    listen 80;
    server_name domain.com;

    access_log /var/log/nginx/localhost.access_log main;
    error_log /var/log/nginx/localhost.error_log info;

    root /var/www;

    location /priv {
            auth_basic            "Restricted";
            auth_basic_user_file  authfile;
        }
    }

The authfile is created by htpasswd:

htpasswd -c -m authfile <user>
9
  • I've added that, now when I access to domain.com/priv I don't get any prompts?
    – MacMac
    Sep 21, 2011 at 11:03
  • I've tried restarting nginx, and I get this: Restarting nginx: [emerg]: unknown directive "location" in /etc/nginx/nginx.conf:80 configuration file /etc/nginx/nginx.conf test failed
    – MacMac
    Sep 21, 2011 at 11:04
  • Post the full configuration file.
    – quanta
    Sep 21, 2011 at 12:57
  • Updated my post.
    – MacMac
    Sep 21, 2011 at 13:34
  • You must place location section in server context. Please check out my above updated answer.
    – quanta
    Sep 21, 2011 at 15:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .