내 서버에서 `/` 및 `/index.php`에 대한 액세스를 제한하고 다른 것은 허용하지 않으려면 어떻게 해야 합니까?

내 서버에서 `/` 및 `/index.php`에 대한 액세스를 제한하고 다른 것은 허용하지 않으려면 어떻게 해야 합니까?

사용자가 내 서버에서만 방문할 수 있도록 허용하고 싶습니다 index.php( /유효한 경우 403을 반환하고 유효하지 않은 파일인 경우 404를 반환).

어떻게 해야 하나요? 나는 다음 해결책을 시도했습니다 index.html apache를 제외한 모든 파일에 대한 액세스 거부

index.php를 제외한 모든 파일에 대한 접근을 거부하고 htaccess 파일의 "/"를 통한 접근을 허용합니다.

하지만 아무런 효과가 없는 것 같아요.

내 .htaccess 파일은 다음과 같지만 효과가 없으며 내 서버의 다른 파일에 계속 액세스할 수 있습니다.

Order allow,deny
Deny from all
<FilesMatch index\.php>
        Allow from all
</FilesMatch>

내 virtualhosts 구성에서도 무엇인가를 정의해야 하는지 잘 모르겠습니다.

내 가상 호스트 파일은 다음과 같습니다

ServerAdmin webmaster@localhost
ServerName server.mydomain.com
ServerAlias server.mydomain.com
DocumentRoot /var/www/server.mydomain.com
DirectoryIndex index.php
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

SSLCertificateFile /etc/letsencrypt/live/server.mydomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/server.mydomain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf

apache2ctl -S 출력은 다음과 같습니다.

VirtualHost configuration:
*:443                  server.mydomain.com (/etc/apache2/sites-enabled/server.mydomain.com-le-ssl.conf:2)
*:80                   server.mydomain.com (/etc/apache2/sites-enabled/server.mydomain.com.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
Mutex ssl-stapling-refresh: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

답변1

IMHO 당신은 일을 잘못된 방향으로 진행하고 있으며 완전히 피할 수 있는 문제를 해결하려고 노력하고 있습니다.

DocumentRoot에 온라인에 속하지 않는 파일과 디렉터리를 저장하지 마세요.

디렉토리의 내용을 /var/www/server.example.com해당 파일로만 제한하면 index.php잘못될 수 없습니다.


이 질문에는 역사적 태그가 지정되었습니다.버전 및 구문은 현재 Apache httpd 2.4 릴리스에 적합하지 않습니다. - 보다https://httpd.apache.org/docs/2.4/upgrading.html


http://www.example.com/some-path/ 사용 중인 콘텐츠를 게시하고 싶을 때Alias하위 디렉터리를 생성하는 대신 /var/www/server.example.com/some-path/ (예: create /var/www//some-path) 다른 디렉터리를 모두 노출하는 지시어를 사용하고 다음을 사용합니다.

<VirtualHost *:80
    ServerAdmin webmaster@localhost
    ServerName server.mydomain.com
    ServerAlias server.mydomain.com
    DocumentRoot /var/www/server.example.com

    Alias /some-path "/var/www/some-path"

    <Directory "/var/www/some-path"> 
       # add settings here, for example
       Order deny,allow
       Deny from all
       Allow From 127.0.0.1
    </Directory>
</VirtualHost>

관련 정보