kylin-Nginx部署、常用模块及location匹配
Nginx服务
Nginx服务安装
-
使用系统默认的仓库安装
- 版本较低;配制不易读;
- 直接安装即可:
[root@web01 ~]# yum install -y nginx [root@web01 ~]# nginx -v nginx version: nginx/1.21.5 -
基于官网仓库安装
- 版本较新;配置易读;
- 安装方式:
第一步:配置Nginx官方仓库 官方仓库配置网址:https://nginx.org/en/linux_packages.html#RHEL 创建并编辑仓库配置文件: [root@web02 ~]# vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true 第二步:安装Nginx服务 [root@web02 ~]# yum install -y nginx #查看下载过程中使用的仓库是否为刚刚设置的Nginx仓库 Installing: nginx x86_64 1:1.26.1-2.el7.ngx nginx-stable 807 k #检查版本 [root@web02 ~]# nginx -v nginx version: nginx/1.26.1- 仓库配置文件内容详解:
[nginx-stable] #YUM仓库的名称 name=nginx stable repo #仓库的描述 baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ #仓库的基础 URL指定软件包存放的位置;$basearch为变量,代表系统版本,如使用kylin直接替换为7即可。 gpgcheck=0 #是否对软件包进行签名验证 enabled=1 #是否启用此仓库 gpgkey=https://nginx.org/keys/nginx_signing.key #验证公钥 module_hotfixes=true #模块热修复功能 -
编译方式安装
- 需要其他功能模块的时候;自定义安装;
- 参考:https://blog.csdn.net/ziqibit/article/details/129560966
Nginx的启动方式
- 注意:同时只能用一种方式来运行Nginx服务
1.使用systemctl方式启动 调用的绝对路径的命令运行
2.使用绝对路径的方式运行
[root@web01 ~]# which nginx
/usr/sbin/nginx
#启动nginx
/usr/sbin/nginx
#启动Nginx
[root@web01 ~]# /usr/sbin/nginx -s stop
# 重载Nginx
[root@web01 ~]# /usr/sbin/nginx -s reload
配置Nginx
查看Nginx的的配置文件
[root@web02 ~]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf #server区块,网站的配置包含在主配置中
/etc/nginx/fastcgi_params
/etc/nginx/mime.types #Nginx网站支持的文件类型
/etc/nginx/nginx.conf #Nginx的主配置文件
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
主配置文件nginx.conf详解
[root@web02 ~]# vim /etc/nginx/nginx.conf
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; # 文件传输
#tcp_nopush on;
keepalive_timeout 65; # 长连接 65秒自动和浏览器断开
#gzip on; # 是否资源压缩
include /etc/nginx/conf.d/*.conf; # 将conf.d下的*.conf引入到当前的文件
}
server区块default.conf详解
[root@web02 ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80; # 监听的端口
server_name www.oldboy.com; # 自己购买的域名 hosts解析
location / { # 路径匹配 www.oldboy.com/
root /code; # 让用户去/code目录获取网站信息
index index.html; # 默认给浏览器返回的文件 index.html
}
}
部署Nginx的web网站
第一步:基于官网仓库安装Nginx
第二步:根据需求配置Nginx的主配置文件/etc/nginx/nginx.conf
第三步:配置server区块文件;根据需求配置端口、域名、代码目录等
第四步:检查Nginx的语法
[root@web02 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
第五步:创建代码目录,上传代码,解压
[root@web02 ~]# ll /code/
total 7708
-rw-r--r-- 1 root root 7890373 Dec 6 11:34 game.zip
[root@web02 /code]# unzip game.zip
[root@web02 /code]# ll
-rw-r--r-- 1 root root 28032 May 24 2021 bgm.mp3
drwxr-xr-x 2 root root 23 May 24 2021 css
-rw-r--r-- 1 root root 7890373 Dec 6 11:34 game.zip
drwxr-xr-x 2 root root 23 May 24 2021 images
-rw-r--r-- 1 root root 8956 May 24 2021 index.html
drwxr-xr-x 2 root root 213 May 24 2021 js
drwxr-xr-x 2 root root 4096 May 24 2021 roms
-rw-r--r-- 1 root root 811 May 24 2021 shuoming.html
第六步:启动Nginx服务,检查端口
[root@web02 ~]# systemctl start nginx
[root@web02 ~]# systemctl enable nginx
[root@web02 ~]# netstat -tnulp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2521/nginx: maste
访问网站后常见的错误代码
HTTP 404 # 代码目录不存在
HTTP 403 # 有代码目录但是没有文件
配置Nginx多业务
基于多IP(了解)
为一台服务器配置多个IP地址,访问不同IP地址,即不同业务;
现规划如下:
10.0.0.8-------小霸王业务
10.0.0.9-------坤坤打篮球业务
第一步:配置一个ip地址
[root@web02 ~]# ip add add 10.0.0.9/24 dev ens33
第二步:修改两个服务的server区块配置文件
[root@web02 ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 10.0.0.8:80;
server_name _;
location / {
root /code;
index index.html;
}
}
[root@web02 ~]# vim /etc/nginx/conf.d/kunkun.conf
server {
listen 10.0.0.9:80;
server_name _;
location / {
root /kunkun;
index index.html;
}
}
第三步:上传代码至对应目录,并解压
第四步:检查配置文件
[root@web02 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
第五步:重启Nginx服务
[root@web02 ~]# systemctl restart nginx
第六步:测试访问
基于多端口(常用)
通过选择不同端口运行业务,也可实现
#修改配置文件中的端口即可
[root@web02 ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name _;
location / {
root /code;
index index.html;
}
}
[root@web02 ~]# vim /etc/nginx/conf.d/kunkun.conf
server {
listen 81;
server_name _;
location / {
root /kunkun;
index index.html;
}
}
基于多域名(常用)
设置不同域名从而实现多业务
#修改配置文件的域名即可
[root@web02 ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name dezyan.cn;
location / {
root /code;
index index.html;
}
}
[root@web02 ~]# vim /etc/nginx/conf.d/kunkun.conf
server {
listen 80;
server_name free.dezyan.cn;
location / {
root /kunkun;
index index.html;
}
}
Nginx常用模块
1.auto_index索引模块
-
语法:
autoindex on | off -
默认(不添加时):
autoindex off -
可使用的区块:
http、server、location -
功能描述:启用或禁用目录列表的输出(即在网页展示代码目录下的所有目录)
-
常用参数:
autoindex_exact_size off; #默认为on,显示文件具体大小,单位为bytes
#修改为off,显示文件大概大小,单位为kb,mb,gb
autoindex_localtime on; #默认为off,为GMT时间;
#修改为on,为服务器时间;
charset utf-8,gbk; #设置字符集,解决中文乱码;
- 示例:
server {
listen 80;
server_name _;
location / {
root /code/index;
index index.html;
#开启后访问web,即可查看到/code/index,目录下所有的目录及文件
autoindex on;
#关闭后可查看到文件的大概大小(kb,mb,gb)
autoindex_exact_size off;
#设置后解决中文目录、文件乱码问题
charset utf-8,gbk;
#修改为服务器时间
autoindex_localtime on;
}
}
2.limit_rate下载限制模块
-
语法:
limit_rate sizelimit_rate_after size -
默认(不添加时):
limit_rate 0 limit_rate_after 0 -
可使用的区块:
http、server、location、if in location -
功能描述:限制从服务器下载资源的速度;当已下载的文件大小为size时,启用限速;
-
示例:
server {
listen 80;
server_name _;
location / {
root /code/index;
index index.html;
autoindex on;
autoindex_exact_size off;
charset utf-8,gbk;
autoindex_localtime on;
#当下载了20m的资源后,限制下载速度为1m/s
limit_rate_after 20m;
limit_rate 1m;
}
}
3.auth_basic用户登录验证模块
-
作用:登录网站是需要提供用户名和密码
-
密码文件配置方式
- htppasswd生成密码文件
1.安装htpasswd命令所需的软件包 [root@web01 ~]# yum install httpd-tools 2.创建新的密码文件, -c创建新文件 -b允许命令行输入密码 [root@web01 ~]# htpasswd -b -c /etc/nginx/auth_conf ding ding Adding password for user ding -
模块配置方式
- 语法:
auth_basic "字符串" | off#用户提示符auth_basic_user_file auth.conf#指定用户名密码文件位置
- 默认(不添加时):均为off状态
- 可使用的区块:
http、server、location、limit_except - 功能描述:用户访问web需输入用户名及密码才可访问
- 示例
server { listen 80; server_name _; location / { root /code/index; index index.html; autoindex on; autoindex_exact_size off; charset utf-8,gbk; autoindex_localtime on; #启用基本认证并设置认证区域 auth_basic "测试Nginx"; #指定用户名和密码文件 auth_basic_user_file auth_conf; } } - 语法:
4.stub_status---Nginx状态监控(记忆)
- 常用设置方法:
#在模块配置文件中添加location模块:
location /nginx_status {
stub_status;
}
#web访问http://10.0.0.7/nginx_status即可看到
Active connections # 当前活动的连接数
accepts # 已接收的总TCP连接数量
handled # 已处理的TCP连接数量
requests # 当前处理http请求的总数
Reading # 当前读取请求头数量
Writing # 当前响应的请求头数量
Waiting # 等待的请求数,开启了keepalive
5.使用IP的访问限制
-
语法:
allowdeny -
示例:
- 访问控制配置示例,拒绝指定的IP,其他全部允许
location /nginx_status { stub_status; deny 10.0.0.1; #不允许IP10.0.0.1访问 allow all; #其他全部允许 }- 访问控制配置示例, 只允许谁能访问, 其它全部拒绝
location /nginx_status { stub_status; allow 10.0.0.0/24; #允许10.0.0.0/24网段访问 allow 127.0.0.1; #允许本地访问 deny all; #其他全部拒绝访问 }
6.TCP连接数限制
-
语法:
limit_conn_zone key zone=name:size;limit_conn zone number; -
可用区块:
http, server, location -
功能描述:需在http中设置限制模块名称(limit_conn_zone)客户端IP(key)开辟内存空间名称(zone=name)空间大小(size);然后在http, server, location中设置:限制空间里的所有IP同一时间的连接数;
-
示例:
limit_conn_zone $remote_addr zone=conn_zone:10m rate=20r/s;
#限制模块名称 客户端IP 开辟内存空间名称叫conn_zone 空间大小10m 每秒最多20个请求
server {
listen 80;
server_name www.game.com;
location / {
root /game;
index index.html;
limit_conn conn_zone 1; #限制空间里的所有IP同一时间只有1个TCP连接
limit_req zone=req_zone burst=5 nodelay; # 限制延时处理5个,剩余的返回503
}
- 实战
1.在公网nginx中配置
http{ #http层,设置
# Limit settings
limit_conn_zone $remote_addr zone=conn_zone:10m;
server{
#server层调用
#连接限制,限制同时最高1个连接
limit_conn conn_zone 1;
}
}
2.使用 ab 工具进行压力测试
[root@web01 ~]# yum install -y httpd-tools
[root@web01 ~]# ab -n 20 -c 2 http://127.0.0.1/index.html
3.nginx日志结果
2018/10/24 18:04:49 [error] 28656#28656: *1156 limiting connections by zone "conn_zone", client:
123.66.146.123, server: www.oldboy.com, request: "GET / HTTP/1.0", host: "www.oldboy.com"
7.nginx请求限制重定向
- 修改默认返回状态码
server {
listen 80;
server_name module.oldboy.com;
charset utf-8,gbk;
location / {
root /code;
index index.html index.htm;
limit_req zone=req_zone burst=3 nodelay;
#修改返回状态码为:478
limit_req_status 478
}
}
- 页面太丑,重定向页面
server {
listen 80;
server_name module.oldboy.com;
charset utf-8,gbk;
location / {
root /code;
index index.html index.htm;
limit_req zone=req_zone burst=3 nodelay;
limit_req_status 478
#重定错误页面
error_page 478 /err.html;
}
}
vim /code/err.html
<img style='width:100%;height:100%;' src=https://www.linuxnc.com/478_page.png>
location匹配规则优先级(记忆)
| 匹配符 | 匹配规则 | 优先级 |
|---|---|---|
| = | 精确匹配 | 1 |
| ^~ | 以某个字符串开头 | 2 |
| ~ | 区分大小写的正则匹配 | 3 |
| ~* | 不区分大小写的正则匹配 | 4 |
| / | 通用匹配,任何请求都会匹配到 | 5 |
- 配置验证
[root@Nginx conf.d]# cat testserver.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
default_type text/html;
return 200 "location /";
}
location =/ {
default_type text/html;
return 200 "location =/";
}
location ~ / {
default_type text/html;
return 200 "location ~/";
}
location ^~ / {
default_type text/html;
return 200 "location ^~";
}
}
- 测试Location效果
# 优先级最高符号=
[root@Nginx conf.d]# curl www.oldboy.com
location =/
# 注释掉精确匹配=, 重启Nginx
[root@Nginx ~]# curl www.oldboy.com
location ~/
# 注释掉~, 重启Nginx
[root@Nginx ~]# curl www.oldboy.com
location /
# 通用匹配,任何请求都会匹配到
location / {
...
}
# 严格区分大小写,匹配以.php结尾的都走这个location
location ~ \.php$ {
...
}
# 严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ \.jsp$ {
...
}
# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
location ~* .*\.(jpg|gif|png|js|css)$ {
...
}
location ~* \.(jpg|gif|png|js|css)$ {
...
}
# 不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
...
}
Nginx的内置参数
$args #这个变量等于请求行中的参数。
$content_length #请求头中的Content-length字段。
$content_type #请求头中的Content-Type字段。
$document_root #当前请求在root指令中指定的值。
$host #请求主机头字段,否则为服务器名称。
$http_user_agent #客户端agent信息
$http_cookie #客户端cookie信息
$limit_rate #这个变量可以限制连接速率。
$request_body_file #客户端请求主体信息的临时文件名。
$request_method #客户端请求的动作,通常为GET或POST。
$remote_addr #客户端的IP地址。
$remote_port #客户端的端口。
$remote_user #已经经过Auth Basic Module验证的用户名。
$request_filename #当前请求的文件路径,由root或alias指令与URI请求生成。
$query_string #与$args相同。
$scheme #HTTP方法(如http,https)。
$server_protocol #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr #服务器地址,在完成一次系统调用后可以确定这个值。
$server_name #服务器名称。
$server_port #请求到达服务器的端口号。
$request_uri #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri #不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri #与$uri相同。
$X-Forwarded-For:HTTP的请求端真实的IP,只有在通过了HTTP 代理或者负载均衡服务器时才会添加该项。标准格式如下:
X-Forwarded-For: client1, proxy1, proxy2
本文来自博客园,作者:丁志岩,转载请注明原文链接:https://www.cnblogs.com/dezyan/p/18784451

浙公网安备 33010602011771号