虚拟主机的配置

1.IP地址的配置

[[email protected] alen]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:32:1c:07  txqueuelen 1000  (Ethernet)
        RX packets 114  bytes 7023 (6.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        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 1  (Local Loopback)
        RX packets 27  bytes 2666 (2.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27  bytes 2666 (2.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:f4:d5:2e  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[[email protected] alen]# ifconfig virbr0 192.168.122.1 netmask 255.255.255.0
[[email protected] alen]# ifconfig virbr0:1 192.168.122.2 broadcast 192.168.122.255 netmask 255.255.255.0
[[email protected] alen]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:32:1c:07  txqueuelen 1000  (Ethernet)
        RX packets 189  bytes 11523 (11.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        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 1  (Local Loopback)
        RX packets 27  bytes 2666 (2.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27  bytes 2666 (2.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:f4:d5:2e  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0:1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.2  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:f4:d5:2e  txqueuelen 1000  (Ethernet)

[[email protected] alen]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:32:1c:07  txqueuelen 1000  (Ethernet)
        RX packets 202  bytes 12303 (12.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        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 1  (Local Loopback)
        RX packets 27  bytes 2666 (2.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27  bytes 2666 (2.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:f4:d5:2e  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0:1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.3  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:f4:d5:2e  txqueuelen 1000  (Ethernet)

 

2.进入安装目录cd /usr/local/nginx/conf,新建配置文件xlzj.conf,在原先的配置文件中改也行

文件内容

user nobody;
worker_processes 4;
events{
worker_connections 1024;
}
http{
    server{
        listen 192.168.1.1:80;
        server_name 192.168.1.1;
        access_log logs/server1.access.log combined;
        location /
        {
        index index.html index.htm;
        root html/server1;
        }
        }
    server{
        listen 192.168.1.2:80;
        server_name 192.168.1.2;
        access_log logs/server2.access.log combined;
        location /
        {
        index index.html index.htm;
        root html/server2;
        }
        }    
}


3.新建各自的root配置对应的文件夹server2,server1,和index.html文件

4.加载配置文件

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/xlzj.conf


5.输入http://192.168.122.1/ 验证



2.日志格式的设置
1. #日志格式设定  
        #$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;  
        #$remote_user:用来记录客户端用户名称;  
        #$time_local: 用来记录访问时间与时区;  
        #$request: 用来记录请求的url与http协议;  
        #$status: 用来记录请求状态;成功是200,  
        #$body_bytes_sent :记录发送给客户端文件主体内容大小;  
        #$http_referer:用来记录从那个页面链接访问过来的;  
        #$http_user_agent:记录客户浏览器的相关信息;  
        #通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。  
        log_format access '$remote_addr - $remote_user [$time_local] "$request" '  
        '$status $body_bytes_sent "$http_referer" '  
        '"$http_user_agent" $http_x_forwarded_for';  
           
        #定义本虚拟主机的访问日志  
        access_log  /usr/local/nginx/logs/host.access.log  main;  
        access_log  /usr/local/nginx/logs/host.access.404.log  log404;  
        

2.进入nginx.conf文件,修改如下        
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
    
3.日志切割
手动切割
[[email protected] logs]# mv access.log  20180225.log
[[email protected] logs]# ls
20180225.log  error.log  nginx.pid  server1.access.log  server2.access.log
[[email protected] logs]# ps -ef|grep nginx
root       6933      1  0 16:47 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     6937   6933  0 16:47 ?        00:00:00 nginx: worker process
root       7080   3970  0 16:53 pts/2    00:00:00 grep --color=auto nginx
[[email protected] logs]# kill -USR1 6933
[[email protected] logs]# ls
20180225.log  error.log  server1.access.log
access.log    nginx.pid  server2.access.log
[[email protected] logs]#

自动切割
通过定时批处理
新建cutlog.sh文件
[[email protected] logs]# touch cutlog.sh
[[email protected] logs]# vi cutlog.sh

D=$(date +%Y%m%d)
mv /usr/local/nginx/logs/access.log ${D}.log
kill -USR1 $(cat /usr/local/nginx/nginx.pid)

设置定时执行cutlog.sh文件
[[email protected] logs]# crontab -e
17 50 *** /bin/bash /usr/local/nginx/logs/cutlog.shNginx虚拟主机配置(四)


相关文章:

  • 2021-05-28
  • 2022-02-15
  • 2022-02-04
猜你喜欢
  • 2021-06-22
  • 2021-05-11
  • 2021-09-09
  • 2021-10-07
  • 2021-10-23
  • 2021-07-29
  • 2021-07-27
相关资源
相似解决方案