Apache 配置

一般配置文件位置:

  • Debian/Ubuntu: /etc/apache2/sites-available/000-default.conf
  • CentOS/RHEL: /etc/httpd/conf.d/

启动/重启命令:

  • Debian/Ubuntu: sudo systemctl restart apache2
  • CentOS/RHEL: sudo systemctl restart httpd

示例配置

1
2
3
4
5
6
7
8
9
10
11
12
13
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html

<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

ErrorLog /var/log/httpd/html_error.log
CustomLog /var/log/httpd/html_access.log combined
</VirtualHost>

说明:

  • ServerName:指定主机名。
  • DocumentRoot:指定网站根目录。
  • <Directory>:设置对该目录的访问规则。

其他

禁用默认欢迎页面

1
2
3
4
5
# CentOS手动禁用默认配置文件或者删除欢迎页面配置
sudo mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.disabled

# Ubuntu禁用默认站点
sudo a2dissite 000-default.conf

防止泄漏Apache版本信息

新增一个安全配置文件security.conf

1
2
ServerTokens Prod
ServerSignature Off

检查配置情况

1
apachectl configtest

Nginx 配置

配置文件位置:

  • Debian/Ubuntu: /etc/nginx/sites-available/default
  • CentOS/RHEL: /etc/nginx/nginx.conf

启动/重启命令:

  • Debian/Ubuntu/CentOS/RHEL 通用:sudo systemctl restart nginx

示例配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80;
server_name www.example.com;

root /var/www/html;
index index.html index.php;

location / {
try_files $uri $uri/ =404;
}

error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}

说明:

  • listen 80:监听80端口。
  • server_name:指定主机名。
  • root:指定网站根目录。
  • try_files:检查请求是否为实际存在的文件,否则显示404。

仅允许域名访问

nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 先处理非法 Host
server {
listen 80;
server_name _;
return 404;
}

# 只允许指定的域名
server {
listen 80;
server_name www.example.com;

root /var/www/html;
index index.html;

location / {
try_files $uri $uri/ =404;
}
}

说明:

  1. 第一个 server_name _ 区块匹配所有没有匹配到指定 server_name 的请求,直接 404
  2. 第二个只允许 www.example.com

apache

1
2
3
4
5
6
7
8
9
10
11
12
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html
</VirtualHost>

# 拒绝其他请求
<VirtualHost *:80>
ServerName _
<Location />
Require all denied
</Location>
</VirtualHost>

说明:

  1. 第一个配置只允许指定的 www.example.com
  2. 第二个默认匹配其他 Host 请求,并 Require all denied

仅允许域名访问

nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 默认 Server:处理非法请求
server {
listen 80;
server_name _;

# 对没有匹配到的 Host 返回 404
return 404;
}

# 正确的域名配置
server {
listen 80;
server_name www.example.com;

root /var/www/html;
index index.html;

location / {
try_files $uri $uri/ =404;
}
}

说明:

  1. 非法处理请求
    • 不通过域名访问的处置方式
  2. 正确的域名配置
    • 设置可访问的域名

apache

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 默认虚拟主机配置
<VirtualHost *:80>
ServerName _default_

# 返回403禁止访问
<Location />
Require all denied
</Location>
</VirtualHost>

<VirtualHost *:80>
ServerName www.local.com
ServerAlias www.local.com

DocumentRoot /usr/share/nginx/html
<Directory /usr/share/nginx/html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog /var/log/httpd/html_error.log
CustomLog /var/log/httpd/html_access.log combined
</VirtualHost>

除了拒绝访问,还可以重定向到你的网页

1
2
3
<Location />
Redirect permanent / http://www.example.com/
</Location>

说明:(和nginx相似)

  1. 默认虚拟主机放在第一个加载的配置文件中,匹配未指定域名的请求,比如直接访问IP
  2. 正确的域名配置