您的位置:首页 > 运维架构 > Nginx

nginx笔记

2016-05-28 23:09 513 查看
linux安装配置nginx其实很简单

#获取
[root@template tmp]#wget http://nginx.org/download/nginx-1.9.15.tar.gz #解压到指定目录
[root@template tmp]# tar -zxvf nginx-1.9.15.tar.gz -C /usr/local/
#安装之前必须先安装依赖
[root@template tmp]yum install pcre pcre-devel
[root@template tmp]cd /usr/local/nginx-1.9.15
#安装到/usr/local/nginx目录
[root@template nginx-1.9.15]./configure --prefix=/usr/local/nginx
#编译安装
[root@template nginx-1.9.15]make && make install
[root@template nginx-1.9.15]cd /usr/local/nginx/sbin/
[root@template sbin]./nginx  #启动
[root@template sbin]netstat -lnp|grep 80  #查看80端口
[root@template sbin]# netstat -lnp|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3091/nginx
udp        0      0 0.0.0.0:68                  0.0.0.0:*                               1380/dhclient
[root@template sbin]# curl 127.0.0.1 #也可以这么看一下
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
...
...
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@template sbin]#


设置nginx开机启动 最简单的办法

vim /etc/rc.d/rc.local
追加一行
/usr/local/nginx/sbin/./nginx


cd /usr/local/nginx, 看到如下4个目录

./

….conf 配置文件

… html 网页文件

…logs 日志文件

…sbin 主要二进制程序

如果出现
[root@localhost sbin]# ./nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
....
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()


不能绑定80端口,80端口已经被占用

把占用80端口的软件或服务关闭即可

PS: Windows7下80端口被占用是系统进程,需要修改注册表,具体百度一下。

打开RegEdit:开始-运行-输入regedit-调出注册表
找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP
找到一个DWORD值Start,将其改为0
重启电脑,System进程将不会占用80端口


Nginx的信号控制

TERM, INT 快速关闭程序,中止当前正在处理的请求

QUIT 处理完当前请求后,关闭程序

HUP 重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求

USR1 重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件

USR2 平滑升级可执行程序

WINCH 从容关闭工作进程

具体语法:

Kill -信号选项 nginx的主进程号(/usr/local/nginx/logs/nginx.pid文件)

Kill -信号控制
cat /usr/local/nginx/logs/nginx.pid


Kill -HUP 4873

等效于

kill -HUP
cat /usr/local/nginx/logs/nginx.pid


改变配置文件,平滑的重读配置文件 常用

Kill -USR1
cat /usr/local/nginx/logs/nginx.pid


测试nginx配置文件是否正确


nginx 测试配置文件

[root@wx12240 conf]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

或者

[root@wx12240 conf]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

-c 配置文件路径

-g Set global directives. (version >=0.7.4)

-t 检测文件是否正确

-v Print version.

-V Print nginx version, compiler version and configure

parameters.

Nginx配置段

// 全局区
worker_processes 1; // 有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数

Event {
// 一般是配置nginx连接的特性
// 如1个word能同时允许多少连接
worker_connections  1024; // 这是指 一个子进程最大允许连1024个连接
}

http {  //这是配置http服务器的主要段
Server1 { // 这是虚拟主机段

Location {  //定位,把特殊的路径或文件再次定位 ,如image目录单独处理
}             /// 如.php单独处理

}

Server2 {
}
}

例子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;
}
}


nginx日志切割

#!/bin/bash
base_path='/usr/local/nginx/logs'
log_path=$(date -d yesterday +"%Y%m")
day=$(date -d yesterday +"%d")
mkdir -p /data/logs/nginxlogs/$log_path
mv $base_path/access.log /data/logs/nginxlogs/$log_path/access_$day.log
#echo $base_path/$log_path/access_$day.log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`


添加定时任务

crontab -e
00 2 * * * /home/sh/generateNingxLog.sh 文件路径 记得更改权限


location 语法

location 有”定位”的意思, 根据Uri来进行不同的定位.
在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上.
比如, 碰到.php, 如何调用PHP解释器?  --这时就需要location
location 的语法
location [=|~|~*|^~] patt {
}
中括号可以不写任何参数,此时称为一般匹配
也可以写参数
因此,大类型可以分为3种
location = patt {} [精准匹配]
location patt{}  [一般匹配]
location ~ patt{} [正则匹配]


正则匹配最长的

找到localtion 后会继续匹配正则,并且匹配最长的

rewrite 重写

重写中用到的指令
if  (条件) {}  设定条件,再进行重写
set #设置变量
return #返回状态码
break #跳出rewrite
rewrite #重写

If  语法格式
If 空格 (条件) {
重写模式
}

条件又怎么写?
答:3种写法
1: “=”来判断相等, 用于字符串比较
2: “~” 用正则来匹配(此处的正则区分大小写)
~* 不区分大小写的正则
3: -f -d -e来判断是否为文件,为目录,是否存在.

例子:

if  ($remote_addr = 192.168.1.100) {
return 403;
}

if ($http_user_agent ~ MSIE) {
rewrite ^.*$ /ie.htm;
break; #(不break会循环重定向)
}

if (!-e $document_root$fastcgi_script_name) {
rewrite ^.*$ /404.html break;
}
注, 此处还要加break,
以 xx.com/dsafsd.html这个不存在页面为例,
我们观察访问日志, 日志中显示的访问路径,依然是GET /dsafsd.html HTTP/1.1
提示: 服务器内部的rewrite和302跳转不一样.
跳转的话URL都变了,变成重新http请求404.html, 而内部rewrite, 上下文没变,
就是说 fastcgi_script_name 仍然是 dsafsd.html,因此 会循环重定向.
set 是设置变量用的, 可以用来达到多条件判断时作标志用.
达到apache下的 rewrite_condition的效果

如下: 判断IE并重写,且不用break; 我们用set变量来达到目的
if ($http_user_agent ~* msie) {
set $isie 1;
}

if ($fastcgi_script_name = ie.html) {
set $isie 0;
}

if ($isie 1) {
rewrite ^.*$ ie.html;
}


注意:用url重写时, 正则里如果有”{}”,正则要用双引号包起来

网页内容的压缩编码与传输速度优化

gzip配置的常用参数
gzip on|off;  #是否开启gzip
gzip_buffers 32 4K| 16 8K #缓冲(压缩在内存中缓冲几块? 每块多大?)
gzip_comp_level [1-9] #推荐6 压缩级别(级别越高,压的越小,越浪费CPU计算资源)
gzip_disable #正则匹配UA 什么样的Uri不进行gzip
gzip_min_length 200 # 开始压缩的最小长度(再小就不要压缩了,意义不在)
gzip_http_version 1.0|1.1 # 开始压缩的http协议版本(可以不设置,目前几乎全是1.1协议)
gzip_proxied          # 设置请求者代理服务器,该如何缓存内容
gzip_types text/plain  application/xml # 对哪些类型的文件用压缩 如txt,xml,html ,css
gzip_vary on|off  # 是否传输gzip压缩标志

Nginx具体的压缩配置
常用以下配置
gzip on|off
gzip_buffers 4K|8K 缓冲(和硬盘块相当)
gzip_comp_level [1-9] 推荐6
gzip_disable 正则匹配如User-Agent,针对古老浏览器不压缩
gzip_min_length 200
gzip_http_version 1.0|1.1
gzip_types text/plain , application/xml (各mime之间,一定要加空格,不是逗号)
gzip_vary on|off


nginx的缓存设置 提高网站性能

对于网站的图片,尤其是新闻站, 图片一旦发布, 改动的可能是非常小的.我们希望 能否在用户访问一次后, 图片缓存在用户的浏览器端,且时间比较长的缓存.

可以, 用到 nginx的expires设置 .

nginx中设置过期时间,非常简单,

在location或if段里,来写.

格式 expires 30s;

expires 30m;

expires 2h;

expires 30d;

(注意:服务器的日期要准确,如果服务器的日期落后于实际日期,可能导致缓存失效)

另: 304 也是一种很好的缓存手段

原理是: 服务器响应文件内容是,同时响应etag标签(内容的签名,内容一变,他也变), 和 last_modified_since 2个标签值

浏览器下次去请求时,头信息发送这两个标签, 服务器检测文件有没有发生变化,如无,直接头信息返回 etag,last_modified_since

浏览器知道内容无改变,于是直接调用本地缓存.

这个过程,也请求了服务器,但是传着的内容极少.

对于变化周期较短的,如静态html,js,css,比较适于用这个方式

nginx反向代理服务器+负载均衡

用nginx做反向代理和负载均衡非常简单,

支持两个用法 1个proxy, 1个upstream,分别用来做反向代理,和负载均衡

以反向代理为例, nginx不自己处理php的相关请求,而是把php的相关请求转发给apache来处理.

—-这不就是传说的”动静分离”,动静分离不是一个严谨的说法,叫反向代理比较规范.

反向代理后端如果有多台服务器,自然可形成负载均衡,

但proxy_pass如何指向多台服务器?

把多台服务器用 upstream指定绑定在一起并起个组名,

然后proxy_pass指向该组

默认的均衡的算法很简单,就是针对后端服务器的顺序,逐个请求.

也有其他负载均衡算法,如一致性哈希,需要安装第3方模块.

nginx 与memcached的组合

用法: nginx响应请求时,直接请求memcached,

如果没有相应的内容,再回调PHP页面,去查询database,并写入memcached.

分析: memcached是k/v存储, key–>value,

nginx请求memecached时,用什么做key?

一般用 uri arg 做key, 如 /abc.php?id=3

Nginx 第三方模块的安装

以ngx_http_php_memcache_standard_balancer-master为例

1:解压 到 path/ngx_module

配置:

./configure –prefix=/xxx/xxx –add_module=/path/ngx_module

编译 安装

Make && make instal

配置memcache集群

upstream memserver {  把用到的memcached节点,声明在一个组里
hash_key $request_uri;  // hash计算时的依据,以uri做依据来hash
server localhost:11211;
server localhost:11212;
}


Location里

location / {

# root html;

set memcachedkeyuri;

memcached_pass memserver; // memserver为上面的memcache节点的名称

error_page 404 /writemem.php;

index index.php index.html index.htm;

}

在nginx中做集群与负载均衡,步骤都是一样的

Upstream {}模块 把多台服务器加入到一个组

然后 memcached_pass, fastcgi_pass, proxy_pass ==> upstream组

默认的负载均衡的算法:

是设置计数器,轮流请求N台服务器.

可以安装第3方模式,来利用uri做hash等等.

http://wiki.nginx.org/NginxHttpUpstreamConsistentHash

这个模块就是用一致性hash来请求后端结节,并且其算法,与PHP中的memcache模块的一致性hash算法,兼容.

安装该模块后:

Nginx.conf中

upstream memserver {

consistent_hash $request_uri;

server localhost:11211;

server localhost:11212;

}

在PHP.ini中,如下配置

memcache.hash_strategy = consistent

这样: nginx与PHP即可完成对memcached的集群与负载均衡算法.

安装时报错:

./configure: error: the HTTP rewrite module requires the PCRE library.


解决办法.

yum install pcre pcre-devel


问题是机器不能连外网

单独下载

https://pkgs.org/download/pcre-devel

centos6

https://pkgs.org/centos-6/centos-x86_64/pcre-devel-7.8-7.el6.x86_64.rpm.html

能上网的局域网机器上

wget mirror.centos.org/centos/6/os/x86_64/Packages/pcre-devel-7.8-7.el6.x86_64.rpm
再拷贝到目标主机
scp -P 22 root@10.110.110.110:/tmp/pcre-devel-7.8-7.el6.x86_64.rpm /tmp/setupfiles/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx linux centos