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 ...
随机推荐
- 服务器自己用户名下编译gcc
要点: 1.上传gcc 学习命令 scp 具体格式: scp local_file remote_username@remote_ip:remote_folder scp /home/linux/so ...
- iOS开发ARC内存管理
本文的主要内容: ARC的本质 ARC的开启与关闭 ARC的修饰符 ARC与Block ARC与Toll-Free Bridging ARC的本质 ARC是编译器(时)特性,而不是运行时特性,更不是垃 ...
- 构建一个简单的WCF应用——WCF学习笔记(1)
通过<WCF全面解析>来知识分享....感谢蒋金楠老师@Artech 一.VS中构建解决方案 Client一个控制台程序模拟的客户端,引用Service.ServiceModel.dl ...
- 转weblogic 10.3新建域
一.安装前准备 1.解决linux中文乱码问题 修改/etc/sysconfig/i18n文件 #LANG="en_US.UTF-8"#SUPPORTED="en_US. ...
- 关于 Graph Convolutional Networks 资料收集
关于 Graph Convolutional Networks 资料收集 1. GRAPH CONVOLUTIONAL NETWORKS ------ THOMAS KIPF, 30 SEPTE ...
- SQL 数据库的使用
<1>存到数据库 CSql Sql; Sql.SqlSave(15, &m_SALink, 0, 0, 0, 0); <2>取数据 int *pt = new int[ ...
- Windows下启动,关闭Nginx命令
启动 直接点击Nginx目录下的nginx.exe 或者 cmd运行start nginx 关闭 nginx -s stop 或者 nginx -s quit stop表示立即 ...
- 使用as3控制动画
1.建立按钮元件 2.在属性面板使用英文为按钮元件命名,例如playButton 3.建立函数,例如 function startMovie(event:MouseEvent):void { thi ...
- CMD规范
define(function (require, exports, module) { module.exports = require('xx/xx/xx')({}); });
- 在Linux下安装PHP过程中,编译时出现错误的解决办法
在Linux下安装PHP过程中,编译时出现configure: error: libjpeg.(a|so) not found 错误的解决办法 configure: error: libjpeg.(a ...