Ngingx自学手册(二)常用配置虚拟主机,访问控制,认证和状态信息

简介:

环境概况:

IP地址 服务器状态 简述
192.168.180.4 Nginx服务器
192.168.180.23
client
192.168.171.231 client


具体测试步骤如下:

(一)基于虚拟主机的配置。是通过不同的域名来区分提供的web服务器的主机,server_name指令主要用于配置基于域名的虚拟主机

    1,首先在192.168.180.23修改/etc/hosts文件

1
2
3
4
5
6
[root@localhost haproxy] # vim /etc/hosts
192.168.180.13  a.lqb.com
192.168.180.13  b.lqb.com
192.168.180.4   xn1.lqb.com
192.168.180.4   xn2.lqb.com
192.168.180.4   xn3.lqb.com

    2,修改nginx的配置文件。首先先把nginx.conf配置文件中的虚拟主机server段取出来,通过include导入进来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@Monitor conf] # cat nginx.conf
worker_processes  1;
user appuser appuser;
error_log   /data/nginx/error .log;
events {
     worker_connections  1024;
}
http {
     include       mime.types;
     default_type  application /octet-stream ;
     log_format  main   '$remote_addr - $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"' ;
    sendfile        on;
     access_log       /data/nginx/access .log;
     keepalive_timeout  65;
     gzip   on;
     server_tokens off;
    
     error_log   /data/nginx/error .log  debug;
     include server /server .conf   
  }

    3,接下来编辑配置虚拟主机段

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
[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
[root@Monitor conf] # cat server/server.conf 
server {
          listen    80; 
          server_name  xn1.lqb.com; 
          location =/ { 
                  root  /html/xn1
                  index index.html; 
                   
            }
server {                                                                                    
          listen    80;                                                                             
          server_name  xn2.lqb.com;                                                                 
          location =/ {                                                                             
                  root  /html/xn2 ;                                                                   
                  index index.html;                                                                 
                    }                                                                                
            }
server {
          listen    80; 
          server_name  xn3.lqb.com; 
          location =/ { 
                  root  /html/xn2
                  index index.html; 
                   
           
            }         
[root@Monitor conf] # /usr/local/nginx/sbin/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@Monitor conf] # /usr/local/nginx/sbin/nginx -s reload

   

  4,根据端口和域名的不同访问情况:

       a,端口和域名不能同时相同,如果相同的话会出现如下报错:“nginx: [warn] conflicting server name "xn1.lqb.com" on 0.0.0.0:80, ignored”,其实也是可以正常访问的,访问的结果是最上边的生效。

 server_name段的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
          listen    80;
          server_name  xn1.lqb.com;
          location / {
                  root  /html/xn1 ;
                  index index.html;
                    }
            }
server {
          listen    80;  
          server_name  xn1.lqb.com;
          location /  {
                  root  /html/xn2 ;
                  index index.html;
                    }
            }
1
2
3
4
[root@Monitor conf] # /usr/local/nginx/sbin/nginx -s reload
nginx: [warn] conflicting server name  "xn1.lqb.com"  on 0.0.0.0:80, ignored
[root@localhost ~] # curl xn1.lqb.com
Xn1 is this

        b,域名不同,但端口可以相同 .是正确的配置(基于域名的虚拟主机)

server_name段的配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
          listen    80;
          server_name  xn1.lqb.com;
          location / {
                  root  /html/xn1 ;
                  index index.html;
                    }
            }
server {
          listen    80;
          server_name  xn2.lqb.com;
          location /  {
                  root  /html/xn2 ;
                  index index.html;
                    }
            }

访问结果如下:

1
2
3
4
[root@localhost ~] # curl xn1.lqb.com
Xn1 is this
[root@localhost ~] # curl xn2.lqb.com
Xn2 is this

   c,域名相同,端口号不同,访问的路径也是不同的(基于端口号的虚拟主机)

server_name段的配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
          listen    80;
          server_name  xn1.lqb.com;
          location / {
                  root  /html/xn1 ;
                  index index.html;
                    }
            }
server {
          listen    8080;
          server_name  xn1.lqb.com;
          location /  {
                  root  /html/xn2 ;
                  index index.html;
                    }
            }

访问结果如下;

1
2
3
4
[root@localhost ~] # curl xn2.lqb.com
Xn1 is this
[root@localhost ~] # curl xn2.lqb.com:8080
Xn2 is this

   d,基于IP的端口访问。以在一块物理网卡上绑定多个lP地址。这样就能够在使用单一网卡的同一个服务器上运行多个基于IP的虚拟主机。设置IP别名也非常容易,只须配置系统上的网络接口,让它监听额外的lP地址。

1
2
3
4
5
6
7
[root@Monitor conf] # ip addr add 192.168.0.10/24 dev eth0
[root@Monitor conf] # ip addr add 192.168.0.20/24 dev eth0 
[root@Monitor conf] # ip add |grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
     inet 192.168.180.4 /24  brd 192.168.180.255 scope global eth0
     inet 192.168.0.10 /24  scope global eth0
     inet 192.168.0.20 /24  scope global secondary eth0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
          listen    192.168.0.10:8001;
          server_name  xn1.lqb.com;
          location / {
                  root  /html/xn1 ;
                  index index.html;
                    }
            }
server {
          listen    192.168.0.20:8080;
          server_name  xn2.lqb.com;
          location /  {
                  root  /html/xn2 ;
                  index index.html;
                    }
            }
server {
          listen    80;
          server_name  xn3.lqb.com;
          location / {
                  root  /html/xn3 ;
                  index index.html;
                    }

 测试,基于IP地址的访问需要重启nginx服务,重新加载时无法生效的

1
2
3
4
5
6
7
8
9
10
[root@Monitor ~] # netstat -lntp|grep nginx
tcp        0      0 192.168.0.10:8001           0.0.0.0:*                   LISTEN      2432 /nginx          
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      2432 /nginx          
tcp        0      0 192.168.0.20:8080           0.0.0.0:*                   LISTEN      2432 /nginx     
[root@Monitor ~] # curl 192.168.0.10:8001  
Xn1 is this
[root@Monitor ~] # curl 192.168.0.20:8080
Xn2 is this
[root@Monitor ~] # curl 192.168.180.4
Xn3 is this


 (二)IP访问控制。通过deny和allow设置访问控制,通过without-http_access_module模块来实现的

语法:

Syntax: allow address | CIDR | unix: | all;
Default:
Context: httpserverlocationlimit_except


Syntax: deny address | CIDR | unix: | all;
Default:
Context: httpserverlocationlimit_except


eg:配置信息如下:只允许192.168.180.23访问,其他的都禁止访问

1
2
3
4
5
6
7
8
9
10
server {
          listen    80;
          server_name  xn1.lqb.com;
          location / {
                  root  /html/xn1 ;
                  index index.html;
                  allow 192.168.180.23;
                  deny all;
                    }
            }

在180.4访问结果

1
2
3
4
5
6
7
8
[root@Monitor ~] # curl  xn1.lqb.com       
<html>
< head ><title>403 Forbidden< /title >< /head >
<body bgcolor= "white" >
<center><h1>403 Forbidden< /h1 >< /center >
<hr><center>nginx< /center >
< /body >
< /html >

在windows客户端访问如下:

wKioL1l-6QKx0ux4AABVcnDnzQE024.png

180.23访问如下

1
2
[root@localhost ~] # curl xn1.lqb.com
Xn1 is this

nginx日志如下:

1
2
3
192.168.181.231 - - [31 /Jul/2017 :16:25:40 +0800]  "GET / HTTP/1.1"  403 192  "-"  "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
192.168.180.23 - - [31 /Jul/2017 :16:27:06 +0800]  "GET / HTTP/1.1"  200 12  "-"  "curl/7.29.0"
192.168.180.4 - - [31 /Jul/2017 :16:27:54 +0800]  "GET / HTTP/1.1"  403 162  "-"  "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"


 只拒绝IP地址192.168.180.23访问,其他的都是可以访问的:

1
2
3
4
5
6
7
8
9
10
server {
          listen    80;
          server_name  xn1.lqb.com;
          location / {
                  root  /html/xn1 ;
                  index index.html;
                  deny 192.168.180.23;
                  allow  all;
                    }
            }

具体的nginx访问日志如下:

1
2
3
192.168.180.23 - - [31 /Jul/2017 :16:29:51 +0800]  "GET / HTTP/1.1"  403 162  "-"  "curl/7.29.0"
192.168.180.4 - - [31 /Jul/2017 :16:29:57 +0800]  "GET / HTTP/1.1"  200 12  "-"  "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
192.168.181.231 - - [31 /Jul/2017 :16:30:03 +0800]  "GET / HTTP/1.1"  200 12  "-"  "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"

备注:如果有很多IP地址需要拒绝,可以通过include deny.ip; 然后新建deny.ip文件,把所有的IP放到该文件里,就可以实现批量拒绝了


(三)nginx访问认证。让用户通过输入用户名和密码认证才可以访问web页面。

 1,通过htpasswd生成用户名及对应的密码数据库文件。

1
2
3
4
5
[root@Monitor conf] # htpasswd -bc /usr/local/nginx/conf/passwd yz 123456
Adding password  for  user yz
[root@Monitor conf] # more passwd 
yz:C9qDroTFbuldY
[root@Monitor conf] # chmod 400 passwd

 2,配置虚拟主机的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
server {
          listen    80;
          server_name  xn1.lqb.com;
          location / {
                  auth_basic  "please input you username and password" ;    ####虚拟主机的认证名称
                  auth_basic_user_file  /usr/local/nginx/conf/passwd ;       ###虚拟主机的认证文件
                  root  /html/xn1 ;
                  index index.html;
                  deny 192.168.180.23;
                  allow  all;
                    }
            }

 3,测试访问

wKioL1l-8Fji-zK8AAFg1oso88E602.png4,访问结果

wKioL1l-8HmjRkxqAABNqxrLJEg038.png


本文转自 lqbyz 51CTO博客,原文链接:http://blog.51cto.com/liqingbiao/1952421


相关实践学习
云安全基础课 - 访问控制概述
课程大纲 课程目标和内容介绍视频时长 访问控制概述视频时长 身份标识和认证技术视频时长 授权机制视频时长 访问控制的常见攻击视频时长
相关文章
|
8月前
|
安全 Apache PHP
Apache配置----访问控制,禁止解析php
Apache配置----访问控制,禁止解析php
96 0
|
安全 网络虚拟化 数据安全/隐私保护
华为ensp模拟器 配置ACL访问控制列表
华为ensp模拟器,模拟配置acl访问规则,配置acl访问规则的详细解释和操作。
华为ensp模拟器 配置ACL访问控制列表
|
网络协议 安全
CAPWAP:无线接入点的控制和配置访问控制器 DHCP 选项
本文件受 BCP 78 和 IETF 信托关于 IETF 文件的法律规定的约束,该法律规定自本文件发布之日起生效 (http://trustee.ietf.org/license-info)。请仔细阅读这些文件,因为它们描述了您对本文件的权利和限制。
180 0
CAPWAP:无线接入点的控制和配置访问控制器 DHCP 选项
EMQ
|
JSON 网络性能优化 API
支持 ACL 访问控制、引入 HOCON 全新配置文件格式
11月,超轻量MQTT Broker NanoMQ 0.14版本发布,推出了ACL鉴权服务,并引入了HOCON格式的配置文件。
EMQ
187 0
支持 ACL 访问控制、引入 HOCON 全新配置文件格式
|
数据安全/隐私保护 网络架构
网络工程之标准访问控制列表配置
这次我们来讲述大学网络工程之思科路由器标准访问控制列表配置
99 4
网络工程之标准访问控制列表配置
|
Linux 网络安全 数据安全/隐私保护
CentOS Stream 9通过配置sshd_config中AllowUsers实现SSH访问控制
CentOS Stream 9通过配置sshd_config中AllowUsers实现SSH访问控制
1104 0
CentOS Stream 9通过配置sshd_config中AllowUsers实现SSH访问控制
|
存储 应用服务中间件 开发工具
Nginx location 基础使用、四层访问控制、账户认证及自定义日志路径(四)|学习笔记
快速学习 Nginx location 基础使用、四层访问控制、账户认证及自定义日志路径
132 0
Nginx location 基础使用、四层访问控制、账户认证及自定义日志路径(四)|学习笔记
|
监控 Ubuntu JavaScript
Nginx location 基础使用、四层访问控制、账户认证及自定义日志路径(三)
Nginx location 基础使用、四层访问控制、账户认证及自定义日志路径(三)
381 0
Nginx location 基础使用、四层访问控制、账户认证及自定义日志路径(三)
|
负载均衡 数据安全/隐私保护 开发者
配置访问控制|学习笔记
快速学习配置访问控制
315 1
配置访问控制|学习笔记
|
网络协议 数据安全/隐私保护
ACL访问控制列表 基础、创建ACL访问控制列表的两种方式、配置ACL访问控制列表规则、修改ACL规则的默认步长。子网掩码、反掩码、通配符掩码的区别和作用。
ACL访问控制列表、创建ACL访问控制列表的两种方式、配置ACL访问控制列表规则、修改ACL规则的默认步长。子网掩码、反掩码、通配符掩码的区别和作用。ACL匹配机制详解。ACL的分类与标识,各种ACL的作用区别
ACL访问控制列表 基础、创建ACL访问控制列表的两种方式、配置ACL访问控制列表规则、修改ACL规则的默认步长。子网掩码、反掩码、通配符掩码的区别和作用。