linux --nginx篇
NGINX是什么?
nginx是开源的,支持高性能的,高并发的www服务和代理服务软件,就是web服务器,nginx不但是一个优秀的web服务软件,还可以做反向代理,负载均衡,以及缓存服务使用.
优点:支持高并发,支持几万的并发连接;资源消耗少.在3万并发连接下开启10个nginx线程消耗内存不到200M;可以做负载均衡,反向代理;支持异步网络I/O事件模型epoll
Tengine是淘宝网的web项目,是Nginx的高性能版,tengine的性能和稳定性极高.
web服务器和web框架的关系
web服务器(nginx):接收HTTP请求(例如www.alonemans.com/dudu.jpg)并返回数据
web框架(django,flask):开发web应用程序,处理接收到的数据
基础安装
- 下载源码包 --- wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
- 卸载掉之前通过yum源安装的nginx -- yum remove nginx -y
- 解决编译安装的依赖环境
yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y
- 解压缩源码包 --- tar -zxf tengine-2.2.0.tar.gz
- 释放Makefile --- ./configure --prefix=/opt/tnginx220
- 执行 make && make install
- 编译完成后 tnginx可以正常使用
- 了解tnginx220的目录结构作用
/opt/tnginx220 [root@master tnginx220]# ll drwxr-xr-x. 2 root root 4096 Mar 11 08:50 conf #放nginx所有配置文件的地儿
drwxr-xr-x. 2 root root 40 Mar 11 08:50 html #存放前端 html文件的
drwxr-xr-x. 2 root root 4096 Mar 11 08:50 include
drwxr-xr-x. 2 root root 41 Mar 11 08:52 logs #nginx的日志文件夹
drwxr-xr-x. 2 root root 6 Mar 11 08:50 modules drwxr-xr-x. 2 root root 35 Mar 11 08:50 sbin #存放nginx二进制命令的 - 添加linux环境变量PATH,使用快捷命令
echo $PATH
PATH="/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/tnginx220/sbin" #将tnginx220 的路径添加到PATH中,在后面添加即可 编辑 #vim /etc/profile
添加后执行 #source /etc/profile - nginx亦可以正常使用了
nginx.conf配置文件相关
http内核模块 //公共的配置定义在http{}
http { //http层开始
...
//使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
'server' {
listen 80; //监听端口, 默认80
server_name localhost; //提供服务的域名或主机名
access_log host.access.log //访问日志
//控制网站访问路径
'location' / {
root /usr/share/nginx/html; //存放网站代码路径
index index.html index.htm; //服务器返回的默认页面文件
}
//指定错误代码, 统一定义错误页面, 错误代码重定向到新的Locaiton
error_page 500 502 503 504 /50x.html;
}
...
//第二个虚拟主机配置
'server' {
...
} include /etc/nginx/conf.d/*.conf; //包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件 } //http层结束
基于域名的多虚拟主机实战
也就是在一个服务器上运行多个网站
方法:
1.环境准备,准备好两个域名,这里模拟的本地域名解析,找到windows下的hosts文件( 由于我们是通过windows访问,达到访问不同的 域名,因此配置windows的hosts)
#编辑文件 C:\Windows\System32\drivers\etc\hosts #写入
192.168.11.229 meihao.com
192.168.11.229 shenghuo.com
2.配置nginx支持多虚拟主机
-- 修改nginx.conf 修改2个server虚拟主机配置
#meihao的虚拟主机
server {
listen 80;
server_name meihao.com;
# 当我们访问meihao.com:80/的时候,就进入这个虚拟主机,且找到这个location,进行网站资源分配
location / {
root /opt/meihao/;
index index.html;
}
} #第二个虚拟主机,shenghuo
server{
listen 80;
server_name shenghuo.com; location / {
root /opt/shenghuo/;
index index.html;
} }
--分别修改两个网址的根目录数据
mkdir -p /opt/{meihao,shenghuo} #分别在/opt/meihao/创建 index.html;/opt/shenghuo/创建 index.html
--修改完配置文件,先检测语法 nginx -t
-- 平滑加载nginx(不重启nginx,重新读取配置文件) nginx -s reload
nginx的访问日志logs功能
vim nginx.conf
打开注释 http {
include 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 logs/access.log main; #开启日志功能,生成access.log文件,可以监控访问者
nginx的404网页优化
编辑nginx.conf server {
listen 80;
server_name meihao.com; #charset koi8-r; #access_log logs/host.access.log main; location / {
root /opt/meihao;
index index.html index.htm;
}
#打开这个参数的配置,开启错误页面
error_page 404 403 402 401 /40x.html; #40几的错误会对应生成40x.html
}
记得在 /opt/meihao/ 创建一个40x.html 文件 --- touch 40x.html ,写入你的创意404界面...嘿嘿要美观,当你访问的meihao.com/asdfasdas 的时候自动跳转的温馨的404页面
nginx拒绝ip访问
location / {
deny 你想限制的ip; #!!!还可以限制网段
root /opt/meihao;
index index.html index.htm;
allow 10.0.0.1; #限制网段访问
}
nginx反向代理
什么是反向代理?
对于客户端而言,代理服务端就是原始服务端,实际来讲,就是为了保护和隐藏原始服务器不受到攻击.
反向代理的实现:
- 环境准备:两台服务器
192.168.220.130 #真是资源服务器 192.168.220.128 #nginx代理服务器
- 我们作为客户端,访问代理服务器,代理服务器,将资源服务器上的东西进行返回
- 先配置资源服务器 192.168.220.130 meihao.com
- 配置代理服务器 192.168.220.128 修改192.168.220.128 这台机器上的配置文件,开始反向代理
#配置 nginx.conf 的server{}如下: server {
listen 80 ;
server_name _; location / {
#反向代理参数,当我们请求192.168.11.136:80/的时候,进入这里server,然后location进行资源分配
proxy_pass http://192.168.220.130; #就是间接的访问了 192.168.220.130:80/
} }
nginx负载均衡
配置nginx负载均衡环境准备,三台服务器,这三台服务器都是nginx实现的
192.168.220.128 反向代理服务器
192.168.220.130 资源服务器 返回xiaohua页面
192.168.220.131 资源服务器 返回一个h1标签,且行且珍惜
实现过程:
- 配置反向代理 192.168.220.128服务器 修改nginx.conf参数如下
#定义负载均衡池 upstream load_leveling_pool{
server 192.168.220.130;
server 192.168.220.131;
} #转发请求给负载均衡池
location /{
proxy_pass http:// load_leveling_pool; } 此时通过负载均衡器 192.168.220.128.进行测试访问,默认是轮询机制
- nginx负载均衡算法
轮询 #按照时间顺序逐一分配到不同的服务器(默认) weight #加权轮询,weight值越大,分配到的访问几率越高 upstream s17server {
server 192.168.220.130 weight=8;
server 192.168.220.131 weight=2;
} #此时 192.168.220.130访问的几率比较高了 ip_hash #每个请求按照ip的hash结果分配,这样来自同一ip的固定访问最后一个服务器 url_hash #按照访问的urlhash结果来分配,是每个固定的url定向到一个后端服务器 least_conn #最少链接数,哪个机器链接数少就分发
....
最新文章
- OSPF协议详解
- Nginx日志常用分析命令汇总 (转)
- vim中多标签和多窗口的使用
- Android中log4j的运用
- ViewPager 详解(二)---详解四大函数
- Grok 正则捕获
- Spring MVC ajax:post/get 的具体实现
- Unity3d中的PlayerPrefs游戏存档API的扩展
- svn解除控制
- 国内常用DNS
- PostMan如何做Post请求测试
- Maximum Average Subarray I
- vue学习(3)
- python深浅拷贝与赋值
- django 数据库查询 ORM
- EF6 DbModelBuilder
- ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath、
- (利用DOM)在新打开的页面点击关闭当前浏览器窗口
- day19 IO编程
- python 字符串截断
热门文章
- iOS 随机数获取
- grid - 隐式地命名网格区域名称
- CSAPP Tiny web server源代码分析及搭建执行
- 安装rcssmin方法
- np.memmap读取大文件
- CoreGraphics之CGContextSaveGState与UIGraphicsPushContext
- Effective Java 第三版——64. 通过对象的接口引用对象
- Mybatis抛出:Cannot obtain primary key information from the database, generated objects may be incomplete
- CentOS 7.4安装nodejs &; nginx &; pm2
- Software Engineer Title Ladder