Nginx配置文件和虚拟主机

目录

一、配置文件

1、访问统计

2、基于客户的访问控制

二、虚拟主机

1、基于域名的虚拟web主机

配置步骤:

2、基于端口的虚拟web主机

配置步骤:

3、基于ip的虚拟web主机

配置步骤:

总结


一、配置文件

1、访问统计

也可以在/etc/hosts文件中,提前做网站映射,测试的时候需要在虚拟机的浏览器访问

1、先使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块
2、修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置

cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.ly.com;
		charset utf-8;
		location / {
			root html;
			index index.html index.php;
		}
		##添加 stub_status 配置##
		location /status { 					#访问位置为/status
			stub_status on; 				#打开状态统计功能
			access_log off; 				#关闭此位置的日志记录
		}
	}
}

systemctl restart nginx

2、基于客户的访问控制

访问控制规则如下:
deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。

vim /usr/local/nginx/conf/nginx.conf
......
	server {
		location / {
			......
			##添加控制规则##
			deny 192.168.255.230; 					#拒绝访问的客户端 IP
			allow all;								#允许其它IP客户端访问
		}
	}

systemctl restart nginx

二、虚拟主机

提高主机的利用率,节约成本,节约主机

1、基于域名的虚拟web主机

目的:通过访问不同域名,来访问不同页面

配置步骤:

添加域名解析

echo "192.168.255.180 www.ly.com www.ly2.com" >> /etc/hosts

准备虚拟站点的网页文档

mkdir -p /var/www/html/ly
mkdir -p /var/www/html/ly2
echo "<h1>www.ly.com</h1>" > /var/www/html/ly/index.html
echo "<h1>www.ly2.com</h1>" > /var/www/html/ly2/index.html

修改配置文件

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.ly.com;					#设置域名www.ly.com
		charset utf-8;
		access_log logs/www.ly.access.log; 
		location / {
			root /var/www/html/ly;				#设置www.ly.com 的工作目录
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
	
	server {
		listen 80;
		server_name www.ly2.com;					#设置域名www.ly2.com
		charset utf-8;
		access_log logs/www.ly2.access.log; 
		location / {
			root /var/www/html/ly2;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}	
}

 重启测试

systemctl restart nginx

在centos系统中进行测试
http://www.ly.com
http://www.ly2.com

2、基于端口的虚拟web主机

目的:通过不同的端口号,来访问不同的页面

配置步骤:

添加域名解析

echo "192.168.255.180 www.ly.com www.ly8080.com" >> /etc/hosts

准备虚拟站点的网页文档

mkdir -p /var/www/html/ly
mkdir -p /var/www/html/ly8080
echo "<h1>www.ly.com</h1>" > /var/www/html/ly/index.html
echo "<h1>www.ly8080.com</h1>" > /var/www/html/ly8080/index.html

修改配置文件

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 192.168.255.180:80;					#设置监听 80 端口
		server_name www.ly.com;
		charset utf-8;
		access_log logs/www.ly.access.log; 
		location / {
			root /var/www/html/ly;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}

server {
	listen 192.168.255.180:8080;					#设置监听 8080端口
	server_name www.ly.com;
	charset utf-8;
	access_log logs/www.ly8080.access.log; 
	location / {
		root /var/www/html/ly8080;
		index index.html index.php;
	}
	error_page 500 502 503 504 /50x.html;
	location = 50x.html{
		root html;
	}
}

systemctl restart nginx

重启测试

systemctl restart nginx

在centos系统中进行测试
192.168.255.180:80
192.168.255.180:8080

3、基于ip的虚拟web主机

目的:通过不同的网卡,来访问不同的页面

配置步骤:

添加域名解析

echo "192.168.255.180 www.ly.com " >> /etc/hosts
echo "192.168.255.100 www.ly2100.com " >> /etc/hosts

准备虚拟站点的网页文档

mkdir -p /var/www/html/ly
mkdir -p /var/www/html/ly2100
echo "<h1>www.ly.com</h1>" > /var/www/html/ly/index.html
echo "<h1>www.ly2100.com</h1>" > /var/www/html/ly2100/index.html

建立临时虚拟网卡

ifconfig ens33:0 192.168.255.100 netmask 255.255.255.0 

修改配置文件

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
		listen 192.168.255.180:80;					#设置监听地址
		server_name www.ly.com;
		charset utf-8;
		access_log logs/www.ly.access.log; 
		location / {
			root /var/www/html/ly;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
server {
	listen 192.168.255.100:80;					#设置监听地址
	server_name www.ly2100.com;
	charset utf-8;
	access_log logs/www.ly2100.access.log; 
	location / {
		root /var/www/html/ly2100;
		index index.html index.php;
	}
	error_page 500 502 503 504 /50x.html;
	location = 50x.html{
		root html;
	}
}	
}

重启测试

systemctl restart nginx

在centos系统中进行测试
192.168.255.180
192.168.255.100

总结

 在客户通过http服务访问Ng的时候,首先进行三次握手,通过不同的请求,在这里我们通常也是通过同域名不同的接口来调用的,可以跳转给不同的业务区域,提供不同需求,在进行调用数据库资源,这里也充分应用了虚拟主机来实现

猜你喜欢

转载自blog.csdn.net/y1035793317/article/details/120639791