nginx配置的简单介绍

Nginx 基础使用


在这里插入图片描述

目录结构


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

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
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匹配分先后顺序,写在前面的匹配上就不会继续往下匹配了。

完整匹配
server_name www.xxx.com www.xxx.net
通配符匹配
sever_name *.xxx.com
正则匹配
server_name ~^[a-z]+\.xxx\.com$;

反向代理


location / {
	proxy_pass http://xxx.com/;
}

基于反向代理的负载均衡

upstream httpd {
	server 192.168.xx1.yy1:80;
	server 192.168.xx2.yy2:80;
}
轮询
weight(权重)

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

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

最少连接

动静分离

配置
location / {
	proxy_pass http://127.0.0.1:8080;
	root html;
	index index.html index.htm;
}
增加location
location /css {
	root /usr/local/nginx/static;
	index index.html index.htm;
}
location的使用
location 的前缀
/匹配所有请求
=精准匹配
~正则匹配,区分大小写
~*正则匹配,不区分大小写
^~非正则,匹配以指定模式开头
  • 匹配循序
    • 多个正则location直接按书写顺序匹配,成功后就不会继续往后面匹配
    • 普通(非正则)location会一直往下,直到找到匹配度最高的(最大前缀匹配)
    • 当普通location与正则location同时存在,如果正则匹配成功,则不会再执行普通匹配
    • 所有类型location存在时,“=”匹配 > “^~”匹配 > 正则匹配 > 普通(最大前缀匹配)

alias与root

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

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

UrlRewrite

rewrite		<regex>		<replacement>		[flag]
关键字		正则			替代内容				flag标记
flag标记说明:
  • last 本条规则匹配完成后,继续向下匹配新的location URI规则
  • break 本条规则匹配完成即终止,不再匹配后面的任何规则
  • redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址
  • permanent 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
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"
网关配置
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 ; 
 } 

防盗链配置

valid_referers none | blocked | server_names | strings ....;
  • none, 检测 Referer 头域不存在的情况。
  • blocked,检测 Referer 头域的值被防火墙或者代理服务器删除或伪装的情况。这种情况该头域的值不以
  • “http://” 或 “https://” 开头。
  • server_names ,设置一个或多个 URL ,检测 Referer 头域的值是否是这些 URL 中的某一
valid_referers 192.168.xx.yy;
if ($invalid_referer) {
	return 403;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值