Nginx 学习
1.Nginx编译安装
nginx依赖于pcre库,需要先安装pcre(二进制包,跟正则表达式有关),pcre-devel(头文件)
configure --prefix=/usr/local/nginx make && make install
Nginx:
主进程(master)功能:
1.读取并验证配置信息;
2.创建、绑定及关闭套接字;
3.启动、终止及维护worker进程;
4.无须终止服务而重新配置工作特性;
5.控制程序非中断升级;
6.重新打开日志文件,实现日志滚动;
7.编译嵌入式perl脚本;
工作进程(worker)功能:
1.接收、传入并处理来自客户端的连接;
2.提供反向代理及过滤功能;
3.nginx任何能完成的其他功能;
cache loader进程主要完成的任务包括:
1.检查缓存存储中的缓存对象;
2.使用缓存元数据建立内存数据库;
cache manager进程的主要任务:
1.缓存的失效及过期检验
事件驱动模型:
最著名的三个:epoll,kqueue,/dev/poll
sendfile
nginx的某些模块包依赖pcre-devel(perl的正则表达式)
worker数目如果是CPU密集型,worker应该与CPU数相同,若是计算密集型,那么是CPU数的1.5或者2倍
location URI {}:对当前路径及子路径下的所有对象都生效;
location = URI {}:精确匹配指定的路径,不包括子路径,因此,只对当前资源生效;
location ~URI {}:
location ~*URI {}:模式匹配,使用正则表达式,*表示不区分大小写;
location ^~URI{}:不使用正则表达式,不做原字符匹配
= 优先级最高
非正则表达式的次之
正则表达式
不加任何符号的
基于IP的用户访问限制
基于用户的用户访问限制
htpasswd -c -m /etc/nginx/.users tom
auth_basic "Restricted Area..."
auth_basic_user_file /etc/nginx/.users;
这样访问的时候,网页需要输入用户名和密码才能访问
2.Nginx配置管理
#user nobody;
worker_processes 1; //工作子进程数,一般设置成CPU数*核数 #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} 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; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }
基于域名和端口的虚拟主机管理
例子1: 基于域名的虚拟主机
server {
listen 80; #监听端口
server_name a.com; #监听域名
location / {
root /var/www/a.com; #根目录定位
index index.html;
}
}
例子2: 基于端口的虚拟主机配置
server {
listen 8080;
server_name 192.168.1.204;
location / {
root /var/www/html8080;
index index.html;
}
}
3.日志管理
针对不同的server,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 logs/access_8080.log mylog;
声明log log位置 log格式;
将日志每天进行重命名然后重新生成,避免日志太大解析不方便
#!/bin/bash
base_path='/usr/local/nginx/logs'
log_path=$(date -d yesterday +"%Y%m")
day=$(date -d yesterday +"%d")
mkdir -p $base_path/$log_path
mv $base_path/access.log $base_path/$log_path/access_$day.log
#echo $base_path/$log_path/access_$day.log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
4.location定位语法
location / {
root html;
index index.htm index.html;
}
location = /index.htm {
root /usr/local/nginx/html/bbs/;
index index.html;
}
访问http://10.160.65.44/,得到结果:This is /usr/local/nginx/html/bbs/index.html
访问http://10.160.65.44/index.htm,得到结果:This is /usr/local/nginx/html/bbs/index.html
location / {
root html;
index b.htm;
}
location = /b.htm {
root /usr/local/nginx/html/bbs/;
index b.html;
}
location的命中原则:
1.精准命中,命中则返回结果
2.判断普通命中,如果有多个命中,记录最长的命中结果,普通命中顺序无所谓,按照长短来匹配。
3.继续判断正则表达式的解析结果,按配置里的正则表达式顺序为准,由上到下开始匹配,一旦匹配成功,立即返回结果并结束。正则匹配有所谓,
因为按照顺序来匹配。
5.rewrite的写法
在location中添加rewrite的语法,下面来自10.140.65.135的访问都会被forbidden。
location / {
if ( $remote_addr = 10.140.65.135 ){
return ;
}
root html;
index index.htm index.html;
}
在location中rewrite到其他页面
location / {
if ($http_user_agent ~ MSIE) {
rewrite ^.*$ /ie.html;
break;
}
root html;
index index.htm index.html;
}
6.nginx和php结合
先编译php:
./configure --prefix=/usr/local/fastphp --with-mysqli=mysqlnd --enable-mysqlnd --with-gd \
--enable-gd-native-ttf --enable-gd-jis-conv
Nginx 学习的更多相关文章
- Nginx学习回顾总结 部分:
21:46 2015/11/9Nginx学习回顾总结进程间通信,近似于socket通信的的东西:才发现这种通信并不是很难,并不是我想象的那样很多内容,新领域,入门只是几个函数的使用而已.以前猜过是这样 ...
- Nginx学习笔记4 源码分析
Nginx学习笔记(四) 源码分析 源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_ ...
- Nginx学习笔记~目录索引
回到占占推荐博客索引 前几天整理了<Docker的学习笔记索引>,受到了很多朋友的关注,今天把Nginx的文章也整理一下,以后将永久更新,像大叔之前的<EF文章系列>,< ...
- Nginx学习系列二Linux下Nginx实现负载均衡
关于在本地虚拟机(VMware 14)下安装Linux同时安装Nginx,请参考Nginx学习系列之搭建环境 1.启动Nginx 在Nginx安装成功的前提下,启动Nginx 已root模式登陆(权限 ...
- Nginx系列0:Nginx学习历程
Nginx学习历程 一.初识Nginx 1.Nginx适用于哪些场景 (1)静态资源服务 通过本地文件系统提供服务 (2)反向代理服务 Nginx的强大性能 缓存 负载均衡 (3)API服务 Open ...
- nginx 学习资料
nginx 学习资料 table th:first-of-type { width: 90px; } table th:nth-of-type(2) { } table th:nth-of-type( ...
- Nginx学习总结
2017年2月23日, 星期四 Nginx学习总结 Nginx是目前比较主流的HTTP反向代理服务器(其企业版提供了基于TCP层的反向代理插件),对于构建大型分布式web应用,具有举足轻重的作用.简单 ...
- nginx 学习笔记(2) nginx新手入门
这篇手册简单介绍了nginx,并提供了一些可以操作的简单的工作.前提是nginx已经被安装到你的服务器上.如果没有安装,请阅读上篇:nginx 学习笔记(1) nginx安装.这篇手册主要内容:1. ...
- Nginx学习---Nginx的详解_【all】
1.1. Nginx简介 1.什么是nginx nginx:静态的,开源的www软件,可以解析静态的小文件(低于1M ),支持高并发占用较发少的资源(3W并发,10个进程,内存150M),跨平台 te ...
- Nginx学习之从零搭建静态资源网站
前言 在某学习网站学习了nginx的安装和使用,以此文记录. 环境准备 安装在VMWare下的Centos虚拟机.由于我这是新装的虚拟机.所以很多插件都没有,这里干脆一次性安装上. wget ...
随机推荐
- 实时刷新Winform中Label的Text
最直白的例子: private void btnStart_Click(object sender, EventArgs e) { ; ) { labelTime.Text = i.ToString( ...
- 第三个Sprint冲刺第三天
讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论问题:增强了界面的效果,改善了视角
- 《JavaScript模式》第4章 函数
@by Ruth92(转载请注明出处) 第4章:函数 一.JavaScript 中函数的两个重要特征 函数是第一类对象,可以作为带有属性和方法的值以及参数进行传递: 函数提供了局部作用域,而其他大括号 ...
- my Js
1. __doPostBack是.net自动生成的(当页面中有LinkButton.DropDownList(AutoPostBack)等时:Button和ImageButton不会生成它,也不会调用 ...
- CSS样式“display:none”与“visibility:hidden”区别
CSS样式“display:none”和“visibility:hidden”都可以实现将页面元素隐藏,但是具体的效果是有差别的!下面通过两个小实验来说明这种差异. 实验代码: <!DOCTYP ...
- Python asyncio库的学习和使用
因为要找工作,把之前自己搞的爬虫整理一下,没有项目经验真蛋疼,只能做这种水的不行的东西...T T,希望找工作能有好结果. 之前爬虫使用的是requests+多线程/多进程,后来随着前几天的深入了解 ...
- Windows 10 error code 0x80072efd
访问windows 10 store出现如下图的错误: 如果你是宽带连接,删掉现在的连接,新建一个同样的宽带连接就OK了. 哎,这个问题导致我半个月没上去Windows Store,我还真以为微软有问 ...
- SQL总结(四)编辑类
SQL总结(四)编辑类 应有尽有 1.数据库 创建数据库语法: CREATE DATABASE database_name 1)创建测试库 CREATE DATABASE TestDB 2)使用库 U ...
- Maven进价:Maven构建错误汇总
问题:The method of type must override asuperclass? annotation:@Override的原因 办法:项目右键->build path-> ...
- 特效合集(原生JS代码)适合初学者
1.返回顶部(完全兼容各个浏览器,不含美化) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" & ...