虚拟主机

发布时间 2023-11-24 21:57:41作者: yuyongqi

1、基于IP的虚拟主机

主配置文件,需要加include

vim nginx.conf

[root@mylinux1 conf.d]# cat /etc/nginx/nginx.conf 

user nginx;
#worker_processes auto;
worker_processes 2;
worker_cpu_affinity 01 10;

error_log /var/log/nginx/error.log notice;
pid       /var/run/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include       /etc/nginx/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"';

  access_log /var/log/nginx/access.log main;
  #access_log off;
  sendfile       on;
  #tcp_nopush     on;

  keepalive_timeout 65;

  #gzip on;
  include /etc/nginx/conf.d/ip_121.conf;
}

 

虚拟主机配置文件,本次为172.16.5.121:80

vim conf.d/ip_121.conf


server {
  listen       172.16.5.121:80;
  server_name _;
  charset utf-8;
  access_log /var/log/nginx/172.16.5.121.access.log main;
  error_log /var/log/nginx/172.16.5.121.error.log;

  location / {
      root   /usr/share/nginx/121;
      index index.html index.htm;
  }
}

网页文件

创建该虚拟主机的网页主目录及修改文件

cp /usr/share/nginx/www_hello80/ /usr/share/nginx/121 -r

修改一下index.html,做区分

cat /usr/share/nginx/121/index.html 
<!DOCTYPE html>
<html>
<head>
<title>IP_121</title>
<style>
</style>
</head>
<body>
<h1>Welcome to IP_121</h1>
<p>
卜算子·咏梅
【作者】陆游 【朝代】宋
驿外断桥边,寂寞开无主。已是黄昏独自愁,更着风和雨。

无意苦争春,一任群芳妒。零落成泥碾作尘,只有香如故。
</p>
</body>
</html>

 

给本地网卡加IP

创建出多IP的环境

以下两个参数都可以
ifconfig ens33:1 172.16.5.121/24
ifconfig ens33:2 172.16.5.122 netmask 255.255.255.0

2,基于域名的虚拟主机

虚拟主机配置文件

vim /etc/nginx/conf.d/www_hello80_com.conf

server {
  listen       80;
  server_name www.hello80.com hello80.com hk.hello80.com;

  access_log /var/log/nginx/www_hello80_com.access.log main;
  error_log /var/log/nginx/www_hello80_com.error.log;
      if ( $host != 'www.hello80.com' ) {
      rewrite ^/(.*)$ http://www.hello80.com/$1 permanent;
      }

  location / {
      root   /usr/share/nginx/www_hello80;
      index index.html index.htm;
  }

}

主配置文件夹include

[root@mylinux1 conf.d]# cat /etc/nginx/nginx.conf 

user nginx;
#worker_processes auto;
worker_processes 2;
worker_cpu_affinity 01 10;

error_log /var/log/nginx/error.log notice;
pid       /var/run/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include       /etc/nginx/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"';

  access_log /var/log/nginx/access.log main;
  #access_log off;
  sendfile       on;
  #tcp_nopush     on;

  keepalive_timeout 65;

  #gzip on;

  include /etc/nginx/conf.d/www_hello80_com.conf;
  include /etc/nginx/conf.d/ip_121.conf;
}

网页文件

mkdir -p  /usr/share/nginx/www_hello80/ 
vim /usr/share/nginx/www_hello80/index.html
<!DOCTYPE html>
<html>
<head>
<title>www_hello80</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to www.hello80</h1>
</body>
</html>

3,基于端口的虚拟主机

虚拟主机配置文件

server {
  listen       172.16.5.109:81;
  server_name _;

  #access_log /var/log/nginx/81.access.log main;
  #error_log /var/log/nginx/81.error.log;

  location / {
      root   /usr/share/nginx/81;
      index index.html index.htm;
  }

}

主配置文件加include

加完后的nginx.conf是这样的

[root@mylinux1 conf.d]# cat /etc/nginx/nginx.conf 

user nginx;
#worker_processes auto;
worker_processes 2;
worker_cpu_affinity 01 10;

error_log /var/log/nginx/error.log notice;
pid       /var/run/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include       /etc/nginx/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"';

  access_log /var/log/nginx/access.log main;
  #access_log off;
  sendfile       on;
  #tcp_nopush     on;

  keepalive_timeout 65;

  #gzip on;

  include /etc/nginx/conf.d/www_hello80_com.conf;
  include /etc/nginx/conf.d/81.conf;
  include /etc/nginx/conf.d/ip_121.conf;
}

网页文件

先创建虚拟主机的网页主目录

mkdir -p  /usr/share/nginx/81

再创建网页文件index.html

vim /usr/share/nginx/81/index.html 
<!DOCTYPE html>
<html>
<head>
<title>81</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to 81</h1>
</body>
</html>

重新加载nginx配置文件

先检查配置文件,然后再重启或重新加载,restart/reload

nginx -t 

nginx -s reload

 

# nginx -s stop

# nginx

4,日志切割

编辑切割脚本

vim /data/script/cut_nginx_log.sh


#!/bin/bash
# This script run at 00:00
# The Nginx logs path
log_bak_dir="/data/backup/logs"
nginx_log="/var/log/nginx"
DATE=$(date -d yesterday +%F)
mkdir -p ${log_bak_dir}/${DATE}
cd ${nginx_log}
for i in *log;
do  
mv ${i} ${log_bak_dir}/${DATE}/${i}-${DATE}
done
# kill -USR1 `cat /var/run/nginx.pid`
kill -USR1 $(ps -ef|grep nginx |grep master | awk '{print $2}')

填加计划任务

[root@mylinux1 www_hello80]# crontab -e
00 00 * * * bash /data/script/cut_nginx_log.sh

 

5,autoindex

修改配置文件,加以下3行

    autoindex on;
  autoindex_localtime on;
  autoindex_exact_size off;
vim /etc/nginx/conf.d/www_hello80_com.conf 
server {
  listen       80;
  server_name www.hello80.com hello80.com hk.hello80.com;

  access_log /var/log/nginx/www_hello80_com.access.log main;
  error_log /var/log/nginx/www_hello80_com.error.log;
      # if ( $host != 'www.hello80.com' ) {
      # rewrite ^/(.*)$ http://www.hello80.com/$1 permanent;
      # }

  location / {
      autoindex on;
      autoindex_localtime on;
      autoindex_exact_size off;
      root   /usr/share/nginx/www_hello80;
      index index.html index.htm;
  }

}

然后重新加载nginx

nginx -t

nginx -s reload