大网数据广告活动
服务器运维问答

Subdirectories configuration Apache2

· 52CE 我爱测

I am currently setting up a website on Ubuntu 18.04 with Apache2.4. What I am trying to do is restrict HTTP(S) access to my subdomains to certain IPs. Those subdomains are either used for development tests or are admin tools (php…

问题原文

原帖链接 · Amaury · CC BY-SA 4.0

I am currently setting up a website on Ubuntu 18.04 with Apache2.4.

What I am trying to do is restrict HTTP(S) access to my subdomains to certain IPs. Those subdomains are either used for development tests or are admin tools (phpMyAdmin, etc.), so I definitely don't want those subdomains to be public.
I also want domain.com to be public.

What is the current state of the website:

  • domain.com is publicly accessible
  • domain.com is showing /var/www/html/ content
  • sub1.domain.com is publicly accessible
  • sub1.domain.com is showing /var/www/html/sub1 content
  • sub2.domain.com is publicly accessible
  • sub2.domain.com is showing /var/www/html/sub2 content

What is the expected result:

  • domain.com is publicly accessible
  • sub1.domain.com is restricted to several IPs
  • sub2.domain.com is restricted to several IPs

This is my configuration so far:

DNS entries:

Name   Type   TTL  Target
        A     300  ServerIP
sub1   CNAME  300  domain.com
sub2   CNAME  300  domain.com
www    CNAME  300  domain.com

apache2.conf:

[...]
<Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

<Directory /var/www/html/sub1>
    AllowOverride All
    Require all denied
</Directory>
[...]

/etc/apache2/sites-enabled/000-default.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ServerName domain.com
    ServerAlias domain.com

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =domain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

/etc/apache2/sites-available/sub1.domain.com.conf

<VirtualHost *:80>
    ServerName sub1.domain.com
    DocumentRoot /var/www/html/sub1
    <Directory /var/www/html/sub1>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all denied
    </Directory>

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =sub1.domain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Symbolic links are made from /etc/apache2/sites-available/sub*.domain.com.conf to /etc/apache2/sites-enabled

Something I also don't understand is if I try to add a .htaccess file in /var/www/html/php56, it isn't taken into account, despite the AllowOverride All

Another thing I am trying to do, is to redirect domain.com/sub1 requests to sub1.domain.com instead of throwing a 403 error, but without a .htaccess file, so I minimize the number of config files.

Note: For testing purposes, I intentionally set Require all denied instead of Require ip x.x.x.x

What I am doing wrong?

If I forgot to provide any required info, please let me know.

Thanks for your help

UPDATE:

Thanks to Tom's advice, I figured out what was wrong. In my /etc/apache2/sites-available/sub1.domain.com.conf I had a redirection from HTTP to HTTPS. Actually, Let's Encrypt created a /etc/apache2/sites-available/sub1.domain.com-le-ssl.conf file (during the certificate setup, I chose to force HTTPS redirection), and in that file there were Require all granted so, that file was overwriting my sub1.domain.com.conf file.

Finally, I changed my directories setup according to Tom's answer - much clearer that way, thanks!
It also solved my redirection question, as now my root folder for domain.com is /var/www/domain.com/www

已采纳解答

原帖链接 · Tommiie · CC BY-SA 4.0

I would not make the root directory of the subdomain a subfolder of the root domain. Instead of:

/var/www/html/
    - sub1/
    - sub2/
    - sub3/

I would do it myself like this:

/var/www/domain.com/
    - www/ (the root)
    - sub1/
    - sub2/
    - sub3/

This makes it a lot easier to limit access to files. In your current setup, perhaps access is limited by a VirtualHost but the same data can be accessed by going via the root domain: http://www.example.com/sub1/data.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-11-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。