Nginx学习日记第二篇 -- 虚拟主机,状态信息,访问认证

简介:

一、虚拟主机配置

 1、基于域名的虚拟主机

基于域名的虚拟主机就是通过不同的域名来区分提供web服务的主机  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
1.配置nginx配置文件
[root@Nginx conf] # vim server.conf
将nginx.conf配置文件中的server段取出创建了server.conf,在nginx.conf中include server.conf
[root@Nginx html] # grep -E -v "#|^$" ../conf/server.conf
     server {
     listen       80;
     server_name  xn2.51cto.com;
     location / {
         root  html /xn2 ;
         index  index.html;
     }
     }
     server {
         listen       80;
         server_name  xn3.51cto.com;
         location / {
             root  html /xn3 ;
             index  index.html;
         }
     }
     server {
         listen       80;
         server_name  xn1.51cto.com;
         location / {
             root   html /xn1 ;
             index  index.html index.htm;
         }
         error_page   500 502 503 504   /50x .html;
         location =  /50x .html {
             root   html;
         }
     }
     
2.创建虚拟主机目录及文件
[root@Nginx conf] # cd ../html/
[root@Nginx html] # mkdir -pv xn{1,2,3}
mkdir : 已创建目录  "xn1"
mkdir : 已创建目录  "xn2"
mkdir : 已创建目录  "xn3"
[root@Nginx html] # echo "This is xn1" >> xn1/index.html
[root@Nginx html] # echo "This is xn2" >> xn2/index.html
[root@Nginx html] # echo "This is xn3" >> xn3/index.html
[root@Nginx html] # cat xn1/index.html 
This is xn1
 
3.平滑重启nginx
[root@Nginx html] # 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@Nginx html] # nginx -s reload
 
4.修改hosts文件,并测试nginx虚拟主机
[root@Nginx html] # echo "192.168.0.110 xn1.51cto.com" >> /etc/hosts
[root@Nginx html] # echo "192.168.0.110 xn2.51cto.com" >> /etc/hosts
[root@Nginx html] # echo "192.168.0.110 xn3.51cto.com" >> /etc/hosts
[root@Nginx html] # curl xn1.51cto.com
This is xn1
[root@Nginx html] # curl xn2.51cto.com
This is xn2
[root@Nginx html] # curl xn3.51cto.com
This is xn3


 2、基于端口的虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
1.配置
[root@Nginx conf] # vim server.conf
[root@Nginx conf] # grep -E -v "#|^$" server.conf
     server {
     listen       8002;
     server_name  xn2.51cto.com;
     location / {
         root  html /xn2 ;
         index  index.html;
     }
     }
     server {
         listen       8003;
         server_name  xn3.51cto.com;
         location / {
             root  html /xn3 ;
             index  index.html;
         }
     }
     server {
         listen       80;
         server_name  xn1.51cto.com;
         location / {
             root   html /xn1 ;
             index  index.html index.htm;
         }
         error_page   500 502 503 504   /50x .html;
         location =  /50x .html {
             root   html;
         }
     }
2.测试
[root@Nginx 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@Nginx conf] # nginx -s reload
 
[root@Nginx conf] # curl 192.168.0.110:80
This is xn1
[root@Nginx conf] # curl 192.168.0.110:8002
This is xn2
[root@Nginx conf] # curl 192.168.0.110:8003
This is xn3


 3、基于IP的虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1.为网卡配置多个IP
[root@Nginx conf] # ip addr add 192.168.0.10/24 dev eth0
[root@Nginx conf] # ip addr add 192.168.0.20/24 dev eth0
[root@Nginx conf] # ip add | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
     inet 192.168.0.110 /24  brd 192.168.0.255 scope global eth0
     inet 192.168.0.10 /24  scope global secondary eth0
     inet 192.168.0.20 /24  scope global secondary eth0
之后  ping 测试IP连通性
 
2.配置Nginx
[root@Nginx conf] # grep -E -v "#|^$" server.conf
     server {
     listen       192.168.0.10:8002;
     server_name  xn2.51cto.com;
     location / {
         root  html /xn2 ;
         index  index.html;
     }
     }
     server {
         listen       192.168.0.20:8003;
         server_name  xn3.51cto.com;
         location / {
             root  html /xn3 ;
             index  index.html;
         }
     }
     server {
         listen       80;
         server_name  xn1.51cto.com;
         location / {
             root   html /xn1 ;
             index  index.html index.htm;
         }
         error_page   500 502 503 504   /50x .html;
         location =  /50x .html {
             root   html;
         }
     }
3.测试
[root@Nginx conf] # curl 192.168.0.10:8002
This is xn2
[root@Nginx conf] # curl 192.168.0.20:8003
This is xn3
[root@Nginx conf] # curl 192.168.0.110
This is xn1


基于域名,IP,端口可混用,具体使用是基于域名使用的较多。基于域名的虚拟主机可配置多个域名,中间以空格分开。

更多虚拟主机的配置详见官方站点:http://nginx.org/en/docs/http/request_processing.html


二、Nginx状态信息模块

 1、ngx_http_stub_status_module模块提供Nginx的基本访问状态信息,在编译时要加入--with-http_stub_status_module参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1.编辑配置文件
[root@Nginx conf] # grep -E -v "#|^$" server.conf
     server {
         listen       80;
         location / {
             root   html /xn1 ;
             index  index.html index.htm;
         }
     location =   /status  {
         stub_status on;
         access_log  off;
         allow 192.168.0.0 /24 ;
         deny all;
     }
         error_page   500 502 503 504   /50x .html;
         location =  /50x .html {
             root   html;
         }
     }
其中状态信息页访问不记录日志,并设置了可访问的IP段。
 
2.测试访问
[root@Nginx conf] # curl http://192.168.0.110/status
Active connections: 1 
server accepts handled requests
  30 30 35 
Reading: 0 Writing: 1 Waiting: 0

status显示结果详解:

    Active connections: 1 :活动连接数

    server accepts handled requests:处理的连接,创建的握手,总共处理多少次请求

    Reading:  读取客户端的信息数

    Writing:  返回给客户端的信息数

    Waiting:  开启keep-alive时,waiting = active - (reading + writing)

更多详细信息请看:http://nginx.org/en/docs/http/ngx_http_stub_status_module.html



三、Nginx访问认证

 Nginx访问认证为网站设置帐号密码,每次请求都要先认证帐号密码才可以访问网站。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1、配置文件
[root@Nginx conf] # grep -E -v "#|^$" server.conf
     server {
         listen       80;
         location / {
             root   html /xn1 ;
             index  index.html index.htm;
         }
     location =   /status  {
         stub_status on;
         access_log  off;
         allow 192.168.0.0 /24 ;
         deny all;
         auth_basic   "please imput user and password:" ;
         auth_basic_user_file   /usr/local/nginx/conf/htpasswd ;
     }
         error_page   500 502 503 504   /50x .html;
         location =  /50x .html {
             root   html;
         }
     }
Note:
     auth_basic:后跟提示信息
     auth_basic_user_file:帐号密码认证文件的路径
     auth_basic_user_file文件的格式为name:password ,password为加密的
     
2、准备认证文件
使用apache自带的htpasswd命令直接生成
[root@Nginx conf] # htpasswd -bc /usr/local/nginx/conf/htpasswd jym 123456
Adding password  for  user jym
[root@Nginx conf] # cat htpasswd 
jym:Rq5uy9jvgi.gc
[root@Nginx conf] # chmod 400 htpasswd 
[root@Nginx conf] # chown nginx htpasswd

 3、测试访问

wKiom1iXJkfQ1ZyJAABZrdPVHkg197.png-wh_50





wKioL1iXJlbiUtiIAAAetKQRUKE694.png-wh_50



更多详细信息请看:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html



本文转自 元婴期 51CTO博客,原文链接:http://blog.51cto.com/jiayimeng/1895069

相关文章
|
缓存 负载均衡 应用服务中间件
Nginx 学习
【10月更文挑战第17天】Nginx 是一款非常强大的工具,掌握它的使用和配置对于构建高性能、可靠的 Web 应用至关重要。随着技术的不断发展,Nginx 也在不断更新和完善,为我们提供更好的服务和支持。
393 59
|
安全 应用服务中间件 网络安全
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
974 60
|
安全 应用服务中间件 网络安全
配置Nginx反向代理实现SSL加密访问的步骤是什么?
我们可以成功地配置 Nginx 反向代理实现 SSL 加密访问,为用户提供更安全、可靠的网络服务。同时,在实际应用中,还需要根据具体情况进行进一步的优化和调整,以满足不同的需求。SSL 加密是网络安全的重要保障,合理配置和维护是确保系统安全稳定运行的关键。
990 60
|
应用服务中间件 Linux 网络安全
nginx安装部署ssl证书,同时支持http与https方式访问
为了使HTTP服务支持HTTPS访问,需生成并安装SSL证书,并确保Nginx支持SSL模块。首先,在`/usr/local/nginx`目录下生成RSA密钥、证书申请文件及自签名证书。接着,确认Nginx已安装SSL模块,若未安装则重新编译Nginx加入该模块。最后,编辑`nginx.conf`配置文件,启用并配置HTTPS服务器部分,指定证书路径和监听端口(如20000),保存后重启Nginx完成部署。
5371 8
|
负载均衡 应用服务中间件 Linux
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
这篇博客文章详细介绍了Nginx的下载、安装、配置以及使用,包括正向代理、反向代理、负载均衡、动静分离等高级功能,并通过具体实例讲解了如何进行配置。
788 5
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
|
监控 应用服务中间件 定位技术
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
1520 3
|
Web App开发 算法 应用服务中间件
nginx开启局域网https访问
【10月更文挑战第22天】为了调试WebRTC功能,需要在局域网内搭建HTTPS协议。具体步骤包括:在已部署Nginx和安装OpenSSL的环境中生成私钥、证书签名请求和自签名证书;将生成的文件放置到Nginx的证书目录并修改Nginx配置文件,最后重启Nginx服务。注意,自签名证书不受第三方机构认可,如需正式使用,需向CA申请签名。
1343 2
|
缓存 负载均衡 算法
nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇
Nginx 是一款高性能的 HTTP 和反向代理服务器,也是一个通用的 TCP/UDP 代理服务器,以及一个邮件代理服务器和通用的 HTTP 缓存服务器。
796 0
nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇
|
应用服务中间件 nginx
Nginx 服务器中设置禁止访问文件或目录的方法
Nginx 服务器中设置禁止访问文件或目录的方法
|
应用服务中间件 nginx