Nginx 基础使用

kyang MVP++

在这里插入图片描述

目录结构


进入Nginx的主目录我们可以看到这些主要文件夹

1
conf html logs sbin

conf

用来存放配置文件相关

html

用来存放配置文件相关

sbin

nginx的主程序

Nginx配置与应用场景


最小配置

worker_processes

worker_processes 1;默认为1,表示开启一个业务进程
#### work_connections
work_connections 1024;单个业务进程可以接受连接数
#### include
include mime.types; 引入http mime类型
#### default_type
default_type application/octet-stream; 如果mime类型没匹配上,默认使用二进制流的方式传输。
#### sendfile
sendfile on; 使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝。
#### keepalive_timeout 65;
#### server
1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80; 监听端口号
server_name localhost; 主机名
location / { 匹配路径
root html; 文件根目录
index index.html index.htm; 默认页名称
}

error_page 500 502 503 504 /50x.html; 报错编码对应页面
location = /50x.html {
root html;
}
}

### servername匹配规则
我们需要注意的是servername匹配分先后顺序,写在前面的匹配上就不会继续往下匹配了。
#### 完整匹配
1
server_name www.xxx.com www.xxx.net

#### 通配符匹配
1
sever_name *.xxx.com

#### 正则匹配
1
server_name ~^[a-z]+\.xxx\.com$;

## 反向代理

1
2
3
location / {
proxy_pass http://xxx.com/;
}

基于反向代理的负载均衡

1
2
3
4
upstream httpd {
server 192.168.xx1.yy1:80;
server 192.168.xx2.yy2:80;
}

轮询

weight(权重)

指定轮询的几率,weigth和访问比率成正比,用于性能不均的情况

1
2
3
4
5
upstream httpd {
server 192.168.xx1.yy1:80; weight=10 down;
server 192.168.xx2.yy2:80; weight=1;
server 192.168.xx3.yy3:80; weight=1 backup;
}
  • down:不参与负载
  • weight:默认为1,weight越大,负载的权重就越大。
  • backup: 备用

ip_hash

根据ip计算hash值,选择server

url_hash

根据url计算hash值,选择server

least_conn

最少连接

动静分离

配置

1
2
3
4
5
location / {
proxy_pass http://127.0.0.1:8080;
root html;
index index.html index.htm;
}

增加location

1
2
3
4
location /css {
root /usr/local/nginx/static;
index index.html index.htm;
}

location的使用

location 的前缀
/匹配所有请求
=精准匹配
~正则匹配,区分大小写
~*正则匹配,不区分大小写
^~非正则,匹配以指定模式开头
  • 匹配循序
    • 多个正则location直接按书写顺序匹配,成功后就不会继续往后面匹配
    • 普通(非正则)location会一直往下,直到找到匹配度最高的(最大前缀匹配)
    • 当普通location与正则location同时存在,如果正则匹配成功,则不会再执行普通匹配
    • 所有类型location存在时,“=”匹配 > “^~”匹配 > 正则匹配 > 普通(最大前缀匹配)

alias与root

1
2
3
4
5
location /css { 

alias /usr/local/nginx/static/css;
index index.html index.htm;
}

root用来设置根目录,而alias在接受请求的时候在路径上不会加上location。

UrlRewrite

1
2
rewrite		<regex>		<replacement>		[flag]
关键字 正则 替代内容 flag标记

flag标记说明:

  • last 本条规则匹配完成后,继续向下匹配新的location URI规则
  • break 本条规则匹配完成即终止,不再匹配后面的任何规则
  • redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址
  • permanent 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
1
rewrite		^/([0-9]+).html$		/index.jsp?pageNum=$1		break;

同时使用负载均衡

应用服务器防火墙配置

  • 开启防火墙
    systemctl start firewalld
  • 重启防火墙
    systemctl restart firewalld
  • 重载规则
    firewall-cmd --reload
  • 查看已配置规则
    firewall-cmd --list-all
  • 指定端口和ip访问
    firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.xx.yy" port protocol="tcp" port="8080" accept"
  • 移除规则
    firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.xx.yy" port port="8080" protocol="tcp" accept"

网关配置

1
2
3
4
5
6
7
8
9
10
upstream httpds {  
server 192.168.xx1.yy1 weight=8 down;
server 192.168.xx2.yy2:8080 weight=2;
server 192.168.xx3.yy3:8080 weight=1 backup;
}

location / {
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 redirect;
proxy_pass http://httpds ;
}

防盗链配置

1
valid_referers none | blocked | server_names | strings ....;
  • none, 检测 Referer 头域不存在的情况。
  • blocked,检测 Referer 头域的值被防火墙或者代理服务器删除或伪装的情况。这种情况该头域的值不以
  • “http://” 或 “https://” 开头。
  • server_names ,设置一个或多个 URL ,检测 Referer 头域的值是否是这些 URL 中的某一
    1
    2
    3
    4
    valid_referers 192.168.xx.yy;
    if ($invalid_referer) {
    return 403;
    }
  • 标题: Nginx 基础使用
  • 作者: kyang
  • 创建于 : 2024-09-17 15:56:08
  • 更新于 : 2025-07-11 16:56:12
  • 链接: https://blog.kyang.top/2024/09/17/nginx配置的简单介绍/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论