Nginx使用—基础应用

Nginx日志配置

1.mkdir /etc/nginx/logs

2.server{
	listen 80;
	server_name nrj.test.com;
	access_log logs/acess-test.log; #正确日志
	error_log  logs/error-test.log; #错误日志
	root /www;
	index index.html;
}

Nginx目录索引

语法:autoindex on | off
默认:autoindex off;
使用模块:hhttp,server,location

#autoindex常用参数
autoindex_exact_size off;
默认为on,显示出文字的确切大小,单位是bytes
修改为off,显示出文字的大概大小,单位是kb或者mb或者gb

autoindex_localtime on;
默认为off,显示文件时间为GMT时间
修改为on ,显示的文件时间为文件的服务器时间

charset utf-8,gbk;
默认中文目录乱码,添加上解决乱码。
vim /etc/nginx/conf.d/download.conf
server{
	listen 80;
	server_name download.nrj.com; 
	autoindex on; #是否开启
	autoindex_exact_size off; #是否显示大小
	autoindex_localtime on; #文件时间
	charset utf-8; #字符集
	root /www/download;
	index index.html; 
}

Nginx状态监控

server{
	listen 80;
	server_name nrj.maliao.com;
	location / {
		root /www/maliao;
		index index.html;
	}
	location /stub_status {  
		stub_status on; 
		access_log off;
	}
}

基于ip地址访问控制
server{
	listen 80;
	server_name nrj.maliao.com;
	location / {
		root /www/maliao;
		index index.html;
	}
	location /stub_status {  
		stub_status on;
		access_log off;
		allow 10.0.0.1; #允许
		allow 127.0.0.1; 
		deny all; #不允许
	}
}

Nginx访问控制

基于用户登录的认证
#配置语法
语法: auth_basic string| off;
默认: auth_basic off;
应用模块:http,server,location,limit_except
#用户密码记录配置文件
语法: auth_basic_user_file file;
默认: -
应用模块: http,server,location,limit_except

httpd-tools
yum install -y httpd-tools
htpasswd -b -c /etc/nginx/auth_conf nrj 123
server{
	listen 80;
	server_name nrj.maliao.com;
	location / {
		root /www/maliao;
		index index.html;
		auth_basic on;
		auth_basic_user_file auth_conf;
	}
	location /stub_status {  
		stub_status on;
		access_log off;
		allow 10.0.0.1;
		allow 127.0.0.1;
		deny all;
	}
}

Nginx虚拟站点

1.基于不同的端口号
vim /etc/nginx/conf.d/test.conf
server{
	listen 80;
	server_name localhost;
	root /www/test;
	index index.html;
}
server{
	listen 81;
	server_name localhost;
	root /www/test;
	index index.html;
}

2.基于不同的域名
vim /etc/nginx/conf.d/test.conf
server{
	listen 80;
	server_name maliao.com;
	root /www/test;
	index index.html;
}
server{
	listen 80;
	server_name test.maliao.com;
	root /www/test;
	index index.html;
}
server{
	listen 80;
	server_name abc.maliao.com;
	root /www/test;
	index index.html;
}

3.基于域名的别名
vim /etc/nginx/conf.d/test.conf
server{
	listen 80;
	server_name www.maliao.com maliao.com;
	root /www/test;
	index index.html;
}

案例练习

部署nginx web server,要求如下:

1)部署三个站点,需要通过域名访问,有独立的日志 h5game.zjh.com donwload.zjh.com maliao.zjh.com

2)要求每个站点都开启状态监控

3)download开启目录索引

4)download需要通过用户认证才能访问

5)每个站点的状态监控只允许10.0.0.1可以访问  

环境准备
1.准备游戏压缩包
[root@web03 ~]# ls 
anaconda-ks.cfg  h5game.zip  html5-mario.zip

2.创建/web目录存放相关文件
mkdir -p /web

3.将压缩包解压后移动至/web目录下并在/web目录下创建download目录
unzip h5game.zip 
unzip html5-mario.zip 
mv h5game /web
mv html5-mario /web/maliao
mkdir -p /web/download

4.向download目录下放置点资源
cd /web/download

5.配置用户认证
htpasswd -bc /etc/nginx/auth_conf zjh 123

6.创建独立日志文件目录
mkdir /etc/nginx/logs

编写配置文件
1.编写配置文件,将三个虚拟站点写在一起
vim /etc/nginx/conf.d/default.conf
server {
    listen 80;
	server_name h5game.zjh.com;
	access_log logs/acess-h5game.log;
	error_log  logs/error-h5game.log;
    location /{
	   root /web/h5game;
	   index index.html; 
	}	
	location /stub_status {  
		stub_status on;
		access_log off;
		allow 10.0.0.1;
		deny all;
	}
}
server {
    listen 80;
	server_name download.zjh.com;
	access_log logs/acess-download.log;
	error_log  logs/error-download.log;
	location /{
	   auth_basic on;
	   auth_basic_user_file auth_conf;
	   autoindex on;
	   autoindex_exact_size off;
	   autoindex_localtime on;
	   charset utf-8;
	   root /web/download;
	   index index.html;
	}
	location /stub_status {  
		stub_status on;
		access_log off;
		allow 10.0.0.1;
		deny all;
	}
}
server {
    listen 80;
	server_name maliao.zjh.com;
	access_log logs/acess-maliao.log;
	error_log  logs/error-maliao.log;
	location /{
	   root /web/maliao;
	   index index.html; 
	}	
	location /stub_status {  
		stub_status on;
		access_log off;
		allow 10.0.0.1;
		deny all;
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值