深入Nginx
Nginx功能模块汇总
--with-http_core_module #包括一些核心的http参数配置,对应nginx的配置为http区块部分
--with-http_access_module #访问控制模块,用来控制网站用户对nginx的访问
--with-http_gzip_module #压缩模块,nginx返回的数据压缩,属于性能优化模块
--with-http_fastcgi_module #FastCGI模块,和动态应用相关的模块,例如PHP
--with-http_proxy_module #proxy代理模块
--with-http_upstream_module #负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查
--with-http_rewrite_module #URL地址重写模块
--with-http_limit_conn_module #限制用户并发连接及请求数模块
--with-http_limit_req_module #根据定义的key限制nginx请求过程的sulv
--with-http_log_module #请求日志模块,以制定的个事记录nginx客户访问日志的信息
--with-http_auth_basic_module #web认证模块,设置web用户通过账号、密码访问nginx
--with-http_ssl_module #ssl模块,用于加密的http连接,如https
--with-http_stub_status_module 记录nginx基本访问状态信息等的模块
Nginx的目录结构说明
conf #这是nginx所有配置文件的目录
fastcgi.conf #fastcgi相关参数的配置文件
fastcgi.conf.default #fastcgi.conf的原始备份
fastcgi_params #fastcgi的参数文件
fastcgi_params.default
koi_utf
koi_win
mime.types #媒体类型
mime.types.defualt
nginx.conf #Nginx默认的配置文件
nginx.conf.default
scgi_params #scgi相关参数文件
uwsgi_params #uwsgi相关参数配置文件
fastcgi_temp #fastcgi临时配置文件
html
logs #默认的日志路径
access.log #默认访问日志文件
error.log
nginx.pid
proxy_temp #临时文件
sbin #nginx的命令目录
nginx #nginx的 启动命令
Nginx的配置文件说明
cat nginx.conf.defaultuser www www;worker_processes 2;#worker进程的数量
pid logs/nginx.pid;
events { #事件区块开始
use epoll;
worker_connections 2048; #每个worker进程支持的最大连接数
} http { #http区块开始
include mime.types; #ngninx支持的媒体类型库文件
default_type application/octet-stream; #默认的媒体类型
sendfile on; #开启高效传输模式
keepalive_timeout 65; #连接超时 # 很重要的虚拟主机配置
server { #第一个server区块开始
listen 80;
server_name itoatest.example.com; #提供服务的域名主机名ip/domain
charset utf-8; location / {root /apps/oaapp; #站点的根目录
index index.html index.htm; #默认首页文件
}
error_page 500 502 504 504 /50x.html #出现对应的http状态码时,使用50x。html回应客户
local = /50x.html{
root html;
}
}
Nginx虚拟主机配置
1.基于域名的nginx.conf配置文件
server {
listen 80;
server_name www.abc.com;
location / {
root html/www;
index index.html index.htm;
}
server {
listen 80;
server_name blog.abc.com;
location / {
root html/blog;
index index.html index.htm;
}
server {
listen 80;
server_name bbs.abc.com;
location / {
root html/bbs;
index index.html index.htm;
}
2.基于端口的虚拟主机
server {
listen 80;
server_name www.abc.com;
location / {
root html/www;
index index.html index.htm;
}
server {
listen 81;
server_name blog.abc.com;
location / {
root html/blog;
index index.html index.htm;
}
server {
listen 82;
server_name bbs.abc.com;
location / {
root html/bbs;
index index.html index.htm;
}
3.基于ip的虚拟配置
server {
listen 10.0.0.1:80;
server_name www.abc.com;
location / {
root html/www;
index index.html index.htm;
}
server {
listen 10.0.0.2:81;
server_name blog.abc.com;
location / {
root html/blog;
index index.html index.htm;
}
server {
listen 10.0.0.3:82;
server_name bbs.abc.com;
location / {
root html/bbs;
index index.html index.htm;
}
Nginx的规范优化配置文件
主文件包含的所有虚拟主机的子配置文件会统一放在extra目录中。虚拟主机的配置文件按照网站的域名或功能名称取名,例如www.conf等
events { #事件区块开始
use epoll;
worker_connections 2048; #每个worker进程支持的最大连接数
}
http { #http区块开始
include mime.types; #ngninx支持的媒体类型库文件
default_type application/octet-stream; #默认的媒体类型
sendfile on; #开启高效传输模式
keepalive_timeout 65; #连接超时
include extra/www.conf;
include extra/bbs.conf;
}
[root@www conf]# cat extra/www.conf
server {
listen 80;
server_name www.abc.com; location / {
root html/www;
index index.html index.htm;
}
}
location里面设置允许和禁止的ip段访问
location /nginx_status{
stub_status on; #打开状态信息开关
access_log off;
allow 10.0.0.0/24; #设置允许和禁止的IP段访问
deny all; #设置允许和禁止的ip段访问
}
Nginx错误日志配置
错误日志常见的级别【debug|info|notice|warn|error|crit|alert|emerg】
生存环境通过是warn|error|crit,注意不要配置info等较低级别,会损坏巨大磁盘IO
通常配置如下:
worker_processes 1;
error_log logs/error.logs error; #就在这里配置
events {
.....
}
Nginx访问日志
1.访问日志两个参数
log_format 用来定义记录日志格式(可以定义多种日志个事,取不同名字)
access_log 用来制定日志文件的路径和使用何种日志格式记录日志
2.访问日志配置说明
log_format
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" ';
$remote_addr #记录访问网站的客户端地址
$http_x_forwarded_for#当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了相关的
$remote_user #远程客户端用户名称
$time_local #记录访问时间与时区
$request #用户的http请求其实行信息
$status #http状态码,记录请求返回的状态,例如200,404,301等
$body_bytes_sents #服务器发送给客户端的响应body字节数
$http_referer #记录此次请求是从那个链接访问过来的,可以根据referer进行防盗链设置
$http_user_agent #记录客户端访问信息。例如浏览器,手机客户端等
access_log
语法如下:
access_log path [format [buffer=size [flush=time]] [if=condition]];
access_log path format gzip[=level] [buffer=size] [flush=time][if=codition];
access_log syslog:server=address[,parameter=value] [format [if=condition]];
buffer=size 为存放访问日志的缓冲区大小
flush=time为将缓冲区的日志刷到磁盘的时间
gzip[=level]表示压缩级别
[if=condition] 表示其他条件
access_log off表示不记录访问日志
一般情况无须配置,极端优化菜考虑
样例
events { #事件区块开始
use epoll;
worker_connections 2048; #每个worker进程支持的最大连接数
}
http { #http区块开始
include mime.types; #ngninx支持的媒体类型库文件
default_type application/octet-stream; #默认的媒体类型
sendfile on; #开启高效传输模式
keepalive_timeout 65; #连接超时
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" ';
include extra/www.conf;
include extra/bbs.conf; }
[root@www conf]# cat extra/www.conf
server {
listen 80;
server_name www.abc.com; location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main; }
如果在高并发场景下提升网站的访问性能,可以加入buffer和flush选项
access_log logs/access_www.log main gzip buffer=32k flush=5s;
然后重启服务,记得创建文件呀access_www.log
深入Nginx的更多相关文章
- accept_mutex与性能的关系 (nginx)
注:运行环境CentOS 6+ 背景 在对启动了20个worker的nginx进行压力测试的时候发现:如果把配置文件中event配置块中的accept_mutex开关打开(1.11.3版 ...
- nginx配置反向代理或跳转出现400问题处理记录
午休完上班后,同事说测试站点访问接口出现400 Bad Request Request Header Or Cookie Too Large提示,心想还好是测试服务器出现问题,影响不大,不过也赶紧上 ...
- 【大型网站技术实践】初级篇:借助Nginx搭建反向代理服务器
一.反向代理:Web服务器的“经纪人” 1.1 反向代理初印象 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从 ...
- Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境
首先为什么要自己编写Dockerfile来构建 nginx.php.mariadb这三个镜像呢?一是希望更深入了解Dockerfile的使用,也就能初步了解docker镜像是如何被构建的:二是希望将来 ...
- nginx+php的使用
原文来自:windows下配置nginx+php环境 按照他的步骤走,亲测可用! 但是这里他后面说的根目录可能有些人有点懵. 其实在设置的时候就设置了: 网站根目录就是www这个目录,如果没创建请自行 ...
- nginx的使用
1.nginx的下载 解压后文件目录: 2.nginx的常用命令 nginx -s stop 强制关闭 nginx -s quit 安全关闭 nginx -s reload 改变配置文件的时候,重 ...
- nginx+iis+redis+Task.MainForm构建分布式架构 之 (redis存储分布式共享的session及共享session运作流程)
本次要分享的是利用windows+nginx+iis+redis+Task.MainForm组建分布式架构,上一篇分享文章制作是在windows上使用的nginx,一般正式发布的时候是在linux来配 ...
- windows+nginx+iis+redis+Task.MainForm构建分布式架构 之 (nginx+iis构建服务集群)
本次要分享的是利用windows+nginx+iis+redis+Task.MainForm组建分布式架构,由标题就能看出此内容不是一篇分享文章能说完的,所以我打算分几篇分享文章来讲解,一步一步实现分 ...
- nginx源码分析之模块初始化
在nginx启动过程中,模块的初始化是整个启动过程中的重要部分,而且了解了模块初始化的过程对应后面具体分析各个模块会有事半功倍的效果.在我看来,分析源码来了解模块的初始化是最直接不过的了,所以下面主要 ...
- Nginx如何处理一个请求
看了下nginx的官方文档,其中nginx如何处理一个请求讲解的很好,现在贴出来分享下.Nginx首先选定由哪一个虚拟主机来处理请求.让我们从一个简单的配置(其中全部3个虚拟主机都在端口*:80上监听 ...
随机推荐
- jQuery基础课程
环境搭建 搭建一个jQuery的开发环境非常方便,可以通过下列几个步骤进行. 下载jQuery文件库 在jQuery的官方网站(http://jquery.com)中,下载最新版本的jQuery文件库 ...
- 反射在ADO.NET中的运用(你还在每个项目中循环遍历DataTable吗)
图片有点大哈,但大更能说明问题.您是不是每个项目都在重复的做图片中的事情-----循环把数据库中返回的表转化为实体对象.是不是每次都在抱怨这样的重复工作.字段越多抱怨越多!不用抱怨了.当你看到这篇文章 ...
- 数学的东西(BZOJ1951)
#include <cstdio> #define LL long long LL finmo=; LL fac[][],inv[][]; LL tmp[],rev[]; LL n,g,x ...
- [转] 对称加密算法DES、3DES
转自:http://www.blogjava.net/amigoxie/archive/2014/07/06/415503.html 1.对称加密算法 1.1 定义 对称加密算法是应用较早的加密算法, ...
- Windows7SP1补丁包(Win7补丁汇总) 32位/64位版 更新截至2016年11月
Windows7SP1(64位)补丁包(Win7补丁汇总)更新到本月最新.包含Windows7SP1中文版所有重要补丁,可离线安装,适用于Windows 7 SP1 64位 简体中文系统.包含Inte ...
- less简介
Less是一种动态的样式语言.Less扩展了CSS的动态行为,比如说,设置变量(Variables).混合书写模式(mixins).操作(operations)和功能(functions)等等,最棒的 ...
- asp.net MVC3的局部缓存页面PartialCache.cshtml
MVC3及以上有了PartialCache.cshtml局部缓存的方式,具体实现: 新建一个PartialCache.cshtml的页面,在控制器上写上如下代码: [OutputCache(Durat ...
- gulp同步执行任务
使用插件:gulp-sync,npm install --save-dev gulp-sync 使用方法参考:https://www.npmjs.com/package/gulp-sync 这些插件也 ...
- NodeJS:Error: Cannot find module 'jshint/src/cli'
以前命令:npm install gulp-jshint --save-dev 实质上是安装jshint失败,缺少该模块. 更换命令 :npm install --save-dev jshint gu ...
- Mac OSX定位命令路径的方法
可以使用which命令来定位一个命令. http://www.cyberciti.biz/faq/how-do-i-find-the-path-to-a-command-file/