Nginx service binding domain
Nginx YUM to install an example:
-
Execute commands
vi /etc/nginx/nginx.conf
to edit Nginx configuration file, the defaultserver {...}
configuration modifications for the following:server {
listen 80 default_server;
server_name www.chinaobd2.com;
root /home/web1;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50.xhtml;
location = /50x.html {
}
}
server {
server_name www.abc.com;
root /home/web2;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
- When accessing www.chinaobd2.com, jump to / home / web1 directory.
- When accessing www.ecobd2.com, jump to / home / web2 directory.
-
Run
nginx -s reload
to restart the Nginx service.
Tomcat service binding domain
-
Execute commands
vi /etc/TOMCAT_HOME/conf/server.xml
to edit the Tomcat configuration file.The above is amended as follows:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoPlay="true">
<Value className="org.apache.catalina.valves.AccessLogValue" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %c "%r" %s %b" />
</Host>
<Host name="120.76.210.250" appBase="/data/wwwroot/web" unpackWARs="true" autoPlay="true">
<Content path="" docBase="/data/wwwroot/web" debug="0" reloadable="false" crossContext="true" />
<Value className="org.apache.catalina.valves.AccessLogValue" directory="logs" prefix="120.76.210.250_access_log." suffix=".txt" pattern="%h %l %u %c "%r" %s %b" />
</Host>
<Host name="www.abc.com" appBase="/data/wwwroot/default" unpackWARs="true" autoPlay="true">
<Content path="" docBase="/data/wwwroot/default" debug="0" reloadable="false" crossContext="true" />
<Value className="org.apache.catalina.valves.AccessLogValue" directory="logs" prefix="www.abc.com_access_log." suffix=".txt" pattern="%h %l %u %c "%r" %s %b" />
</Host>
- When accessing www.chinaobd2.com, jump to the default / data / wwwroot / default directory.
- 120.76.210.250 Access, the default jump to the / data / wwwroot / web directory.
-
Run
bin/shutdown
close Tomcat service, and then runbin/startup
to restart the Tomcat service.
Apache service binding domain
In a package configuration Apache key installation environment as an example:
-
Run
cd /alidata/server/httpd/conf/vhosts/
into the site profile directory. -
Execute commands
vi aa.conf
create a new profile, press the letter on the keyboard "I" key, copy and paste the following:Order allow,deny
Deny from all
DocumentRoot /alidata/www/test
ServerName www.chinaobd2.com
ServerAlias chinaobd2.com
ErrorLog "/alidata/log/httpd/test-error.log"
CustomLog "/alidata/log/httpd/test.log"
Note: You need to modify the corresponding log name to distinguish the different sites of log information.
-
Run
/alidata/server/httpd/bin/apachectl restart
to restart the Apache service.
Apache service configuration multi-domain Jump
Assume that the program directory is / var / www / html, web directory structure is as follows:
-
Open the Apache virtual hosts, virtual host configuration corresponding to the corresponding directory.
Achieve results:
- When accessed using a.example.com, actually accessing / var / www / html / a directory.
- 使用 b.example.com 访问时,实际是访问 /var/www/html/b 目录。
- 使用 c.example.com 访问时,实际是访问 /var/www/html/c 目录。
Apache 服务配置二级域名
前提条件:您有一个泛域名解析的顶级域名,例如: chinaobd2.com 。
- 在 httpd.conf 中打开 mod_rewrite 模块;
-
在 httpd.conf 的最后,添加以下内容:
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteMap vhost txt:/usr/local/etc/apache/vhost.map
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/$1
注意:/usr/local/etc/apache 是 Apache 服务配置文件所在路径,您需要根据实际情况修改。
-
执行命令
vi vhost.map
新建文件,复制并粘贴以下内容:www.chinaobd2.com /usr/local/www/data-dist/aliyun
blog.ecobd2.com /usr/local/www/data-dist/aliyunbbs
tui21.com /usr/local/www/data-dist/anyname
注意:请注意保持域名+空格+绝对路径的格式。
-
在根目录 /usr/local/www/data-dist 下创建对应目录:aliyun , bbs , any name 等目录。
- 通过浏览器访问 www.chinaobd2.com 时,实际上访问的就是 /usr/local/www/data-dist/aliyun 目录下的文件。
- 通过浏览器访问 blog.ecobd2.com 时,实际上访问的就是 /usr/local/www/data-dist/aliyunbbs 目录下的文件。
您可以通过修改 vhost.map links来增加、删除和修改您的二级域名和所指向的实际路径,不需要重启 Apache 服务。
Apache 禁止未经许可的域名访问 ECS 上的网站
ECS 实例上的网站被人恶意指向,例如,您的实例 IP 地址为 123.123.123.123,正常服务的域名为 www.chinaobd2.com , 恶意用户使用其他的域名 www.ecobd2.com , 指向 123.123.123.123,此时客户端访问 www.ecobd2.com 时会出现您的网站内容。
通过 Apache 的虚拟主机可以变通的解决这个问题。以如下场景为例:
Apache 版本号 | ECS 实例上的网站 |
---|---|
2.2.15 | https://www.chinaobd2.com/ https://www.ecobd2.com/ |
-
打开 Apache 的实际配置文件,如执行命令
vi /etc/httpd/conf/httpd.conf
打开 CentOS 的 Apache 配置文件,加入以下内容:-
加入代码
NameVirtualHost *:80
,告知 Apache 使用基于 host 名的虚拟主机功能: -
加入以下代码。
<ViretualHost *:80>
DocumentRoot /var/www/html/error/
ServerName *
ErrorLog logs/dummy-host.example.com-error_log
CustomeLog logs/dummy-host.example.com-access_log common
</ViretualHost>
注意:当客户端携带的 host 名不在之后设置的网站域名内时,会指向一个 403 错误页面告知用户域名非法,其中
DocumentRoot
是放置错误提示页面的目录,在下面可以放置一个简单的 html 页面提示用户正在访问非法域名。 -
加入以下代码。
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/another/
ServerName www.chinaobd2.com
ErrorLog logs/p1.huigher.cn-error_loh
CustomLog logs/p1.huigher.cn-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/
ServerName www.ukobd2.com
ErrorLog logs/t1.huigher.cn-error_loh
CustomLog logs/t1.huigher.cn-access_log common
</VirtualHost>
注意:这一步告知 Apache 合法的网站主机头,您需要根据实际情况修改这个代码块内容,如示例中的 www.ukobd2.com 和 www.ecobd2.com。
-
-
执行命令
/etc/httpd/bin/apachectl restart
重启 Apache 服务。
若您希望其他域名FVDI J2534访问您的网站时直接返回 403 错误:
-
修改第二步中的代码为以下形式:
<VirtualHost *:80>
DucumentRoot /var/www/html/error/
ServerName *
<Location>
Order Allow, Deny
Deny from all
</Location>
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log commom
</VirtualHost>
-
执行命令
/etc/httpd/bin/apachectl restart
重启 Apache 服务。