配置基于域名的虚拟主机;配置nginx基于用户和地址的访问控制;配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义

配置基于域名的虚拟主机 

网页内容所在目录:/usr/share/nginx/html/

配置文件:/etc/nginx/conf.d/

主配置文件:/etc/nginx/nginx.conf

①创建网页内容所在目录并定义网页内容

[[email protected] ~]# cd /usr/share/nginx/html/

[[email protected] html]# mkdir test{1,2}

[[email protected] html]# ls

50x.html  index.html  test1  test2

[[email protected] html]# echo test1 > test1/index.html

[[email protected] html]# echo test2 > test2/index.html

②编辑配置文件

[[email protected] html]# cd /etc/nginx/conf.d/

[[email protected] conf.d]# ls

default.conf

[[email protected] conf.d]# mv default.conf{,.bak}

[[email protected] conf.d]# vim vhost.conf

添加以下内容:

server {

        listen  80;

        server_name www.test1.com;

        location / {

            root   /usr/share/nginx/html/test1;

            index  index.html index.htm;

        }

}

 

server {

        listen  80;

        server_name www.test2.com;

        location / {

            root   /usr/share/nginx/html/test2;

            index  index.html index.htm;

        }

}

③重启服务

检查语法是否正确

[[email protected] conf.d]# nginx -t

[[email protected] conf.d]# systemctl restart nginx

④测试

在hosts文件中添加域名

[[email protected] nginx-1.18.0]# vim /etc/hosts

192.168.150.11 www.test1.com www.test2.com

访问测试

 

配置基于域名的虚拟主机;配置nginx基于用户和地址的访问控制;配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义

Windows访问nginx网页需要配置主机的hosts文件C:\Windows\System32\drivers\etc\hosts

 配置nginx基于用户和地址的访问控制

基于用户的访问控制

①创建账号密码, 此账号密码就是用户访问网站时需要输入的。

安装httpd-tools安装包用于加密使用

[[email protected] ~]# yum install httpd-tools

[[email protected] ~]# htpasswd -c  /opt/pwd tom

New password:

Re-type new password:

Adding password for user tom

②修改配置文件

[[email protected] ~]# vim /etc/nginx/conf.d/vhost.conf

server {

        listen  80;

        server_name www.test1.com;

        location / {

            root   /usr/share/nginx/html/test1/;

            index  index.html index.htm;

            auth_basic  "Restricted";

            auth_basic_user_file /opt/pwd;

        }

}

③重启服务

[[email protected] ~]# systemctl restart nginx

④测试

 

 

配置基于域名的虚拟主机;配置nginx基于用户和地址的访问控制;配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义

配置基于域名的虚拟主机;配置nginx基于用户和地址的访问控制;配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义

基于地址的访问控制

修改配置文件

[[email protected] ~]# vim /etc/nginx/conf.d/vhost.conf

server {

        listen  80;

        server_name www.test1.com;

        #access_log /var/log/nginx/access.log combined;

        location / {

            root   /usr/share/nginx/html/test1/;

            index  index.html index.htm;

            #autoindex on; 

deny 192.168.150.12;

            allow 192.168.150.0/24;

            deny all;

        }

}

重启服务

[[email protected] ~]# systemctl restart nginx

测试

未修改配置文件之前,测试结果如下:

 

配置基于域名的虚拟主机;配置nginx基于用户和地址的访问控制;配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义

修改之后测试结果如下:

配置基于域名的虚拟主机;配置nginx基于用户和地址的访问控制;配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义

 

 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义

①定义错误页面内容:

[[email protected] ~]# echo "the require failed" > /usr/share/nginx/html/test1/err.html

②编辑配置文件:

[[email protected] ~]# vim /etc/nginx/conf.d/vhost.conf

修改内容如下:

server {

        listen  80;

        server_name www.test1.com;

        location / {

            root   /usr/share/nginx/html/test1/;

            index  index.html index.htm;

            if (!-f $request_filename) {

                rewrite /.* err.html permanent;}

        }

}

③重启服务

[[email protected] ~]# systemctl restart nginx

④在浏览器进行测试

如果是基于域名访问

测试之前需要在主机的C:\Windows\System32\drivers\etc\hosts下添加192.168.150.11 www.test1.com www.test2.com

 

配置基于域名的虚拟主机;配置nginx基于用户和地址的访问控制;配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义

配置基于域名的虚拟主机;配置nginx基于用户和地址的访问控制;配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义