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 ...
随机推荐
- 常用的 SQL 函数
SQL 函数 聚合函数(针对数字列): AVG:求平均分 COINT: 计算个数 MAX: 求最大值 MIN: 求最小值 SUM: 求和 数学函数(): ABS: 绝对值 CEIL ...
- oracle数据本机自动备份
1.创建三个文件 exp.list 内容:oracle数据库的用户名和密码 name pwd exp.log 主要用于存储在自动备份数据库时的日志信息 exp.sh #!/bin/sh #找到数 ...
- hiho一下121周 后缀数组二·重复旋律2
后缀数组二·重复旋律2 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成的数列.小Hi ...
- 什么是FTP服务器
FTP服务器,则是在互联网上提供存储空间的计算机,它们依照FTP协议提供服务. FTP的全称是File Transfer Protocol(文件传输协议).顾名思义,就是专门用来传输文件的协议.简单地 ...
- JSBinding / About 2048 sample
2048 Source 2048 source code is here: https://github.com/gabrielecirulli/2048 Play here!http://gabri ...
- 获得图片颜色---摘自php手册
Example #1 imagecolorsforindex() 例子 ;$color_index = imagecolorat($im, $start_x, $start_y); // 使其可读$c ...
- Ubuntu Gnome16.04下安装cuda、theano和opencv
1. 安装显卡驱动 ~$ lspci | grep controller00:02.0 VGA compatible controller: Intel Corporation Sky Lake In ...
- asp.net使用My97 Date Picker时设置默认起始时间为n年之前的今天
可以使用My97 Date Picker组件来收集用户输入的日期值. 首先下载该组件:http://www.my97.net/dp/index.asp放到自己的项目中. 然后在项目里面引用js和css ...
- [Java] - 格式字符串替换方法
Java 字符串格式替换方法有两种,一种是使用String.format(...),另一种是使用MessageFormat.format(...) 如下: import java.text.Messa ...
- 11 TCP/IP 基础与Linux的网络配置
1. TCP/IP与OSI参考模型 TCP/IP是Unix/Linux世界的网络基础,在某种意义上Unix网络就是TCP/IP,而TCP/IP就是网络互联的标准.它不是一个独立的协议,而是一组协议.其 ...