首页 > 其他分享 >5-Ngnix配置基于用户访问控制和IP的虚拟主机

5-Ngnix配置基于用户访问控制和IP的虚拟主机

时间:2023-07-28 11:33:52浏览次数:53  
标签:00 cent79 虚拟主机 IP Ngnix local nginx conf root

Nginx配置文件在/usr/local/nginx/conf下,文件名为nginx.conf

5.1.Ngnix配置基于用户访问控制的多虚拟主机

5.1.1.前提条件

Ngnix配置基于用户访问控制的多虚拟主机时,需要使用htpasswd命令,需要安装httpd-tools rpm

命令:

yum install -y httpd-tools

[root@cent79-2 ~]# yum install -y httpd-tools
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * centos-sclo-rh: mirrors.huaweicloud.com
 * centos-sclo-sclo: mirrors.huaweicloud.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package httpd-tools.x86_64 0:2.4.6-99.el7.centos.1 will be installed
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-tools-2.4.6-99.el7.centos.1.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-tools-2.4.6-99.el7.centos.1.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-7.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7_9.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================
 Package                  Arch                Version                             Repository            Size
=============================================================================================================
Installing:
 httpd-tools              x86_64              2.4.6-99.el7.centos.1               updates               94 k
Installing for dependencies:
 apr                      x86_64              1.4.8-7.el7                         CentOS7              104 k
 apr-util                 x86_64              1.5.2-6.el7_9.1                     updates               92 k

Transaction Summary
=============================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 290 k
Installed size: 584 k
Downloading packages:
(1/2): apr-util-1.5.2-6.el7_9.1.x86_64.rpm                                            |  92 kB  00:00:00     
(2/2): httpd-tools-2.4.6-99.el7.centos.1.x86_64.rpm                                   |  94 kB  00:00:00     
-------------------------------------------------------------------------------------------------------------
Total                                                                        356 kB/s | 290 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-7.el7.x86_64                                                                    1/3 
  Installing : apr-util-1.5.2-6.el7_9.1.x86_64                                                           2/3 
  Installing : httpd-tools-2.4.6-99.el7.centos.1.x86_64                                                  3/3 
  Verifying  : apr-util-1.5.2-6.el7_9.1.x86_64                                                           1/3 
  Verifying  : apr-1.4.8-7.el7.x86_64                                                                    2/3 
  Verifying  : httpd-tools-2.4.6-99.el7.centos.1.x86_64                                                  3/3 

Installed:
  httpd-tools.x86_64 0:2.4.6-99.el7.centos.1                                                                 

Dependency Installed:
  apr.x86_64 0:1.4.8-7.el7                         apr-util.x86_64 0:1.5.2-6.el7_9.1                        

Complete!
[root@cent79-2 ~]#

5.1.2.Ngnix配置基于用户访问控制的多虚拟主机

-->进入/usr/local/nginx/conf目录

命令:

cd /usr/local/nginx/conf

pwd

[root@cent79-2 conf]# cd /usr/local/nginx/conf/
[root@cent79-2 conf]# pwd
/usr/local/nginx/conf
[root@cent79-2 conf]#

-->编辑nginx.conf,并保存

命令:

vi nginx.conf

5-Ngnix配置基于用户访问控制和IP的虚拟主机_虚拟主机

--> 验证nginx.conf语法的正确性

命令:

nginx -t

[root@cent79-2 conf]# nginx -t
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
[root@cent79-2 conf]#

-->创建用户访问控制文件

命令:

htpasswd -c /usr/local/nginx/conf/nginxpasswd ztj

[root@cent79-2 conf]# htpasswd -c /usr/local/nginx/conf/nginxpasswd ztj
New password: 
Re-type new password: 
Adding password for user ztj
[root@cent79-2 conf]# cat /usr/local/nginx/conf/nginxpasswd
ztj:$apr1$bKLsHgJ3$X8j4Buj2KCw4ojcm3HuTX1
[root@cent79-2 conf]#

备注:

如果创建多个用户访问控制时,后续命令为:

htpasswd  /usr/local/nginx/conf/nginxpasswd USERNAME

-->重启nginx

命令:

nginx -s stop

nginx

[root@cent79-2 conf]# nginx -s stop
[root@cent79-2 conf]# ps -ef |grep nginx
root       1354   1245  0 15:39 pts/0    00:00:00 grep --color=auto nginx
[root@cent79-2 conf]# nginx
[root@cent79-2 conf]# ps -ef |grep nginx
root       1356      1  0 15:39 ?        00:00:00 nginx: master process nginx
nginx      1357   1356  0 15:39 ?        00:00:00 nginx: worker process
root       1359   1245  0 15:40 pts/0    00:00:00 grep --color=auto nginx
[root@cent79-2 conf]#

-->验证

http://www.itztj.com

5-Ngnix配置基于用户访问控制和IP的虚拟主机_nginx_02

输入通过htpasswd命令创建的用户控制文件的用户名和密码

5-Ngnix配置基于用户访问控制和IP的虚拟主机_centos_03

http://www.itxiaohei.com

5-Ngnix配置基于用户访问控制和IP的虚拟主机_虚拟主机_04

 输入通过htpasswd命令创建的用户控制文件的用户名和密码

5-Ngnix配置基于用户访问控制和IP的虚拟主机_centos_05

5.2.Ngnix配置基于IP的虚拟主机

 -->进入/usr/local/nginx/conf目录

命令:

cd /usr/local/nginx/conf

pwd

[root@cent79-2 conf]# cd /usr/local/nginx/conf/
[root@cent79-2 conf]# pwd
/usr/local/nginx/conf
[root@cent79-2 conf]#

-->编辑nginx.conf,并保存

命令:

vi nginx.conf

5-Ngnix配置基于用户访问控制和IP的虚拟主机_虚拟主机_06

 --> 验证nginx.conf语法的正确性

命令:

nginx -t

[root@cent79-2 conf]# nginx -t
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
[root@cent79-2 conf]#

-->重新加载nginx

命令:

nginx -s reload

[root@cent79-2 conf]# nginx -s reload
[root@cent79-2 conf]# ps -ef |grep nginx
root       1591      1  0 16:04 ?        00:00:00 nginx: master process nginx
nginx      1594   1591  0 16:04 ?        00:00:00 nginx: worker process
root       1596   1182  0 16:05 pts/0    00:00:00 grep --color=auto nginx
[root@cent79-2 conf]#

-->验证

curl 192.168.10.156

[root@cent79-2 conf]# curl 192.168.10.156
I am itztj
[root@cent79-2 conf]#

宿主机验证

5-Ngnix配置基于用户访问控制和IP的虚拟主机_centos_07

5.3.Ngnix配置基于IP的多虚拟主机

 -->进入/usr/local/nginx/conf目录

命令:

cd /usr/local/nginx/conf

pwd

[root@cent79-2 conf]# cd /usr/local/nginx/conf/
[root@cent79-2 conf]# pwd
/usr/local/nginx/conf
[root@cent79-2 conf]#

-->编辑nginx.conf,并保存

命令:

vi nginx.conf

5-Ngnix配置基于用户访问控制和IP的虚拟主机_centos_08

 --> 验证nginx.conf语法的正确性

命令:

nginx -t

[root@cent79-2 conf]# nginx -t
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
[root@cent79-2 conf]#

-->重新加载nginx

命令:

nginx -s reload

[root@cent79-2 conf]# nginx -s reload
[root@cent79-2 conf]# ps -ef |grep nginx
root       1591      1  0 16:04 ?        00:00:00 nginx: master process nginx
nginx      1594   1591  0 16:04 ?        00:00:00 nginx: worker process
root       1596   1182  0 16:05 pts/0    00:00:00 grep --color=auto nginx
[root@cent79-2 conf]#

 -->验证

5-Ngnix配置基于用户访问控制和IP的虚拟主机_centos_09

5-Ngnix配置基于用户访问控制和IP的虚拟主机_centos_10

标签:00,cent79,虚拟主机,IP,Ngnix,local,nginx,conf,root
From: https://blog.51cto.com/ztj1216/6879898

相关文章

  • pip 常用操作
    1.更改源 豆瓣:https://pypi.doubanio.com/simple/ 清华:https://pypi.tuna.tsinghua.edu.cn/simplepipconfigsetglobal.index-url源地址2.安装包pipinstallSomePackage#最新版本pipinstallSomePackage==1.0.4#指定版本pipinstall'So......
  • JavaScript中this关键字详解(二)
    使用注意点避免多层this由于this的指向是不确定的,所以切勿在函数中包含多层的this。varo={f1:function(){console.log(this);//objectvarf2=function(){console.log(this);//window}();}}​o.f1()//Object//Window上面代码......
  • JavaScript中this关键字详解(一)
    涵义this关键字是一个非常重要的语法点。毫不夸张地说,不理解它的含义,大部分开发任务都无法完成。this可以用在构造函数之中,表示实例对象。除此之外,this还可以用在别的场合。但不管是什么场合,this都有一个共同点:它总是返回一个对象。简单说,this就是属性或方法“当前”所在的对象......
  • Centos7如何配置IPADDR,NETMASK,GATEWAY?
    1、获取IPADDR、NETMASK:[root@192network-scripts]#ifconfigens33:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500inet192.168.85.139netmask255.255.255.0broadcast192.168.85.255inet6fe80::11df:c601:5b38:ca41prefixlen64s......
  • 15款很棒的 JavaScript 开发工具
    [url][/url]在开发中,借助得力的工具可以事半功倍。今天,这篇文章向大家分享最新收集的15款非常有用的JavaScript开发工具。[color=red]TestSwarm:Continious&DistributedJSTesting[/color][url]http://www.webresourcesdepot.com/testswarm-contini......
  • linux ip获取方式:DHCP | 静态ip
    本文说明目的:说明:登陆后,通过界面的来设置自动获取ip,特点:linux启动后会自动获取IP缺点是每次自动获取的ip地址可能不一样。一、DHCP获取说明:登陆后,通过界面的来设置自动获取ip特点:linux启动后会自动获取IP缺点是每次自动获取的ip地址可能不一样。二、静态ip直接修改配......
  • linux静态ip | 配置vmnet8的ip
    摘要目的:linux虚拟机固定ip,不要每次登录都由DHCP分配设置vmnet的子网ip本文是同时更改了vmnet8的ip,可以更好地理解虚拟机ip与vmnet8的联系,如果不想该vmnet8的话,可以参考这篇博客一、要求要求:将linux的ip地址配置为192.168.200.130二、步骤该步骤配置了vmnet8的ip信......
  • 设置ip和主机名映射:linux配置hosts文件 | windows配置hosts文件
    摘要目的:Windows下主机名和ip的映射关系linux设置主机名和ip的映射关系一、Windows设置主机名和ip映射修改文件C:\Windows\System32\drivers\etc\hosts,添加ip和主机名192.168.200.130CentOS192.168.10.100hadoop100192.168.10.101hadoop101192.168.10.102hadoop1......
  • 查看虚拟机网络ip信息:vmnet8的ip
    摘要目的:查看虚拟机虚拟网络的ip,网关等(也可以设置)查看/设置主机虚拟网络1.VMWare查看首先点击虚拟网络编辑器然后点击更改设置然后就可以设置了虚拟网络的子网了点击DHCP设置有起始和结束的IP设置设置网关如下当然也可以用另一种方式查看2.主机查看查......
  • linux压缩和解压缩:gzip gunzip | zip unzip | tar指令
    摘要目的:介绍压缩和解压缩的指令:gzip和gunzip,用于文件的压缩和解压缩zip和unzip,用于压缩/解压缩文件和文件夹,在项目打包时有用tar指令指令1.gzip和gunzip指令功能说明选项gziphello.txt压缩文件只能压缩文件,不能压缩文件夹gunzip文件.gz解压文件......