nginx: ограничить доступ ко всему с basic_auth, кроме указанной страницы c - PullRequest
0 голосов
/ 02 апреля 2020

Это исходная nginx конфигурация, которая у меня здесь, работает нормально:

  server {

    listen 8080; # http

    # Forward requests to our node app at port 8082
    #
    location /mui {
        # Remove the '/mui' portion of the path (and any extraneous trailing slash)
        rewrite ^/mui/?(.*)$    /$1;     break;
        proxy_pass http://localhost:8082;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location  / {
      # We also rewrite the Java servlet urls to move additional, 'RESTful' path elements
      # to a url query parameter named '_path_suffix'
      #
      rewrite ^/(.*)$    /server?_path_suffix=$1;     break;
      proxy_pass         http://localhost:8081;
      proxy_redirect     off;
    }
  }

Я хочу добавить аутентификацию basi c ко всему - ЗА ИСКЛЮЧЕНИЕМ для одной страницы ... /mui/river

Если в блок сервера включить строки аутентификации basi c и поместить блок auth_basic off внутри location /mui, он будет работать, как и ожидалось, для этой конфигурации (для аутентификации / требуется аутентификация, но не для /mui):


  server {

    listen 8080; # http

    auth_basic           "Restricted Area";
    auth_basic_user_file /etc/ngnix/.htpasswd;


    # Forward requests to our node app at port 8082
    #
    location /mui {
        # Remove the '/mui' portion of the path (and any extraneous trailing slash)
        rewrite ^/mui/?(.*)$    /$1;     break;
        proxy_pass http://localhost:8082;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        auth_basic off;
    }

    location  / {
      # We also rewrite the Java servlet urls to move additional, 'RESTful' path elements
      # to a url query parameter named '_path_suffix'
      #
      rewrite ^/(.*)$    /server?_path_suffix=$1;     break;
      proxy_pass         http://localhost:8081;
      proxy_redirect     off;
    }
  }

Почти идеально. Следующим шагом было бы сделать так, чтобы он запрашивал аутентификацию для всего внутри /mui, за исключением страницы /mui/river.

Вот в чем моя проблема ... Я попробовал следующее, и когда я достиг /mui/river этого все еще требует аутентификации ...

server {

    listen 8080; # http

    auth_basic           "Restricted Area";
    auth_basic_user_file /etc/ngnix/.htpasswd;



    location = /mui/river {
      rewrite ^/mui/?(.*)$    /$1;     break;
        proxy_pass http://localhost:8082;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        auth_basic off;
    }

    # Forward requests to our node app at port 8082
    #
    location /mui {
        # Remove the '/mui' portion of the path (and any extraneous trailing slash)
        rewrite ^/mui/?(.*)$    /$1;     break;
        proxy_pass http://localhost:8082;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location  / {
      # We also rewrite the Java servlet urls to move additional, 'RESTful' path elements
      # to a url query parameter named '_path_suffix'
      #
      rewrite ^/(.*)$    /server?_path_suffix=$1;     break;
      proxy_pass         http://localhost:8081;
      proxy_redirect     off;
    }
  }

Как я могу открыть доступ только для /mui/river?

Обновление: это моя последняя попытка, все еще не работает - все еще блокирует все. Обратите внимание, что я также попытался изменить строку перезаписи:

 server {
    listen 8080; # http

    # Forward requests to our node app at port 8082
    #

 location = /mui/river {
         rewrite ^/mui/river?(.*)$    /river$1;     break;
         auth_basic off;
         proxy_pass http://localhost:8082;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection 'upgrade';
         proxy_set_header Host $host;
         proxy_cache_bypass $http_upgrade;
    }

   location /mui {
        # Remove the '/mui' portion of the path (and any extraneous trailing slash)
        rewrite ^/mui/?(.*)$    /$1;     break;
        proxy_pass http://localhost:8082;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        auth_basic           "Restricted Area";
        auth_basic_user_file /etc/ngnix/.htpasswd;
    }

    location  / {
      # The Java servlet is always assumed to be named 'server', so add that to the path.
      #
      # We also rewrite the Java servlet urls to move additional, 'RESTful' path elements
      # to a url query parameter named '_path_suffix'
      #
      rewrite ^/(.*)$    /server?_path_suffix=$1;     break;
      proxy_pass         http://localhost:8081;
      proxy_redirect     off;
      auth_basic           "Restricted Area";
      auth_basic_user_file /etc/nginx/.htpasswd;
    }
  }

1 Ответ

0 голосов
/ 03 апреля 2020

Прямо сейчас ваш базовый c auth установлен на уровне сервера (внутри блока server {...}), поэтому он будет применяться к всем блокам местоположения.

Если вы хотите чтобы защитить все , за исключением /mui/river, переместите следующие 2 строки в location /mui {...} и location / {...} вас sh для защиты:

auth_basic "Restricted Area";
auth_basic_user_file /etc/ngnix/.htpasswd;

https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-http-basic-authentication/

...