4

I am writing a social networking site in C and serving it all up with Nginx. How can I make it so that authenticated users go to their own directory -ONLY- where a user-specific index.html resides. I am not asking how to populate the index.html with user specific directives, but how to lock them into their own directory

DisgruntledUser
  • 101
  • 2
  • 9

2 Answers2

6
map $remote_user $profile_directory {
    default      $remote_user;
    ''           guests;
    pavel        admins;
    ivan         admins;
}

server {

    location /profile/ {
        alias /path/to/www/$profile_directory/;
        ...
    }

}

second example (see comments):

server {
    location / {
        auth_basic            "Please Login";
        auth_basic_user_file  "/etc/nginx/htpasswd";
        root /var/www/sites/mysite.com/http/$remote_user;
    }
}
VBart
  • 8,309
  • 3
  • 25
  • 26
  • This looks very interesting. What kind of authentication options could I use with this? I assume that is where the $remote_user gets set, in the authentication module? – DisgruntledUser Jul 27 '12 at 23:34
  • Yes, it set by http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html – VBart Jul 27 '12 at 23:36
  • Thank you so much for your response. I would vote your answer up but I am a new user. – DisgruntledUser Jul 27 '12 at 23:57
  • I changed your answer only a little bit. Below is my whole bare bones nginx.conf that works as is. I will add back the rest of the configuration later, this is just to show the bare bones example – DisgruntledUser Aug 09 '12 at 19:13
  • Your "the bare bones example" is very bad. 1. `map $remote_user $profile_directory { default $remote_user; }` is just NOOP, that wasting CPU and memory. 2. `alias /var/www/sites/mysite.com/http/$profile_directory/;` is a NOOP too, you should use the `root` directive instead. – VBart Aug 27 '12 at 16:37
  • I am not sure what you mean. The two sections that you mentioned are not NOOPS. They work and if I pull either out it doesn't work. Can you show me an example of what you mean? – DisgruntledUser Aug 30 '12 at 22:00
  • In the (1) you assign value of the $remote_user variable to $profile_directory. Why? Variables in nginx isn't cheap, they evaluated each request. In this case you should just use $remote_user instead of introducing a new variable. In the (2) you use more expensive the `alias` directive instead of `root` (see docs: http://nginx.org/en/docs/http/ngx_http_core_module.html#alias ) – VBart Aug 31 '12 at 11:10
  • I've added new example. – VBart Aug 31 '12 at 11:16
  • The new example works great. Now If I could figure out how to do this with auth_request, I would be done. I have never really done cgi programming before. – DisgruntledUser Aug 31 '12 at 12:59
0
events {
}

http {

  map $remote_user $profile_directory {
    default      $remote_user;
  }
  server {
     root                 /var/www/sites/mysite.com/http;

    location / {
        auth_basic            "Please Login";
        auth_basic_user_file  "/etc/nginx/htpasswd";
        alias /var/www/sites/mysite.com/http/$profile_directory/;
    }
  }
}

This is my edited version, which includes my entire bare bones nginx.conf

DisgruntledUser
  • 101
  • 2
  • 9