一、nginx回顾
1.安装
1.epol源安装
2.官方源安装
3.源码包安装
1)下载
2)解压
3)生成
4)编译
5)安装
2.nginx配置文件
[ root@web01 ~]
user www;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;
charset utf8;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /download {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
二、Nginx虚拟主机
1.虚拟主机方式
1.基于多IP的方式 不推荐
命令添加多个子ip:
ifconfig eth0:1 10.0.0.8/24
ifconfig eth0:2 10.0.0.9/24
2.基于多端口的方式
ip不变,只更改nginx.conf游戏配置文件内的端口号
比如游戏1为80,游戏2为81..
3.基于多域名的方式
ip不变,端口默认80,添加ip 域名到本地系统的hosts文件内:
C:\Windows\System32\drivers\etc\hosts
方式1:ip后跟多个域名
10.0.0.8 www.caijinbi.com www.wuziqi.com
方式2:每条域名配一个ip
10.0.0.8 www.caijinbi.com
10.0.0.8 www.wuziqi.com
2.基于多IP的方式
0)网卡添加子IP
[ root@web01 ~]
1)第一个配置文件
[ root@web01 ~]
server {
listen 10.0.0.7:80;
server_name localhost;
location / {
root /code/tuixiangzi;
index index.html;
}
}
2)第二个配置文件
[ root@web01 ~]
server {
listen 10.0.0.3:80;
server_name localhost;
location / {
root /code/tank;
index index.html;
}
}
3)检查配置
[ root@web01 ~]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4)重启访问
[ root@web01 ~]
http://10.0.0.7/
http://10.0.0.3/
3.基于多端口的方式
1)第一个配置文件
[ root@web01 ~]
server {
listen 80;
server_name localhost;
location / {
root /code/tuixiangzi;
index index.html;
}
}
2)第二个配置文件
[ root@web01 ~]
server {
listen 81;
server_name localhost;
location / {
root /code/tank;
index index.html;
}
}
3)检查配置
[ root@web01 ~]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4)重启访问
[ root@web01 ~]
http://10.0.0.7/
http://10.0.0.7:81/
4.基于多域名的方式
1)第一个配置文件
[ root@web01 ~]
server {
listen 80;
server_name www.tuixiangzi.com;
location / {
root /code/tuixiangzi;
index index.html;
}
}
2)第二个配置文件
[ root@web01 ~]
server {
listen 80;
server_name www.tank.com;
location / {
root /code/tank;
index index.html;
}
}
3)检查配置
[ root@web01 ~]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4)重启
[ root@web01 ~]
5)配置本地hosts访问
C:\Windows\System32\drivers\etc\hosts
10.0.0.7 www.tuixiangzi.com www.tank.com
http://www.tuixiangzi.com/
http://www.tank.com/
5.日志配置
1)第一个配置
[ root@web01 ~]
server {
listen 80;
server_name www.tuixiangzi.com;
access_log /var/log/nginx/www.tuixiangzi.com.log main;
location / {
root /code/tuixiangzi;
index index.html;
}
}
2)第二个配置
[ root@web01 ~]
server {
listen 80;
server_name www.tank.com;
access_log /var/log/nginx/www.tank.com.log main;
location / {
root /code/tank;
index index.html;
}
}
3)重启访问测试
[ root@web01 ~]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[ root@web01 ~]
[ root@web01 ~]
total 136
-rw-r-----. 1 nginx adm 103660 Nov 27 09:31 access.log
-rw-r-----. 1 nginx adm 21972 Nov 27 09:36 error.log
-rw-r--r--. 1 root root 666 Nov 27 09:36 www.tank.com.log
-rw-r--r--. 1 root root 190 Nov 27 09:36 www.tuixiangzi.com.log
三、nginx日志
Nginx有非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志。日志格式通过log_format命令定义格式
http {
server{
location{
}
}
}
1.log_format语法
Syntax: log_format name [ escape= default| json| none] string .. .;
Default: log_format combined "..." ;
Context: http
2.默认日志格式
log_format main '$remote_addr - $remote_user [$time_local ] "$request " '
'$status $body_bytes_sent "$http_referer " '
'"$http_user_agent " "$http_x_forwarded_for "' ;
10.0.0.1 - - [ 27/Nov/2020:09:36:08 +0800] "GET /images/tank.ico HTTP/1.1" 200 25214 "http://www.tank.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36" "-"
10.0.0.1 - - [ 2020-11-27T10:13:58+08:00] "GET /images/modewin/help0.png HTTP/1.1" 200 27947 "http://www.tank.com/css/tank.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36" "-"
3.日志常用变量
$remote_addr
$remote_user
$time_local
$time_iso8601
$request
$status
$body_bytes_sent
$bytes_sent
$msec
$http_referer
$http_user_agent
$http_x_forwarded_for
$X -Real-IP
$request_length
$request_time
4.nginx日志切割
[ root@web01 ~]
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
not if empty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ] ; then
kill -USR1 ` cat /var/run/nginx.pid`
fi
endscript
}
四、nginx常用模块
1.目录索引模块
ngx_http_autoindex_module模块处理以斜杠字符('/' )结尾的请求,并生成目录列表。
当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。
1)语法
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
2)配置
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
autoindex on;
}
}
3)访问网站正常,加download跳转目录页面
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
}
}
[ root@web01 ~]
[ root@web01 ~]
http://www.autoindex.com/ 为主站
http://www.autoindex.com/download/ 为下载文件的目录
4)常用优化参数
Syntax: autoindex_exact_size on | off;
Default: autoindex_exact_size on;
Context: http, server, location
Syntax: autoindex_localtime on | off;
Default: autoindex_localtime off;
Context: http, server, location
5)完整配置
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
2.Nginx访问控制模块
1)语法
Syntax: allow address | all;
Default: —
Context: http, server, location, limit_except
Syntax: deny address | all;
Default: —
Context: http, server, location, limit_except
2)配置访问控制示例
1>拒绝指定的IP,其他全部允许
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
deny 10.0.0.1;
allow all;
}
}
2>只允许指定IP能访问, 其它全部拒绝
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.1;
deny all;
}
}
3>只允许10.0.0.1访问,拒绝该网段其他IP
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.1;
deny 10.0.0.0/24;
}
}
3.Nginx访问认证模块
1)语法
Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http, server, location, limit_except
Syntax: auth_basic_user_file file ;
Default: —
Context: http, server, location, limit_except
2)创建密码文件
[ root@web01 ~]
New password:
Re-type new password:
Adding password for user lhd
[ root@web01 ~]
New password:
Re-type new password:
Adding password for user egon
[ root@web01 ~]
lhd:$apr1 $A7d4BWYe $HzlIA7pjdMHBDJPuLBkvd /
egon:$apr1 $psp0M3A5 $601t7Am1BG3uINvuBVbFV0
3)配置访问登录
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
access_log /var/log/nginx/www.autoindex.com.log main;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "性感荷官在线发牌!!!" ;
auth_basic_user_file /etc/nginx/auth_basic;
}
}
4.Nginx状态监控模块
ngx_http_stub_status_module模块提供对nginx基本状态信息的访问。
默认情况下不构建此模块,应使用--with-http_stub_status_module配置参数启用它
1)语法
Syntax: stub_status;
Default: —
Context: server, location
2)配置
[ root@web01 ~]
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
access_log /var/log/nginx/www.autoindex.com.log main;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "性感荷官在线发牌!!!" ;
auth_basic_user_file /etc/nginx/auth_basic;
}
location = /basic_status {
stub_status;
}
}
3)访问
Active connections: 2
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 1
Active connections
accepts
handled
requests
Reading
Writing
Waiting
keepalive_timeout 0;
keepalive_timeout 65;