Configure nginx to return different files to different authenticated users with the same URI

前端 未结 3 1097
被撕碎了的回忆
被撕碎了的回忆 2021-01-22 11:54

I\'m using nginx to serve static files in an embedded system, with no CGI back-end. I have basic-authentication up with multiple username/passwords. I\'d like to have a specific

3条回答
  •  耶瑟儿~
    2021-01-22 12:34

    First, there is variable $remote_user.

    I've end up with following structure:

    $ tree
    .
    ├── _auth
    │   ├── admin
    │   │   ├── f
    │   │   │   └── index.html
    │   │   ├── hello.html
    │   │   └── index.html
    │   └── user
    │       ├── f
    │       │   └── index.html
    │       └── index.html
    ├── f
    │   └── x.html
    ├── hello.html
    ├── test.html
    └── x
        └── index.html
    

    and this nginx config:

    auth_basic "Restricted area";
    auth_basic_user_file path/to/passwd/file;
    
    root /path/to/root;
    
    location / {
        try_files /_auth/$remote_user$uri
                  /_auth/$remote_user$uri/index.html
                  $uri $uri/index.html =404;
    }
    
    location /_auth/ {
        internal;
    }
    

    So request to / will end up in /_auth/USER/index.html, request to /test.html will serve /test.html. And request to /hello.html will serve /_auth/admin/hello.html for user admin and /hello.html for any other user.

    Direct access to /_auth/.. is forbidden by internal directive.

提交回复
热议问题