nginx를 여행 (두 번째)의 nginx 로그 관리, nginx를 보안 체인의 nginx 웹 호스팅

A, nginx를 로그 관리

Nginx에 액세스 로그 제어 두 가지 매개 변수가 로그의 형식을 정의하는 데 사용됩니다 1) log_format 번호는 (로그 형식은 여러 정의 할 수 있습니다, 다른 이름이 걸릴 수 없음) log_format의 log_name 문자열 에 파일 경로를 지정하는 데 사용 2) ACCESS_LOG # 경로와 로그 기록 형식이 사용 ACCESS_LOG 로그 / access.log의 본체;

log_format 형식 변수 의미 :

분야 의미
REMOTE_ADDR 클라이언트 주소
REMOTE_USER 클라이언트 사용자 이름
time_local 서버 시간
의뢰 메소드 이름을 포함 요청 내용, 주소 및 HTTP 프로토콜
HTTP_HOST HTTP 주소는 때 사용자 요청을 사용하는
지위 HTTP 상태 코드가 반환
request_length 요청의 크기
body_bytes_sent 반환 크기
HTTP_REFERER 소스 페이지
HTTP_USER_AGENT 클라이언트 이름
REQUEST_TIME 전반적으로 지연 요청
upstream_response_time 업스트림 서비스 처리 지연

액세스 고객 레코드 $의 REMOTE_ADDR 번호는 사이트 로컬 주소 $의 REMOTE_USER 번호 원격 클라이언트 사용자 이름은 time_local # 기록 액세스 시간 및 시간대 $ HTTP 요청은 사용자 정보 트레킹 시작 # $ 요청을 $ 상태 #http 상태 코드가 기록 상태 요청에 의해 반환 코드는, 예를 들면, 예 : 200,301,404 등 $ body_bytes_sent # 1 서버는 응답 본문 바이트 클라이언트로 전송 $ HTTP_REFERER #은 도난 방지 쇄선에서 매개 변수 집합에 따라 수행 될 수 있으며, 위에 접속되는 접속 요청을 기록한다. $ HTTP_USER_AGENT #과 같은 예를 들어, 정보에 대한 클라이언트 액세스를 기록 등 브라우저, 휴대 전화 클라이언트, $ HTTP_X_FORWARDED_FOR #을 때 프런트 엔드 프록시 프로세서 서버 설정 웹 노드 기록 구성 클라이언트 주소, 효과 프록시 관리에서이 인수의 전제 서버는 관련된 행을 설정 x_forwarded_for

 

사용자 설정 로그

구성 파일의 포맷, 응용 프로그램 형식

[루트 @ localhost를 HTML] # pwd을 
/ usr / 지방 /의 nginx / html로 
[루트 @ localhost를 HTML] # VI ../conf/nginx.conf 
... 
HTTP { 
    의 mime.types를 포함한다; 
    default_type 애플리케이션 / octet-stream을; 
    #log_format 메인 '$ REMOTE_ADDR - $의 REMOTE_USER [$ time_local] "$ 요청' ' 
    #'$ 상태 $의 body_bytes_sent"$ HTTP_REFERER " ' 
    #'"$ HTTP_USER_AGENT ","$ HTTP_X_FORWARDED_FOR " '; 
    #自定义日志第一步自己设置日志的格式
    log_format mylogs '[$ time_local] - $ REMOTE_ADDR - "$ 요청"- $ 상태' 
    #access_log 로그 / access.log의 주요; 
    sendfile을에; 
    에 #tcp_nopush;
    
    
    
    
        80 듣지; 
        서버 _ 로컬 호스트를, 
        #charset KOI8-R & LT; 
        캐릭터 UTF-8]. 
        #access_log 로그 / 메인 host.access.log는, 
        # 지정하는 제 2 단계와, 애플리케이션 로그 형식 로그 
        ACCESS_LOG 로그 / host.access.log mylogs을 ; 
        위치 / { 
            루트 HTML, 
            인덱스있는 index.html index.htm으로; 
        }; 
        / 해당 사이트를 나타내는 루트 #location / 
}
        
      
    

  

 

tailf 사용에 /Usr/local/nginx/logs/host.access.log 추적 로그

[루트 @ localhost를 ~] # cd를 / usr / 지방 /의 nginx / 로그 / 
[루트 @ localhost를 로그] # LS 
access.log의하는 error.log host.access.log nginx.pid 
[루트 @ localhost를 로그] # 개의 tailf의 host.access .LOG 
[14 / DEC / 2019 : 20 : 09 : 39 0,800] - 192.168.199.168 - "GET / B / HTTP / 1.1"-304
 

  

두 사용 사례, 사용자 정의 로그 형식 JSON

[루트 @ localhost를 HTML] # pwd을 
/ usr / 지방 /의 nginx / html로 
[루트 @ localhost를 HTML] # VI ../conf/nginx.conf 
... 
HTTP { 
    의 mime.types를 포함한다; 
    default_type 애플리케이션 / octet-stream을; 
    #log_format 메인 '$ REMOTE_ADDR - $의 REMOTE_USER [$ time_local] "$ 요청' ' 
    #'$ 상태 $의 body_bytes_sent"$ HTTP_REFERER " ' 
    #'"$ HTTP_USER_AGENT ","$ HTTP_X_FORWARDED_FOR " '; 
    #自定义日志第一步自己设置日志的格式
    log_format main_json '{ "@timestamp": "$ time_local"' 
    ' "client_ip": "$ REMOTE_ADDR', ' 
    '"요청 ":"$ 요청 ','
    
    ' "$ HTTP_X_FORWARDED_FOR", "x_forwarded' ' 
    ' '리퍼러를": "$ HTTP_REFERER', ' 
    '} '; 
    
    
    #access_log 로그 / access.log의 주요; 
    에 sendfile을; 
    에 #tcp_nopush, 
서버 { 
        80 듣고; 
        서버 _ 로컬 호스트; 
        #charset KOI8-R; 
        캐릭터 셋 UTF-8; 
        #access_log 로그 / host.access.log 주; 
        #自定义日志第二步,应用日志格式
        ACCESS_LOG 로그 / access_json.log main_json, 
        위치 / { 
            루트 HTML; 
            index.htm으로 index.html을 인덱스;
        /이 사이트의 루트를 나타냅니다 #location / 
        
      
    
}

  

둘째, 안티 - 핫 링크

창조 환경

VM --web02 IP 주소가 192.168.199.229입니다

거미를 작성, nginx를 설치, index.html을 작성합니다

[루트 @ 로컬 호스트의 nginx] # CD를 HTML / 
[@ 루트 로컬 호스트 HTML] # LS 
50x.html index.html을 
[루트 @ 로컬 호스트 HTML] # MKDIR 거미 
[루트 @ 로컬 호스트 HTML] # VI 거미 / index.html을 
<HTML> 
<HEAD> 
<TITLE> 팽 DAO 리안 CE시 </ TITLE> 
</ head> 
<body> 
<폰트 크기 = "5"> 
<a href="http://192.168.199.228/c/0.png"> DAO 리안 </a>에 
로 <br> </br> 
</ FONT> 
</ BODY> 
</ HTML> 
~                                     
                          

VM-WEB01의 IP는 192.168.199.228입니다

은 / usr / 지방 / Nginx에 / HTML / C로 0.png

효과

 

 

제공되는 도난 방지 체인 VM-WEB01

[@ localhost를 루트 HTML] # pwd을 
/ usr / 지방 / Nginx의 / HTML 
[@ localhost를 루트 HTML] # VI ../conf/nginx.conf 
... 
HTTP { 
... 
서버 { 
        (가) 80 듣고, 
        서버 _ localhost를; 
        KOI8-R & LT #charset] 
        . 캐릭터 UTF-8; 
        #access_log 로그 / 메인 host.access.log; 
        LOCATION / { 
            루트 HTML, 
            인덱스 된 index.html index.htm을; 
        }; 
        루트 #location / / 사이트 대표 카탈로그 
        안티 - 핫 링크의 C 파일에 대한 # 폴더 설정, 
                IF ($ invalid_referer) { 
                        반환 (403);
        
        
        location /c {
                valid_referers none blocked *.test.com;
                        }
                 }   
​
    
}

  

valid_referers none blocked *.test.com;

表示要么没有Referrer要么Referrer:blocked(走的是硬件防火墙)要么Referrer: 属于*.test.com这个域名

 

针对全站设置图片防盗链

        location ~* \.(png|gif|bmp)$ {
            valid_referers none blocked *.test.com; 
            if ($invalid_referer) {
                return 403;
            }
         }
​

  

效果

 

三、虚拟主机

虚拟主机的概念

如果你有两个不同域名的网站,但是你只有一台服务器,这时候怎么办?其实利用nginx或者apache都可以帮你用一台机器来模拟多台机器作为服务器提供服务。

一个web服务器器软件默认情况下只能发布一个web,因为一个web分享出去需要三个条件(IP、Port、Domain name)。

虚拟主机,就是把一台物理服务器划分成多个“虚拟”的服务器,每一个虚拟主机都可以有独立的域名和独立的目录

,在web服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外提供服务供用户访问。

这个这个独立的站点在配置里是由一定格式的标签段标记,对于apache软件来说,一个虚拟主机的标签段通畅被包含在<VirtualHost></VirtualHost>内,而nginx软件则使用一个server{}标签来标示一个虚拟主机,一个web服务里可以有多个虚拟主机主机标签对,即同时可以支持多个虚拟主机站点。

通过 Nginx 可以实现虚拟主机的配置,Nginx 支持三种类型的虚拟主机配置

  • 基于 IP 的虚拟主机

  • 基于端口的虚拟主机

  • 基于域名的虚拟主机

Nginx 配置文件的结构

# ...
events {
    # ...
}
​
http {
    # ...
    server{
        # ...
    }
    
    # ...
    server{
        # ...
    }
}

 

注:每个 server 就是一个虚拟主机

 

###

1、基于IP的虚拟主机

基于IP的虚拟主机,意思就是通过不同的IP区分不同的虚拟主机,

Linux 操作系统允许添加 IP 别名,IP 别名就是在一块物理网卡上绑定多个 lP 地址。这样就能够在使用单一网卡的同一个服务器上运行多个基于 IP 的虚拟主机。

需求

本机原有ip192.168.199.228

  • 一台 Nginx 服务器绑定两个 IP:192.168.199.228、192.168.199.128

  • 访问不同的 IP 请求不同的 HTML 目录,即:

    • 访问 http://192.168.199.228 将访问 a 目录下的 html 网页

    • 访问 http://192.168.199.128 将访问 b 目录下的 html 网页

增加辅助ip的方法

1.1 临时性增加辅助ip:

方法一:ifconfig ens33:1 192.168.199.128/24 up

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.199.228  netmask 255.255.255.0  broadcast 192.168.199.255
        inet6 fe80::3f62:e00a:ee97:b24d  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::2ab:8d0e:862a:fdf0  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::8d2:921f:c09f:8c97  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:7b:9b:18  txqueuelen 1000  (Ethernet)
        RX packets 57932  bytes 8672597 (8.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18482  bytes 1963395 (1.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 232  bytes 27329 (26.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 232  bytes 27329 (26.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
[root@localhost ~]# ifconfig ens33:1 192.168.199.128/24 up  #创建子ip128
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.199.228  netmask 255.255.255.0  broadcast 192.168.199.255
        inet6 fe80::3f62:e00a:ee97:b24d  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::2ab:8d0e:862a:fdf0  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::8d2:921f:c09f:8c97  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:7b:9b:18  txqueuelen 1000  (Ethernet)
        RX packets 58773  bytes 8728263 (8.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18554  bytes 1971175 (1.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.199.128  netmask 255.255.255.0  broadcast 192.168.199.255
        ether 00:0c:29:7b:9b:18  txqueuelen 1000  (Ethernet)
​
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 232  bytes 27329 (26.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 232  bytes 27329 (26.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
[root@localhost ~]# 
​

  

 

方法二:ip addr

ip addr add 192.168.199.130/24 dev ens33(使用ip addr能查看)

ip addr add 192.168.199.130/24 label ens33:2 dev ens33(使用ifconfig和ipaddr都能查看,推荐使用)

[root@localhost ~]# ip addr add 192.168.199.130/24 label ens33:2 dev ens33
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.199.229  netmask 255.255.255.0  broadcast 192.168.199.255
        inet6 fe80::6fd6:65fd:b960:af14  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::5a0a:f713:1cae:b91b  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::292a:bee0:12:74fa  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:08:ee:cd  txqueuelen 1000  (Ethernet)
        RX packets 10777  bytes 1765191 (1.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2835  bytes 267788 (261.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
ens33:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.199.130  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:08:ee:cd  txqueuelen 1000  (Ethernet)
​
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 352 (352.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 352 (352.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  


1.2永久增加辅助ip

cd /etc/sysconfig/network-scripts/ #进入到网卡配置文件的目录

cp ifcfg-ens33 ifcfg-ens33:1 #拷贝配置文件并重命名

vi ifcfg-ens33:1 #编辑配置文件

/etc/init.d/network restart #重启网络服务

[root@localhost network-scripts]# vi ifcfg-ens33:1
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33:1" #修改
UUID="42820e51-d7c9-49e3-a0ec-7f1b09f797c5"
DEVICE="ens33:1" #修改
ONBOOT="yes"
IPADDR="192.168.199.128"  #增加
NETMASK="255.255.255.0" #增加

[root@localhost network-scripts]# /etc/init.d/network restart 
Restarting network (via systemctl):                       [ OK ]
[root@localhost network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 192.168.199.228 netmask 255.255.255.0 broadcast 192.168.199.255
      inet6 fe80::3f62:e00a:ee97:b24d prefixlen 64 scopeid 0x20<link>
      inet6 fe80::2ab:8d0e:862a:fdf0 prefixlen 64 scopeid 0x20<link>
      inet6 fe80::8d2:921f:c09f:8c97 prefixlen 64 scopeid 0x20<link>
      ether 00:0c:29:7b:9b:18 txqueuelen 1000 (Ethernet)
      RX packets 61195 bytes 9081544 (8.6 MiB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 19449 bytes 2067061 (1.9 MiB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 192.168.199.128 netmask 255.255.255.0 broadcast 192.168.199.255
      ether 00:0c:29:7b:9b:18 txqueuelen 1000 (Ethernet)
...

  




 

创建目录及文件

/usr/local/nginx/html 目录下创建 a 和 b两个目录,并分辨创建两个 index.html 文件

[root@localhost html]# mkdir a b 
[root@localhost html]# ls
50x.html  a  b  c  index.html
[root@localhost html]# echo aaa >a/index.html
[root@localhost html]# echo bbb >b/index.html

  

配置虚拟主机

修改 /usr/local/nginx 目录下的 nginx.conf 配置文件:

...
http{
...
​
    # 配置虚拟主机 192.168.199.228
    server {
    # 监听的ip和端口,配置 192.168.199.228:80
        listen       192.168.199.228:80;
    # 所有的请求都以/开始,所有的请求都可以匹配此 location
        location / {
        # 使用 root 指令指定虚拟主机目录即网页存放目录
        # 比如访问 http://ip/index.html 将找到/usr/local/nginx/html/a/index.html
        # 比如访问 http://ip/item/index.html 将找到 /usr/local/nginx/html/a/item/index.html
            root   /usr/local/nginx/html/a;
        # 指定欢迎页面,按从左到右顺序查找
            index  index.html index.htm;
        }
​
    }
    # 配置虚拟主机 192.168.199.128
    server {
        listen      192.168.199.128:80;
​
        location / {
            root   /usr/local/nginx/html/b;
            index  index.html index.htm;
        }
    }
      
    
}

  

重启nginx

[root@localhost html]# killall nginx
[root@localhost html]# lsof -i :80
[root@localhost html]# ../sbin/nginx 
​

  

 

2、基于端口的虚拟主机配置

需求

  • Nginx 对外提供 80 和 8080 两个端口监听服务

  • 请求 80 端口则请求 html80 目录下的 html

  • 请求 8080 端口则请求 html8080 目录下的 html

创建目录及文件

/usr/local/nginx/html 目录下创建 html80html8080 两个目录,并分辨创建两个 index.html 文件

[root@localhost nginx]# pwd
/usr/local/nginx
[root@localhost nginx]# mkdir html80 html8080
[root@localhost nginx]# echo 80html网页 >html80/index.html
[root@localhost nginx]# echo 8080html网页 >html8080/index.html
[root@localhost nginx]# cat html80/index.html 
80html网页

  


配置虚拟主机

修改 /usr/local/nginx 目录下的 nginx.conf 配置文件:

...
http{
...
​
    # 配置虚拟主机 192.168.199.228
    server {
        listen     80;
    # 所有的请求都以/开始,所有的请求都可以匹配此 location
        location / {
            root   /usr/local/nginx/html80;
            index  index.html index.htm;
        }
​
    }
    server {
        listen      8080;
        location / {
            root   /usr/local/nginx/html8080/;
            index  index.html index.htm;
        }
    }
      
    
}

  

3、基于域名的虚拟主机配置

需求

  • 两个域名指向同一台 Nginx 服务器,用户访问不同的域名显示不同的网页内容

  • 两个域名是 admin.abc.com 和 service.abc.com

  • Nginx 服务器使用虚拟机 192.168.199.228

 

配置Hosts 文件

[root@localhost nginx]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.199.228 admin.abc.com
192.168.199.228 service.abc.com
​

  

创建目录及文件

/usr/local/nginx/html 目录下创建 htmladminhtmlservice 两个目录,并分辨创建两个 index.html 文件

[root@localhost nginx]# mkdir htmladmin htmlservice
[root@localhost nginx]# echo htmladmin >htmladmin/index.html
[root@localhost nginx]# echo htmlservice >htmlservice/index.html
[root@localhost nginx]# cat htmladmin/index.html 
htmladmin
[root@localhost nginx]# cat htmlservice/index.html 
htmlservice

 

配置虚拟主机

修改 /usr/local/nginx 目录下的 nginx.conf 配置文件:

...
http{
...
​
    # 配置虚拟主机 192.168.199.228
    server {
        listen     80;
        server_name admin.abc.com;
        location / {
            root   /usr/local/nginx/htmladmin;
            index  index.html index.htm;
        }
​
    }
    server {
        listen      80;
        server_name service.abc.com;
        location / {
            root   /usr/local/nginx/htmlservice/;
            index  index.html index.htm;
        }
    }
      
    
}

  

重启nginx

[root@localhost 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@localhost nginx]# killall nginx
[root@localhost nginx]# sbin/nginx

  

效果

[root@localhost nginx]# elinks http://admin.abc.com --dump
   htmladmin
[root@localhost nginx]# elinks http://service.abc.com --dump
   htmlservice
​

  

 

추천

출처www.cnblogs.com/Nicholas0707/p/12056399.html