1.nginx的基本组件
- nginx的二进制文件
- nginx的配置文件/etc/nginx/conf.d/*.conf
- nginx的access日志
- nginx的error 日志
2.nginx的安装
yum install nginx -y #安装nginx
rpm -ql nginx #查看nginx的目录文件
cat /etc/nginx/nginx.conf #nginx的配置文件
--------------------/etc/nginx/nginx.conf-------------------------
user www; # nginx进程是哪个用户来运行
worker_processes 2; # 启动多少个worker进程
error_log /var/log/nginx/error.log warn; # 错误日志存在哪里
events {
worker_connections 1024; # worker最大连接数1024*worker_processes
}
http { #HTTP模型
include /etc/nginx/mime.types;
default_type application/octet-stream; #当nginx无法识别的文件时,默认为访问就下载
#定义日志格式
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;
#tcp_nopush on;
keepalive_timeout 65; #长连接(默认长连接65s)
#gzip on; #压缩
include /etc/nginx/conf.d/*.conf; #包含该目录下的所有.conf文件
}
-------------------/etc/nginx/nginx.conf结束-----------------------
3.搭建一个网站
mkdir /code #存放游戏路径
vim /etc/nginx/conf.d/game.conf
--------------------/etc/nginx/conf.d/game.com----------------------
server {
listen 80;
server_name game.com;
location / {
root /code;
index index.html;
}
}
--------------------/etc/nginx/conf.d/game.com结束-------------------
nginx -t #检查语法错误
systemctl start nginx #启动nginx
Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.7 game.com #nginx的服务器的IP,与server_name
在浏览器中输入域名game.com即可访问。
4.nginx多个IP访问(很少)
mkdir -p /code/ip1 #创建两个网址
mkdir -p /code/ip2
echo "hello,10.7" > /code/ip1/index.html #两个网址写入对应的IP
echo "hello,172.7" > /code/ip2/index.html
vim /etc/nginx/conf.d/ip.conf
------------------------/etc/nginx/conf.d/ip.conf-------------------------
server {
listen 10.0.0.7:80;
location / {
root /code/ip1;
index index.html;
}
listen 172.16.1.7:80;
location / {
root /code/ip2;
index index.html;
}
}
------------------------/etc/nginx/conf.d/ip.conf结束----------------------
nginx -t
systemctl restart nginx
#测试
curl 10.0.0.7:80
curl 172.16.1.7:80
5.基于不同的端口访问(测试)
mkdir -p /code/port1
mkdir /code/port2
echo "8081" > /code/port1/index.html
echo "8082" > /code/port2/index.html
vim /etc/nginx/conf.d/port.conf
------------------------/etc/nginx/conf.d/port.conf------------------------
server {
listen 8081;
location / {
root /code/port1;
index index.html;
}
listen 8082;
location / {
root /code/port2;
index index.html;
}
}
------------------------/etc/nginx/conf.d/port.conf结束---------------------
nginx -t
systemctl reload nginx
#测试
curl 10.0.0.7:8081
curl 10.0.07:8082
6.基于不同的域名访问
mkdir -p /code/test1
mkdir /code/test2
echo "hello,test" > /code/test1/index.html
echo "test2" > /code/test2/index.html
vim /etc/nginx/conf.d/all.conf
------------------------/etc/nginx/conf.d/all.conf-------------------------
server {
listen 80;
server_name test1.com;
location / {
root /code/test1;
index index.html;
}
server {
listen 80;
server_name test2.com;
location / {
root /code/test2;
index index.html;
}
}
------------------------/etc/nginx/conf.d/all.conf结束----------------------
nginx -t
systemctl reload nginx #加载nginx
在Windows的c:\windows\system32\drives\etc\hosts中添加下两条:
10.0.0.7 test1.com
10.0.0.7 test2.com
在Windows浏览器中分别输入域名test1.com与test2.com即可访问。
如果在Windows中配置了域名解析,但nginx中没有该域名,则会返回/etc/nginx/conf.d/目录下第一个.conf文件中第一个server。
在另外一台Linux系统访问:
vim /etc/hosts
-------------------------/etc/hosts----------------------------------------
10.0.0.7 test1.com
10.0.0.7 test2.com
-------------------------/etc/hosts结束-------------------------------------
curl test1.com
curl test2.com
7.autoindex模块的使用
#
mkdir -p /code/repo #准备repo目录
rsync -avz rsync://rsync.mirrors.ustc.edu.cn/repo/centos/ /code/repo/ #将阿里云的源同步到本地
vim /etc/nginx/conf.d/mirror.conf
-------------------------------/etc/nginx/conf.d/repo.conf---------------------
server {
listen 80;
server_name mirror.com;
charset utf-8; #中文正常显示
root /code;
autoindex on; #转成目录结构显示
location / {
index index.html;
}
location /repo {
autoindex_exact_siza off; #显示大小为M
autoindex_localtime on; #上传时间为本地时间
}
}
-------------------------------/etc/nginx/conf.d/repo.conf结束-----------------
nginx -t
systemctl restart nginx
在Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.7 mirror.com
在Windows的浏览器中输入域名mirror.com即可访问。
8.设置访问用户与密码
#给mirror.com的/repo添加用户密码认证。
vim /etc/nginx/conf.d/mirror.conf
-------------------------------/etc/nginx/conf.d/repo.conf--------------------
server {
listen 80;
server_name mirror.com;
charset utf-8;
root /code;
autoindex on;
location / {
index index.html;
}
location /repo {
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "hello"; #输入用户名密码时候的提示
auth_basic_user_file "/etc/nginx/auth_pass"; #用户名和密码的路径
}
}
-------------------------------/etc/nginx/conf.d/repo.conf结束-----------------
yum install httpd-tools -y #安装HTTP工具
htpdpasswd -cb /etc/nginx/auth_pass root 123456 #生成密码文件
nginx -t
systemctl reload nginx
在Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.7 mirror.com
在Windows的浏览器中输入域名mirror.com即可访问,当访问/repo时需要验证。
9.请求限制
mkdir -p /code/limit
echo "limit" > /code/limit/index.html
vim /etc/nginx/con.f/limit.conf
-------------------------/etc/nginx/con.f/limit.conf----------------------------
#定义了10M的内存空间,名称叫req_one,限制为每秒一个请求,针对来源的IP地址
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
server {
listen 80;
server_name limit.com;
#请求超过1r/s,剩下的延时处理,超过burst数量则返回503
limit_req zone=req_one burst=3 nodelay;
location / {
root /code/limit;
index index.html;
}
}
-------------------------/etc/nginx/con.f/limit.conf结束-------------------------
nginx -t
systemctl reload nginx
在Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.7 limit.com
在Windows的浏览器中输入域名limit.com即可访问,连续刷新即可验证。
10.连接限制
mkdir -p /code/limit #创建目录并放入一个较大的文件
vim /etc/nginx/conf.d/limit.conf
-------------------------/etc/nginx/conf.d/limit.conf------------------
#请求限制:定义了一个10m内存空间,名称叫req_one 限制的速率是 每S 1个请求,针对的来源的IP地址
#limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
#连接限制: 定义了一个10m内存空间,名称叫conn_od,针对的是来源的IP
limit_conn_zone $binary_remote_addr zone=conn_od:10m;
server {
listen 80;
server_name limit.com;
limit_conn conn_od 1; #一个IP限制为一个连接数
limit_rate_after 100m; #前100M不限速
limit_rete 200k; #限速为200k
root /code/limit;
autoindex on;
}
----------------------/etc/nginx/conf.d/limit.conf结束------------------
在Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.7 limit.com
在Windows的浏览器中输入域名limit.com即可访问,点击下载且同时只能下载一个。
11.综合案例
限制web服务器请求数处理为每秒一个,触发值为5;限制用户同时只能下载一个文件,同时下载两个时则提示“充值会员”,当下载超过100M时限速为500K。
mkdir -p /code/limit
vim /etc/nginx/conf.d/limit.conf
-------------------------/etc/nginx/conf.d/limit.conf------------------
#请求限制:定义了一个10m内存空间,名称叫req_one 限制的速率是 每S 1个请求,针对的来源的IP地址
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
#连接限制: 定义了一个10m内存空间,名称叫conn_od,针对的是来源的IP
limit_conn_zone $binary_remote_addr zone=conn_od:10m;
server {
listen 80;
server_name limit.com;
charset utf-8;
limit_req zone=req_one burst=5 nodelay;
limit_conn conn_od 1; #一个IP限制为一个连接数
limit_rate_after 100m; #前100M不限速
limit_rete 200k; #限速为200k
root /code;
autoindex on;
error_page 503 @err;
location @err {
default_type text/html;
return 200 '联系管理员充值会员';
}
}
----------------------/etc/nginx/conf.d/limit.conf结束------------------
nginx -t
systemctl reload nginx
在Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.7 limit.com
在Windows的浏览器中输入域名limit.com即可访问验证。
12.状态模块stub_status
mkdir /code
vim /etc/nginx/conf.d/game.conf
--------------------/etc/nginx/conf.d/game.com----------------------
server {
listen 80;
server_name game.com;
location / {
root /code;
index index.html;
}
#开启Nginx的状态监控
location /status {
stub_status;
}
}
--------------------/etc/nginx/conf.d/game.com结束-------------------
nginx -t #检查语法错误
systemctl start nginx #启动nginx
Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.7 game.com #nginx的服务器的IP,与server_name
在浏览器中输入域名game.com/status即可查看。
| 状态 | 含义 |
|---|---|
| Active connections | 当前活跃的连接数,包括waiting等待数 |
| accepts | 已接收的总TCP连接数 |
| handled | 已处理的TCP连接数 |
| requests | 当前总HTTP请求数 |
| Reading | 当前读取的请求头数量 |
| writing | 当前响应的请求头数量 |
| waiting | 当前等待请求的空闲客户端连接数 |
13.location的匹配规则与内部重定向
| 匹配符 | 匹配规则 | 优先级 |
|---|---|---|
| = | 精准匹配 | 1 |
| ^~ | 以某个字符开头 | 2 |
| ~ | 区分大小写 | 3 |
| ~* | 不区分大小写 | 4 |
| / | 通用匹配 | 5 |
mkdir -p /code/location
vim /etc/nginx/con.d/location.conf
------------/etc/nginx/con.d/location.conf------------------
server {
listen 80;
server_name location.com;
# 通用匹配,任何请求都会匹配到
location / {
root /code;
index index.html;
}
# 精准匹配,必须请求的uri是/nginx_status
location = /nginx_status {
stub_status;
}
# 严格区分大小写,匹配以.php结尾的都走这个location xx.com/1.php
location ~ \.php$ {
default_type text/html;
return 200 'php访问成功';
}
# 严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ \.jsp$ {
default_type text/html;
return 200 'jsp访问成功';
}
# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
location ~* \.(jpg|gif|png|js|css)$ {
return 403;
}
# 不区分大小写匹配
location ~* \.(sql|bak|tgz|tar.gz|.git)$ {
deny all;
}
error_page 404 403 401 @err;
location @err {
default_type text/html;
return 200 '你可能是不小心走丢了。';
}
}
------------/etc/nginx/con.d/location.conf结束---------------
14.location路劲加 / 的区别
#不添加/,默认上/code/test目录下找index.html文件,如果没有 index.html则会查找/code/test文件
location /test {
root /code;
index index.html;
}
#添加/,默认上/test目录下寻找index.html文件,如果没有index.html 则会直接返回403
location /test/ {
root /code;
}
15.nginx的日志
#log_format:定义日志格式
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;
| 变量名 | 作用 |
|---|---|
| $remote_addr | 记录客户端的IP |
| $remote_user | 记录客户端用户名 |
| $time_local | 记录通用的本地时间 |
| $time_iso8601 | 记录ISO8601标准格式下的本地时间 |
| $request | 记录请求的方法以及请求的http协议 |
| $status | 记录请求状态码(用于定位错误信息) |
| $body_bytes_sent | 发送给客户端的资源字节数,不包括响应头的大小 |
| $bytes_sent | 发送给客户端的总字节数 |
| $msec | 日志写入时间。单位为秒,精度是毫秒。 |
| $http_referer | 记录从哪个页面链接访问过来的 |
| $http_user_agent | 记录客户端浏览器相关信息 |
| $http_x_forwarded_for | 记录客户端IP地址 |
| $request_length | 请求的长度(包括请求行, 请求头和请求正文)。 |
| $request_time | 请求花费的时间,单位为秒,精度毫秒 |
access_log 可以定义在 http、server、location
#每一个server都会定义一个access_log。为了区分网站的访问记录
http {
access_log /var/log/nginx/access.log main;
server {
# access_log /var/log/nginx/log.oldxu.com.log main;
}
#如果某个网站不想记录日志,则可以通过如下两种方式去实现?
server {
access_log off;
#access_log /dev/null;
}
}
#错误日志几乎是所有Nginx统一的一个位置。(全局,作用于所有的网站)
error_log /var/log/nginx/error.log warn;
469

被折叠的 条评论
为什么被折叠?



