.htaccess 와 userdir

개발새발·2021년 12월 12일
0

apache

목록 보기
3/4

로컬에서 basic auth 개발을 마치고, 개발서버로 배포를 했는데 Authroization 헤더와 같이 요청을 보내면 400 에러가 뜬다........ ㅠㅠ 진짜 이거 가지고 3일동안 고생했다.... 이 에러를 알기 위해서는 apache 서버의 .htaccess와 userdir파일에 대해 알아야했다.

🧐 userdir모듈?

apache 서버의 내부 사용자들이 외부에 공개하고자 하는 html문서를 저장하기 위한 용도로 사용되는 내부 사용자별 디렉토리

이 디렉토리에 접근을 허용할지 말지, 어떻게 보여줄지 등을 정하게 된다. <Directory>블럭을 통해 이것을 지정하게 된다.

🤔 .htaccess란?

htaccess 파일은 위 userdir에서 언급한 <Directory>블럭과 똑같은 역할을 한다.<Directory>블럭을 통한 서버의 전역 설정이 있는 상태에서 .htaccess 파일을 사용하면 .htaccess가 위치한 디렉토리에 대한 허용 규칙을 오버라이드(덮어쓰게)하게 된다.

<Directory>블럭과 같은 역할을 한다면, .htaccess 는 왜쓸까? .htaccess의 용도는 아래와 같다.

허가, 인증, URL재작성, 스팸봇 차단, SSL, 디렉토리 리스팅, 에러 응답 커스터마이징 등

⭐ .htaccess에서 인증 사용한 방법

1. .htaccess 파일

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder
    # - https://stackoverflow.com/a/28463209
    RewriteRule ^(.*)/$ /public/$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

2. userdir 파일

# Settings for user home directories
#
# Required module: mod_authz_core, mod_authz_host, mod_userdir

#
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.  Note that you must also set
# the default access control for these directories, as in the example below.
#
UserDir disabled

#
# Control access to UserDir directories.  The following is an example
# for a site where these directories are restricted to read-only.
#
<Directory "/home/*">
    AllowOverride None
    Options -ExecCGI -MultiViews -Indexes -FollowSymLinks -SymLinksIfOwnerMatch -Includes -IncludesNoExec
    <Limit GET POST OPTIONS PROPFIND PUT DELETE>
        Require all granted
    </Limit>
    <LimitExcept GET POST OPTIONS PROPFIND PUT DELETE>
        Require all granted
    </LimitExcept>
</Directory>

## Source Directory
#<Directory "/path/to/www">
#</Directory>
<Directory "/home/apps/www">
    # AllowOverride All
    # FileInfo, FollowSymLinks for .htcacess
    AllowOverride FileInfo
    Options +FollowSymLinks
    php_flag engine on
</Directory>

## Upload Directory
#<Directory "/path/to/upload">
#    AllowOverride None
#    php_flag engine off
#</Directory>

⇒ 나의 경우에는, 위에 #처리해둔 AllowOverride All 이 문제였다. All로 했을 때 보안적으로 좋지 않아 수정해달라는 요청이였는데, 저 부분은 None 으로 하면 .htaccess를 아예 무시해버렸다. 그리고 권유받은 Autoconfig 로 하게 되면 인증관련한 아이디, 비밀번호 파일을 둬야됐었다. 그런데 이게 아무리봐도 basic auth 인증관련한 파일을 두는 게 이상했다. 그래서 다른 분과 함께 찾아보니 FileInfo 로 하는 게 맞다는 결론을 내렸다.

이 부분에 AllowOverride All 이 문제였다. All로 했을 때 보안적으로 좋지 않아 수정해달라는 요청이였는데, 저 부분은 None 으로 하면 .htaccess를 아예 무시해버렸다. 그리고 권유받은 Autoconfig 로 하게 되면 인증관련한 아이디, 비밀번호 파일을 둬야됐었다. 그런데 이게 아무리봐도 basic auth 인증관련한 파일을 두는 게 이상했다. 그래서 다른 분과 함께 찾아보니 FileInfo 로 하는 게 맞다는 결론을 내렸다.

apache AllowOverride 관련 문서에 따르면, 아래와 같았다.

When this directive is set to None and AllowOverrideList is set to None, .htaccess files are completely ignored. In this case, the server will not even attempt to read .htaccess files in the filesystem.
When this directive is set to All, then any directive which has the .htaccess Context is allowed in .htaccess files.
The directive-type can be one of the following groupings of directives. (See the override class index for an up-to-date listing of which directives are enabled by each directive-type.)

AuthConfig
: Allow use of the authorization directives (AuthDBMGroupFile, AuthDBMUserFile, AuthGroupFile, AuthName, AuthType, AuthUserFile, Require, etc.).

FileInfo
: Allow use of the directives controlling document types (ErrorDocument, ForceType, LanguagePriority, SetHandler, SetInputFilter, SetOutputFilter, and mod_mime Add* and Remove* directives), document meta data (Header, RequestHeader, SetEnvIf, SetEnvIfNoCase, BrowserMatch, CookieExpires, CookieDomain, CookieStyle, CookieTracking, CookieName), mod_rewrite directives (RewriteEngine, RewriteOptions, RewriteBase, RewriteCond, RewriteRule), mod_alias directives (Redirect, RedirectTemp, RedirectPermanent, RedirectMatch), and Action from mod_actions.

나의 경우 .htaccess에서 Rewrite문들을 사용하여 인증 헤더를 세팅하고 있기 때문에 Rewrite문을 사용할 수 있는 FileInfo로 AllowOverride 세팅을 해줘야했다.

참고: https://httpd.apache.org/docs/2.4/mod/core.html 중 AllowOverride Directive

profile
발새발개

0개의 댓글