nginx中文手册内容说明

1.什么是nginx?javascript

Nginx 是一个高性能的 Web 和反向代理服务器, 它具备有不少很是优越的特性:php

做为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发链接,体现更高的效率,这点使 Nginx 尤为受到虚拟主机提供商的欢迎。可以支持高达 50,000 个并发链接数的响应,感谢 Nginx 为咱们选择了 epoll and kqueue 做为开发模型.css

做为负载均衡服务器:Nginx 既能够在内部直接支持 Rails 和 PHP,也能够支持做为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不管是系统资源开销仍是 CPU 使用效率都比 Perlbal 要好的多。html

做为邮件代理服务器: Nginx 同时也是一个很是优秀的邮件代理服务器(最先开发这个产品的目的之一也是做为邮件代理服务器),Last.fm 描述了成功而且美妙的使用经验。前端

Nginx 安装很是的简单,配置文件 很是简洁(还可以支持perl语法),Bugs很是少的服务器: Nginx 启动特别容易,而且几乎能够作到7*24不间断运行,即便运行数个月也不须要从新启动。你还可以在 不间断服务的状况下进行软件版本的升级。java

二、如何源码编译安装nginx?
node

 

nginx可使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息。mysql

正式开始前,编译环境gcc g++ 开发库之类的须要提早装好,这里默认你已经装好。linux

ububtu平台编译环境可使用如下指令nginx

apt-get install build-essential apt-get install libtool

centos平台编译环境使用以下指令

安装make:

yum -y install gcc automake autoconf libtool make

安装g++:

yum install gcc gcc-c++

下面正式开始
---------------------------------------------------------------------------
通常咱们都须要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。
1.选定源码目录
能够是任何目录,本文选定的是/usr/local/src

cd /usr/local/src

2.安装PCRE库
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

cd /usr/local/src wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz  tar -zxvf pcre-8.34.tar.gz cd pcre-8.34 ./configure make make install

3.安装zlib库
http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:

cd /usr/local/src wget http://zlib.net/zlib-1.2.8.tar.gz tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure make make install

4.安装ssl(某些vps默认没装ssl)

cd /usr/local/src wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz tar -zxvf openssl-1.0.1c.tar.gz

5.安装nginx

Nginx 通常有两个版本,分别是稳定版和开发版,您能够根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:

cd /usr/local/src wget http://nginx.org/download/nginx-1.4.2.tar.gz tar -zxvf nginx-1.4.2.tar.gz cd nginx-1.4.2 ./configure --sbin-path=/usr/local/nginx/nginx \ --conf-path=/usr/local/nginx/nginx.conf \ --pid-path=/usr/local/nginx/nginx.pid \ --with-http_ssl_module \ --with-pcre=/usr/local/src/pcre-8.34 \ --with-zlib=/usr/local/src/zlib-1.2.8 \ --with-openssl=/usr/local/src/openssl-1.0.1c make make install

--with-pcre=/usr/src/pcre-8.34 指的是pcre-8.34 的源码路径。
--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。

安装成功后 /usr/local/nginx 目录下以下

fastcgi.conf koi-win nginx.conf.default fastcgi.conf.default logs scgi_params fastcgi_params mime.types scgi_params.default fastcgi_params.default mime.types.default uwsgi_params html nginx uwsgi_params.default koi-utf nginx.conf win-utf

6.启动
确保系统的 80 端口没被其余程序占用,运行/usr/local/nginx/nginx 命令来启动 Nginx,

netstat -ano|grep 80

若是查不到结果后执行,有结果则忽略此步骤(ubuntu下必须用sudo启动,否则只能在前台运行)

sudo /usr/local/nginx/nginx

打开浏览器访问此机器的 IP,若是浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

-----------------------------------------------------
到这里nginx就安装完成了,若是只是处理静态html就不用继续安装了

若是你须要处理php脚本的话,还须要安装php-fpm

下面安装排错

附:可能遇到的错误和一些帮助信息

1.1编译pcre错误

libtool: compile: unrecognized option `-DHAVE_CONFIG_H' libtool: compile: Try `libtool --help' for more information. make[1]: *** [pcrecpp.lo] Error 1 make[1]: Leaving directory `/usr/local/src/pcre-8.34' make: *** [all] Error 2

解决办法:安装g++,别忘了从新configure

apt-get install g++ apt-get install build-essential make clean ./configure make

1.2 make出错

make: *** No rule to make target `build', needed by `default'.  Stop. ./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl= option.

按照第4步的安装方法或
ubuntu下

apt-get install openssl apt-get install libssl-dev

centos下

yum -y install openssl openssl-devel

2.nginx编译选项

make是用来编译的,它从Makefile中读取指令,而后编译。

make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。

configure命令是用来检测你的安装平台的目标特征的。它定义了系统的各个方面,包括nginx的被容许使用的链接处理的方法,好比它会检测你是否是有CC或GCC,并非须要CC或GCC,它是个shell脚本,执行结束时,它会建立一个Makefile文件。nginx的configure命令支持如下参数:

  • --prefix=path    定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录。默认使用 /usr/local/nginx。
  • --sbin-path=path 设置nginx的可执行文件的路径,默认为  prefix/sbin/nginx.
  • --conf-path=path  设置在nginx.conf配置文件的路径。nginx容许使用不一样的配置文件启动,经过命令行中的-c选项。默认为prefix/conf/nginx.conf.
  • --pid-path=path  设置nginx.pid文件,将存储的主进程的进程号。安装完成后,能够随时改变的文件名 , 在nginx.conf配置文件中使用 PID指令。默认状况下,文件名 为prefix/logs/nginx.pid.
  • --error-log-path=path 设置主错误,警告,和诊断文件的名称。安装完成后,能够随时改变的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默认状况下,文件名 为prefix/logs/error.log.
  • --http-log-path=path  设置主请求的HTTP服务器的日志文件的名称。安装完成后,能够随时改变的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默认状况下,文件名 为prefix/logs/access.log.
  • --user=name  设置nginx工做进程的用户。安装完成后,能够随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的用户名是nobody。
  • --group=name  设置nginx工做进程的用户组。安装完成后,能够随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的为非特权用户。
  • --with-select_module --without-select_module 启用或禁用构建一个模块来容许服务器使用select()方法。该模块将自动创建,若是平台不支持的kqueue,epoll,rtsig或/dev/poll。
  • --with-poll_module --without-poll_module 启用或禁用构建一个模块来容许服务器使用poll()方法。该模块将自动创建,若是平台不支持的kqueue,epoll,rtsig或/dev/poll。
  • --without-http_gzip_module — 不编译压缩的HTTP服务器的响应模块。编译并运行此模块须要zlib库。
  • --without-http_rewrite_module  不编译重写模块。编译并运行此模块须要PCRE库支持。
  • --without-http_proxy_module — 不编译http_proxy模块。
  • --with-http_ssl_module — 使用https协议模块。默认状况下,该模块没有被构建。创建并运行此模块的OpenSSL库是必需的。
  • --with-pcre=path — 设置PCRE库的源码路径。PCRE库的源码(版本4.4 - 8.30)须要从PCRE网站下载并解压。其他的工做是Nginx的./ configure和make来完成。正则表达式使用在location指令和 ngx_http_rewrite_module 模块中。
  • --with-pcre-jit —编译PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。
  • --with-zlib=path —设置的zlib库的源码路径。要下载从 zlib(版本1.1.3 - 1.2.5)的并解压。其他的工做是Nginx的./ configure和make完成。ngx_http_gzip_module模块须要使用zlib 。
  • --with-cc-opt=parameters — 设置额外的参数将被添加到CFLAGS变量。例如,当你在FreeBSD上使用PCRE库时须要使用:--with-cc-opt="-I /usr/local/include。.如须要须要增长 select()支持的文件数量:--with-cc-opt="-D FD_SETSIZE=2048".
  • --with-ld-opt=parameters —设置附加的参数,将用于在连接期间。例如,当在FreeBSD下使用该系统的PCRE库,应指定:--with-ld-opt="-L /usr/local/lib".

典型实例(下面为了展现须要写在多行,执行时内容须要在同一行)

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-4.4 --with-zlib=../zlib-1.1.3

3.nginx简单配置详解(从welcome to nginx开始上手)
#运行用户 user nobody; #启动进程,一般设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #工做模式及链接数上限 events { #epoll是多路复用IO(I/O Multiplexing)中的一种方式, #仅用于linux2.6以上内核,能够大大提升nginx的性能 use epoll; #单个后台worker process进程的最大并发连接数  worker_connections 1024; # 并发总数是 worker_processes 和 worker_connections 的乘积 # 即 max_clients = worker_processes * worker_connections # 在设置了反向代理的状况下,max_clients = worker_processes * worker_connections / 4 为何 # 为何上面反向代理要除以4,应该说是一个经验值 # 根据以上条件,正常状况下的Nginx Server能够应付的最大链接数为:4 * 8000 = 32000 # worker_connections 值的设置跟物理内存大小有关 # 由于并发受IO约束,max_clients的值须小于系统能够打开的最大文件数 # 而系统能够打开的最大文件数和内存大小成正比,通常1GB内存的机器上能够打开的文件数大约是10万左右 # 咱们来看看360M内存的VPS能够打开的文件句柄数是多少: # $ cat /proc/sys/fs/file-max # 输出 34336 # 32000 < 34336,即并发链接总数小于系统能够打开的文件句柄总数,这样就在操做系统能够承受的范围以内 # 因此,worker_connections 的值需根据 worker_processes 进程数目和系统能够打开的最大文件总数进行适当地进行设置 # 使得并发总数小于操做系统能够打开的最大文件数目 # 其实质也就是根据主机的物理CPU和内存进行配置 # 固然,理论上的并发总数可能会和实际有所误差,由于主机还有其余的工做进程须要消耗系统资源。 # ulimit -SHn 65535 } http { #设定mime类型,类型由mime.type文件定义 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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件, #对于普通应用,必须设为 on, #若是用来进行下载等应用磁盘IO重负载应用,可设置为 off, #以平衡磁盘与网络I/O处理速度,下降系统的uptime. sendfile on; #tcp_nopush on; #链接超时时间 #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; #开启gzip压缩 gzip on; gzip_disable "MSIE [1-6]."; #设定请求缓冲 client_header_buffer_size 128k; large_client_header_buffers 4 128k; #设定虚拟主机配置 server { #侦听80端口 listen 80; #定义使用 www.nginx.cn访问 server_name www.nginx.cn; #定义服务器的默认网站根目录位置 root html; #设定本虚拟主机的访问日志 access_log logs/nginx.access.log main; #默认请求 location / { #定义首页索引文件的名称 index index.php index.html index.htm; } # 定义错误提示页面 error_page 500 502 503 504 /50x.html; location = /50x.html { } #静态文件,nginx本身处理 location ~ ^/(images|javascript|js|css|flash|media|static)/ { #过时30天,静态文件不怎么更新,过时能够设大一点, #若是频繁更新,则能够设置得小一点。 expires 30d; } #PHP 脚本请求所有转发到 FastCGI处理. 使用FastCGI默认配置. location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #禁止访问 .htxxx 文件 location ~ /.ht { deny all; } } }

4.nginx平滑升级

  1. Nginx命令行选项
  2. 启动、中止、重启nginx命令
  3. 如何从新加载nginx配置文件
  4. nginx程序不重启平滑升级可执行文件

 

nginx命令行参数

不像许多其余软件系统,Nginx 仅有几个命令行参数,彻底经过配置文件来配置

-c </path/to/config> 为 Nginx 指定一个配置文件,来代替缺省的。

-t 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。

-v 显示 nginx 的版本。

-V 显示 nginx 的版本,编译器版本和配置参数。

 

nginx控制信号

可使用信号系统来控制主进程。默认,nginx 将其主进程的 pid 写入到 /usr/local/nginx/nginx.pid 文件中。经过传递参数给 ./configure 或使用 pid 指令,来改变该文件的位置。

主进程能够处理如下的信号:

TERM, INT 快速关闭
QUIT 从容关闭
HUP 重载配置
用新的配置开始新的工做进程
从容关闭旧的工做进程
USR1 从新打开日志文件
USR2 平滑升级可执行程序。
WINCH 从容关闭工做进程

尽管你没必要本身操做工做进程,可是,它们也支持一些信号:

TERM, INT 快速关闭
QUIT 从容关闭
USR1 从新打开日志文件

 

nginx 启动、中止、重启命令

nginx启动

sudo /usr/local/nginx/nginx     (nginx二进制文件绝对路径,能够根据本身安装路径实际决定)

 

nginx从容中止命令,等全部请求结束后关闭服务

ps -ef |grep nginx

kill -QUIT  nginx主进程号

 

nginx 快速中止命令,马上关闭nginx进程

ps -ef |grep nginx

kill -TERM nginx主进程号 

若是以上命令无论用,能够强制中止

kill -9 nginx主进程号

 

若是嫌麻烦能够不用查看进程号,直接使用命令进行操做
其中/usr/local/nginx/nginx.pid 为nginx.conf中pid命令设置的参数,用来存放nginx主进程号的文件
kill -信号类型(HUP|TERM|QUIT) `cat /usr/local/nginx/nginx.pid` 
例如

kill -QUIT `cat /usr/local/nginx/nginx.pid`

nginx重启命令

nginx重启能够分红几种类型

1.简单型,先关闭进程,修改你的配置后,重启进程。
kill -QUIT `cat /usr/local/nginx/nginx.pid`
sudo /usr/local/nginx/nginx
2.从新加载配置文件,不重启进程,不会中止处理请求
3.平滑更新nginx二进制,不会中止处理请求

 

使用信号加载新的配置

Nginx 支持几个信号,能在它运行时控制其操做。其中最普通的是 15 ,用来停止运行的进程:

# <strong>ps aux | egrep '(PID|nginx)'</strong> USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND root      2213  0.0  0.0   6784  2036 ?        Ss   03:01   0:00 nginx: master process /usr/sbin/nginx -/etc/nginx/nginx.conf # <strong>kill -15 2213</strong>

而最有趣的是能平滑改变 nginx 配置的选项(请注意,在重载前,要先测试一下配置文件):

 

#<strong> nginx -t -c /etc/nginx/nginx.conf</strong> 2006/09/16 13:07:10 [info] 15686#0: the configuration file /etc/nginx/nginx.conf syntax is ok 2006/09/16 13:07:10 [info] 15686#0: the configuration file /etc/nginx/nginx.conf was tested successfully #<strong> ps aux | egrep '(PID|nginx)'</strong> USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND root      2213  0.0  0.0   6784  2036 ?        Ss   03:01   0:00 nginx: master process /usr/sbin/nginx -/etc/nginx/nginx.conf <strong># kill -HUP 2213</strong>

当 nginx 接收到 HUP 信号,它会尝试先解析配置文件(若是指定配置文件,就使用指定的,不然使用默认的),成功的话,就应用新的配置文件(例如:从新打开日志文件或监听的套接 字)。以后,nginx 运行新的工做进程并从容关闭旧的工做进程。通知工做进程关闭监听套接字可是继续为当前链接的客户提供服务。全部客户端的服务完成后,旧的工做进程被关闭。 若是新的配置文件应用失败,nginx 将继续使用旧的配置进行工做。

 

平滑升级到新的二进制代码

你能够在不中断服务的状况下 - 新的请求也不会丢失,使用新的 nginx 可执行程序替换旧的(当升级新版本或添加/删除服务器模块时)。

首先,使用新的可执行程序替换旧的(最好作好备份),而后,发送 USR2 (kill -USR2 pid)信号给主进程。主进程将重命名它的 .pid 文件为 .oldbin (好比:/usr/local/nginx/logs/nginx.pid.oldbin),而后执行新的可执行程序,依次启动新的主进程和新的工做进程:

 

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND 33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx 33134 33126 nobody   0.0  1368 kqread nginx: worker process (nginx) 33135 33126 nobody   0.0  1380 kqread nginx: worker process (nginx) 33136 33126 nobody   0.0  1368 kqread nginx: worker process (nginx) 36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

在这时,两个 nginx 实例会同时运行,一块儿处理输入的请求。要逐步中止旧的实例,你必须发送 WINCH 信号给旧的主进程,而后,它的工做进程就将开始从容关闭:

 

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND 33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx 33135 33126 nobody   0.0  1380 kqread nginx: worker process is shutting down (nginx) 36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

一段时间后,旧的工做进程处理了全部已链接的请求后退出,就仅由新的工做进程来处理输入的请求了:

 

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND 33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx 36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

这时,由于旧的服务器还还没有关闭它监听的套接字,因此,经过下面的几步,你仍能够恢复旧的服务器:

  • 发送 HUP 信号给旧的主进程 - 它将在不重载配置文件的状况下启动它的工做进程
  • 发送 QUIT 信号给新的主进程,要求其从容关闭其工做进程
  • 发送 TERM 信号给新的主进程,迫使其退出
  • 若是由于某些缘由新的工做进程不能退出,向其发送 KILL 信号

新的主进程退出后,旧的主进程会由移除 .oldbin 前缀,恢复为它的 .pid 文件,这样,一切就都恢复到升级以前了。

若是尝试升级成功,而你也但愿保留新的服务器时,发送 QUIT 信号给旧的主进程使其退出而只留下新的服务器运行:

 

      PID  PPID USER    %CPU   VSZ WCHAN  COMMAND     36264     1 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx     36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)     36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)     36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

5.nginx重要指令之location

location匹配命令

~      #波浪线表示执行一个正则匹配,区分大小写
~*    #表示执行一个正则匹配,不区分大小写
^~    #^~表示普通字符匹配,若是该选项匹配,只匹配该选项,不匹配别的选项,通常用来匹配目录
=      #进行普通字符精确匹配
@     #"@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files

 

location 匹配的优先级(与location在配置文件中的顺序无关)
= 精确匹配会第一个被处理。若是发现精确匹配,nginx中止搜索其余匹配。
普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说若是该项匹配还需去看有没有正则表达式匹配和更长的匹配。
^~ 则只匹配该规则,nginx中止搜索其余匹配,不然nginx会继续处理其余location指令。
最后匹配理带有"~"和"~*"的指令,若是找到相应的匹配,则nginx中止搜索其余匹配;当没有正则表达式或者没有正则表达式被匹配的状况下,那么匹配程度最高的逐字匹配指令会被使用。

location 优先级官方文档

  1. Directives with the = prefix that match the query exactly. If found, searching stops.
  2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
  3. Regular expressions, in order of definition in the configuration file.
  4. If #3 yielded a match, that result is used. Else the match from #2 is used.
  1. =前缀的指令严格匹配这个查询。若是找到,中止搜索。
  2. 全部剩下的常规字符串,最长的匹配。若是这个匹配使用^〜前缀,搜索中止。
  3. 正则表达式,在配置文件中定义的顺序。
  4. 若是第3条规则产生匹配的话,结果被使用。不然,如同从第2条规则被使用。

 

例如

location  = / { # 只匹配"/". [ configuration A ] } location / { # 匹配任何请求,由于全部请求都是以"/"开始 # 可是更长字符匹配或者正则表达式匹配会优先匹配 [ configuration B ] } location ^~ /images/ { # 匹配任何以 /images/ 开始的请求,并中止匹配 其它location [ configuration C ] } location ~* \.(gif|jpg|jpeg)$ { # 匹配以 gif, jpg, or jpeg结尾的请求. # 可是全部 /images/ 目录的请求将由 [Configuration C]处理. [ configuration D ] }

请求URI例子:

  • / -> 符合configuration A
  • /documents/document.html -> 符合configuration B
  • /images/1.gif -> 符合configuration C
  • /documents/1.jpg ->符合 configuration D

@location 例子
error_page 404 = @fetch;

location @fetch(
proxy_pass http://fetch;
)

6.nginx重要指令之upstream

7.nginx rewrite功能

nginx经过ngx_http_rewrite_module模块支持url重写、支持if条件判断,但不支持else。

该模块须要PCRE支持,应在编译nginx时指定PCRE源码目录,nginx安装方法

nginx rewrite指令执行顺序:

1.执行server块的rewrite指令(这里的块指的是server关键字后{}包围的区域,其它xx块相似)
2.执行location匹配
3.执行选定的location中的rewrite指令
若是其中某步URI被重写,则从新循环执行1-3,直到找到真实存在的文件

若是循环超过10次,则返回500 Internal Server Error错误

 

break指令

语法:break;
默认值:无
做用域:server,location,if

中止执行当前虚拟主机的后续rewrite指令集
break指令实例:

 if ($slow) {      limit_rate 10k;      break;  }

if指令

语法:if(condition){...}
默认值:无
做用域:server,location
对给定的条件condition进行判断。若是为真,大括号内的rewrite指令将被执行。
if条件(conditon)能够是以下任何内容:

  • 一个变量名;false若是这个变量是空字符串或者以0开始的字符串;
  • 使用= ,!= 比较的一个变量和字符串
  • 是用~, ~*与正则表达式匹配的变量,若是这个正则表达式中包含},;则整个表达式须要用" 或' 包围
  • 使用-f ,!-f 检查一个文件是否存在
  • 使用-d, !-d 检查一个目录是否存在
  • 使用-e ,!-e 检查一个文件、目录、符号连接是否存在
  • 使用-x , !-x 检查一个文件是否可执行

if指令实例

 if ($http_user_agent ~ MSIE) {      rewrite ^(.*)/msie/$1 break;  }  if ($http_cookie ~* "id=([^;]+)(?:;|$)") {      set $id $1;  }  if ($request_method = POST) {      return 405;  }  if ($slow) {      limit_rate 10k;  }  if ($invalid_referer) {      return 403;  }

return指令

语法:return code;

return code URL;

return URL;
默认值:无
做用域:server,location,if

中止处理并返回指定状态码(code)给客户端。
非标准状态码444表示关闭链接且不给客户端发响应头。
从0.8.42版本起,return 支持响应URL重定向(对于301,302,303,307),或者文本响应(对于其余状态码).
对于文本或者URL重定向能够包含变量

rewrite指令

语法:rewrite regex replacement [flag];
默认值:无
做用域:server,location,if
若是一个URI匹配指定的正则表达式regex,URI就按照replacement重写。
rewrite按配置文件中出现的顺序执行。flags标志能够中止继续处理。
若是replacement以"http://"或"https://"开始,将再也不继续处理,这个重定向将返回给客户端。
flag能够是以下参数
last 中止处理后续rewrite指令集,而后对当前重写的新URI在rewrite指令集上从新查找。
break 中止处理后续rewrite指令集,并不在从新查找,可是当前location内剩余非rewrite语句和location外的的非rewrite语句能够执行。
redirect 若是replacement不是以http:// 或https://开始,返回302临时重定向
permant 返回301永久重定向
最终完整的重定向URL包括请求scheme(http://,https://等),请求的server_name_in_redirect和 port_in_redirec三部分 ,说白了也就是http协议 域名 端口三部分组成。

rewrite实例

 server {      ...      rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;      rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;      return 403;      ...  }

若是这些rewrite放到 “/download/” location以下所示, 那么应使用break而不是last , 使用last将循环10次匹配,而后返回 500错误:

 location /download/ {      rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;      rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break;      return 403;  }

对于重写后的URL(replacement)包含原请求的请求参数,原URL的?后的内容。若是不想带原请求的参数 ,能够在replacement后加一个问号。以下,咱们加了一个自定义的参数user=$1,而后在结尾处放了一个问号?,把原请的参数去掉。

rewrite ^/users/(.*)/show?user=$1? last;

若是正则表达regex式中包含 “}” 或 “;”, 那么整个表达式须要用双引号或单引号包围.

rewrite_log指令


语法:rewrite_log on|off;
默认值:rewrite_log off;
做用域:http,server,location,if
开启或关闭以notice级别打印rewrite处理日志到error log文件。

nginx打开rewrite log例子

rewrite_log on;
error_log logs/xxx.error.log notice;

1.打开rewrite on
2.把error log的级别调整到 notice

set指令


语法:set variable value;
默认值:none
做用域:server,location,if
定义一个变量并赋值,值能够是文本,变量或者文本变量混合体。

uninitialized_variable_warn指令


语法:uninitialized_variable_warn on | off;
默认值:uninitialized_variable_warn on
做用域:http,server,location,if

控制是否输出为初始化的变量到日志

8.nginx的虚拟主机功能(nginx多站点,绑定多个域名)

两个虚拟主机(纯静态-html 支持) - Two Virtual Hosts, Serving Static Files

 

http {     server {         listen          80;         server_name     www.domain1.com;         access_log      logs/domain1.access.log main;         location / {             index index.html;             root  /var/www/domain1.com/htdocs;         }     }     server {         listen          80;         server_name     www.domain2.com;         access_log      logs/domain2.access.log main;         location / {             index index.html;             root  /var/www/domain2.com/htdocs;         }     } }

 

虚拟主机标准配置(简化) - A Default Catchall Virtual Host

 

http {     server {         listen          80 default;         server_name     _ *;         access_log      logs/default.access.log main;         location / {             index index.html;             root  /var/www/default/htdocs;         }     } }

 

在父文件夹中创建子文件夹以指向子域名 - Wildcard Subdomains in a Parent Folder

这是一个添加子域名(或是当DNS已指向服务器时添加一个新域名)的简单方法。须要注意的是,我已经将FCGI配置进该文件了。若是你只想使服务器为静态文件服务,能够直接将FCGI配置信息注释掉,而后将默认主页文件变成index.html。

这个简单的方法比起为每个域名创建一个 vhost.conf 配置文件来说,只须要在现有的配置文件中增长以下内容:

This is just a really easy way to keep adding new subdomains, or to add new domains automatically when DNS records are pointed at the server. Note that I have included FCGI here as well. If you want to just serve static files, strip out the FCGI config and change the default document to index.html. Rather than creating a new vhost.conf file for every domain, just create one of these:

 

server {         # Replace this port with the right one for your requirements         # 根据你的需求改变此端口         listen       80;  #could also be 1.2.3.4:80 也能够是1.2.3.4:80的形式         # Multiple hostnames seperated by spaces.  Replace these as well.         # 多个主机名能够用空格隔开,固然这个信息也是须要按照你的需求而改变的。         server_name  star.yourdomain.com *.yourdomain.com www.*.yourdomain.com;         #Alternately: _ *         #或者可使用:_ * (具体内容参见本维基其余页面)         root /PATH/TO/WEBROOT/$host;         error_page  404              http://yourdomain.com/errors/404.html;         access_log  logs/star.yourdomain.com.access.log;         location / {             root   /PATH/TO/WEBROOT/$host/;             index  index.php;         }         # serve static files directly         # 直接支持静态文件 (爱月说:???从配置上看来不是直接支持啊~有问题有问题~)         location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html){             access_log        off;             expires           30d;         }         location ~ .php$ {           # By all means use a different server for the fcgi processes if you need to           # 若是须要,你能够为不一样的FCGI进程设置不一样的服务信息           fastcgi_pass   127.0.0.1:YOURFCGIPORTHERE;           fastcgi_index  index.php;           fastcgi_param  SCRIPT_FILENAME  /PATH/TO/WEBROOT/$host/$fastcgi_script_name;           fastcgi_param  QUERY_STRING     $query_string;           fastcgi_param  REQUEST_METHOD   $request_method;           fastcgi_param  CONTENT_TYPE     $content_type;           fastcgi_param  CONTENT_LENGTH   $content_length;           fastcgi_intercept_errors on;         }         location ~ /\.ht {             deny  all;         }      }

9.nginx反向代理功能

nginx做为web服务器一个重要的功能就是反向代理。

固然你也可使用nginx配置正向代理,本是介绍如何配置nginx的反向代理。

nginx反向代理的指令不须要新增额外的模块,默认自带proxy_pass指令,只须要修改配置文件就能够实现反向代理。

配置前的准备工做,后端跑apache服务的ip和端口,也就是说能够经过http://ip:port能访问到你的网站。

而后就能够新建一个vhost.conf,加入以下内容,记得修改ip和域名为你的ip和域名。

修改nginx.conf,添加 include quancha.conf 到http{}段, reload nginx就能够了。

nginx反向代理模板

 


## Basic reverse proxy server ## ## Apache backend for www.quancha.cn ## upstream apachephp { server ip:8080; #Apache } ## Start www.quancha.cn ## server { listen 80; server_name www.quancha.cn; access_log logs/quancha.access.log main; error_log logs/quancha.error.log; root html; index index.html index.htm index.php; ## send request back to apache ## location / { proxy_pass http://apachephp; #Proxy Settings proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k; } } ## End www.quancha.cn ##



10.nginx负载均衡功能

使用负载均衡的话,能够修改配置http节点以下:#设定http服务器,利用它的反向代理功能提供负载均衡支持

http { #设定mime类型,类型由mime.type文件定义 include /etc/nginx/mime.types; default_type application/octet-stream; #设定日志格式 access_log /var/log/nginx/access.log; #省略上文有的一些配置节点 #。。。。。。。。。。 #设定负载均衡的服务器列表 upstream mysvr { #weigth参数表示权值,权值越高被分配到的概率越大 server 192.168.8.1x:3128 weight=5; #本机上的Squid开启3128端口,不是必需要squid server 192.168.8.2x:80 weight=1; server 192.168.8.3x:80 weight=6; } upstream mysvr2 { #weigth参数表示权值,权值越高被分配到的概率越大 server 192.168.8.x:80 weight=1; server 192.168.8.x:80 weight=6; } #第一个虚拟服务器 server { #侦听192.168.8.x的80端口 listen 80; server_name 192.168.8.x; #对aspx后缀的进行负载均衡请求 location ~ .*\.aspx$ { #定义服务器的默认网站根目录位置 root /root; #定义首页索引文件的名称 index index.php index.html index.htm; #请求转向mysvr 定义的服务器列表 proxy_pass http://mysvr ; #如下是一些反向代理的配置可删除. proxy_redirect off; #后端的Web服务器能够经过X-Forwarded-For获取用户真实IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #容许客户端请求的最大单文件字节数 client_max_body_size 10m; #缓冲区代理缓冲用户端请求的最大字节数, client_body_buffer_size 128k; #nginx跟后端服务器链接超时时间(代理链接超时) proxy_connect_timeout 90; #链接成功后,后端服务器响应时间(代理接收超时) proxy_read_timeout 90; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffer_size 4k; #proxy_buffers缓冲区,网页平均在32k如下的话,这样设置 proxy_buffers 4 32k;

#高负荷下缓冲大小(proxy_buffers*2) proxy_busy_buffers_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 proxy_temp_file_write_size 64k; } } }
11.nginx web缓存功能

为何要作web cache,我想你们最主要的是解决流量的压力。随着网站流量的提高,若是只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力。与此同时某些网站的页面内容并非常常变化,所以咱们能够分两层架构来组织网站。前端web缓存+后端web服务器,能够参看这里配置nginx反向代理配置

前端web缓存有多重方式实现,原理就是队请求结果页面静态化并设置一个超时期限,缓存页面过时后,新请求到达时从新到后端web服务器获取内容更新;没有nginx前比较流行的方法是squid,但squid不能充分利用处理器的多核特性,愈来愈多的网站选用nginx来作前端的web缓存。

要想使用nginx的缓存功能要保证nginx添加了proxy模块。咱们可使用-V选项(大写的V,小写的v是看版本号的)来查看nginx的编译参数。我使用的是默认的参数编译的,以下所示:

 

root@SNDA-172-17-12-117:/usr/local/nginx# ./nginx -V
nginx version: nginx/1.2.3
built by gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)
TLS SNI support enabled
configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.21 --with-zlib=/usr/local/src/zlib-1.2.7

nginx的全部模块必须在编译的时候添加,不能再运行的时候动态加载,默认的编译选项下包含的模块,若是你不是显示的用参数关闭它。

nginx默认安装的模块以下

模块名称 描述 版本 如何禁用
Core Control ports, locations, error pages, aliases, and other essentials.   --without-http
Access Allow/deny based on IP address.   --without-http_access_module
Auth Basic Basic HTTP authentication.   --without-http_auth_basic_module
Auto Index Generates automatic directory listings.   --without-http_autoindex_module
Browser Interpret "User-Agent" string. 0.4.3 --without-http_browser_module
Charset Recode web pages.   --without-http_charset_module
Empty GIF Serve a 1x1 image from memory. 0.3.10 --without-http_empty_gif_module
FastCGI FastCGI Support.   --without-http_fastcgi_module
Geo Set config variables using key/value pairs of IP addresses. 0.1.17 --without-http_geo_module
Gzip Gzip responses.   --without-http_gzip_module
Headers Set arbitrary HTTP response headers.  
Index Controls which files are to be used as index.  
Limit Requests Limit frequency of connections from a client. 0.7.20 --without-http_limit_req_module
Limit Zone Limit simultaneous connections from a client. Deprecated in 1.1.8, use Limit Conn Instead. 0.5.6 --without-http_limit_zone_module
Limit Conn Limit concurrent connections based on a variable.   --without-http_limit_conn_module
Log Customize access logs.  
Map Set config variables using arbitrary key/value pairs. 0.3.16 --without-http_map_module
Memcached Memcached support.   --without-http_memcached_module
Proxy Proxy to upstream servers.   --without-http_proxy_module
Referer Filter requests based on Referer header.   --without-http_referer_module
Rewrite Request rewriting using regular expressions.   --without-http_rewrite_module
SCGI SCGI protocol support. 0.8.42 --without-http_scgi_module
Split Clients Splits clients based on some conditions 0.8.37 --without-http_split_clients_module
SSI Server-side includes.   --without-http_ssi_module
Upstream For load-balancing.   --without-http_upstream_ip_hash_module (ip_hash directive only)
User ID Issue identifying cookies.   --without-http_userid_module
uWSGI uWSGI protocol support. 0.8.40 --without-http_uwsgi_module
X-Accel X-Sendfile-like module.  

proxy模块中经常使用的指令时proxy_pass和proxy_cache.

nginx的web缓存功能的主要是由proxy_cache、fastcgi_cache指令集和相关指令集完成,proxy_cache指令负责反向代理缓存后端服务器的静态内容,fastcgi_cache主要用来处理FastCGI动态进程缓存(这里我不是很清楚这两个指令的区别,好像功能上都差很少,尤为后面这句话的意思,是我翻译过来的)。

确认proxy模块安装好后,下面对nginx的配置文件进行设置,重点部分如标红字体所示。

这是个人nginx.conf配置文件。

user www-data;
worker_processes 1;

#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" "$host"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#Compression Settings
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Some version of IE 6 don't handle compression well on some mime-types,
# so just disable for them
gzip_disable "MSIE [1-6].(?!.*SV1)";
# Set a vary header so downstream proxies don't send cached gzipped
# content to IE6
gzip_vary on;
#end gzip

#cache begin
proxy_buffering on;
proxy_cache_valid any 10m;
proxy_cache_path /data/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /data/temp;
proxy_buffer_size 4k;
proxy_buffers 100 8k;
#cache end

## Basic reverse proxy server ##
## Apache (vm02) backend for www.example.com ##
upstream apachephp {
server www.quancha.cn:8080; #Apache1
}

## Start www.quancha.cn ##
server {
listen 80;
server_name *.quancha.cn;

access_log logs/quancha.access.log main;
error_log logs/quancha.error.log;
root html;
index index.html index.htm index.php;

## send request back to apache1 ##
location / {
proxy_pass http://apachephp;
proxy_cache my-cache;
proxy_cache_valid 200;

#Proxy Settings
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
##End Proxy Settings
}
}
## End www.quancha.cn ##

}

配置文件中以proxy_开头的指令咱们大均可以字面意思获得理解。请务必注意一点proxy_cache_path和proxy_temp_path设置的目录须要在同一分区,由于它们之间是硬连接的关系。

最后启动nginx,来迎接着激动人心的时刻吧。我已经火烧眉毛了。若是文章哪里有问题或者你遇到了什么麻烦,能够留言让我知道。

12.nginx php-fpm 配置

nginx自己不能处理PHP,它只是个web服务器,当接收到请求后,若是是php请求,则发给php解释器处理,并把结果返回给客户端。

nginx通常是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx

本文以php-fpm为例介绍如何使nginx支持PHP

1、编译安装php-fpm

什么是PHP-FPM

PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,能够在 http://php-fpm.org/download下载获得.

PHP-FPM实际上是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可使用。

新版PHP已经集成php-fpm了,再也不是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,能够有效控制内存和进程、能够平滑重载PHP配置,比spawn-fcgi具备更多优势,因此被PHP官方收录了。在./configure的时候带 –enable-fpm参数便可开启PHP-FPM,其它参数都是配置php的,具体选项含义能够查看这里

安装前准备
centos下执行

yum -y install gcc automake autoconf libtool make yum -y install gcc gcc-c++ glibc yum -y install libmcrypt-devel mhash-devel libxslt-devel \ libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \ zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \ ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \ krb5 krb5-devel libidn libidn-devel openssl openssl-devel

新版php-fpm安装(推荐安装方式)

wget http://cn2.php.net/distributions/php-5.4.7.tar.gz tar zvxf php-5.4.7.tar.gz cd php-5.4.7 ./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt \ --enable-mbstring --disable-pdo --with-curl --disable-debug --disable-rpath \ --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets \ --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \ --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \ --with-gd --with-jpeg-dir make all install

旧版手动打补丁php-fpm安装(旧版程序已经没有了,你们新版的吧,这里作个展现)
wget http://cn2.php.net/get/php-5.2.17.tar.gz
wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
tar zvxf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
cd php-5.2.17
./configure --prefix=/usr/local/php -with-config-file-path=/usr/local/php/etc\
-with-mysql=/usr/local/mysql\
-with-mysqli=/usr/local/mysql/bin/mysql_config -with-openssl -enable-fpm -enable-mbstring\
-with-freetype-dir -with-jpeg-dir -with-png-dir -with-zlib-dir -with-libxml-dir=/usr -enable-xml\
-with-mhash -with-mcrypt -enable-pcntl -enable-sockets  -with-bz2 -with-curl -with-curlwrappers\
-enable-mbregex -with-gd -enable-gd-native-ttf -enable-zip -enable-soap -with-iconv -enable-bcmath\
-enable-shmop -enable-sysvsem -enable-inline-optimization -with-ldap -with-ldap-sasl -enable-pdo\
-with-pdo-mysql
make all install

以上两种方式均可以安装php-fpm,安装后内容放在/usr/local/php目录下

以上就完成了php-fpm的安装。

下面是对php-fpm运行用户进行设置

cd /usr/local/php cp etc/php-fpm.conf.default etc/php-fpm.conf vi etc/php-fpm.conf

修改
user = www-data
group = www-data

若是www-data用户不存在,那么先添加www-data用户
groupadd www-data
useradd -g www-data www-data

2、编译安装nginx

而后按照http://www.nginx.cn/install 安装nginx

3、修改nginx配置文件以支持php-fpm

nginx安装完成后,修改nginx配置文件为,nginx.conf

其中server段增长以下配置,注意标红内容配置,不然会出现No input file specified.错误

# 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 $document_root$fastcgi_script_name;
include fastcgi_params;
}

4、建立测试php文件

建立php文件

在/usr/local/nginx/html下建立index.php文件,输入以下内容

<?php echo phpinfo(); ?>

5、启动服务

启动php-fpm和nginx

/usr/local/php/sbin/php-fpm #手动打补丁的启动方式/usr/local/php/sbin/php-fpm start sudo /usr/local/nginx/nginx

php-fpm关闭重启见文章结尾

6、浏览器访问

访问http://你的服务器ip/index.php,皆能够见到php信息了。

安装php-fpm时可能遇到的错误:

1. php configure时出错

configure: error: XML configuration could not be found

apt-get install libxml2 libxml2-dev  (ubuntu下) yum -y install libxml2 libxml2-develcentos下)

2. Please reinstall the BZip2 distribution

wget http://www.bzip.org/1.0.5/bzip2-1.0.5.tar.gz tar -zxvf bzip2-1.0.5.tar.gz cd bzip2-1.0.5 make make install

3. php的配置文件中有一行--with-mysql=/usr
安装的时候提示:
configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore.

这是因为安装mysql时没有安装mysql头文件,或者是路径指定不正确,php找不到mysql的头文件引发的错误提示。
解决方法。
(1.) 查看你的系统有没有安装mysql header
find / -name mysql.h
若是有。请指定--with-mysql=/跟你的正常路径。
若是没有。请看下一步。
(2.)redhat安装
rpm -ivh MySQL-devel-4.1.12-1.i386.rpm
(3.)ubuntu安装
apt-get install libmysqlclient15-dev
(4.)最后一步php的配置选项添加--with-mysql=/usr便可!

4.No input file specified.

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

5. 若是php configure时缺库,能够先安装库(ubuntu下)

sudo apt-get install make bison flex gcc patch autoconf subversion locate
sudo apt-get install libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 libcurl4-openssl-dev libpq-dev libpq5 libsyck0-dev

6. mcrypt.h not found. Please reinstall libmcrypt

apt-get install libmcrypt-dev

或者

cd /usr/local/src
wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd /usr/local/src/libmcrypt-2.5.8
./configure --prefix=/usr/local
make
make install

7. php-fpm 5.4.7 如何关闭 重启?

php 5.4.7 下的php-fpm 再也不支持 php-fpm 之前具备的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,须要使用信号控制:

master进程能够理解如下信号

INT, TERM 马上终止 QUIT 平滑终止 USR1 从新打开日志文件 USR2 平滑重载全部worker进程并从新载入配置和二进制模块

示例:

php-fpm 关闭:

kill -INT `cat /usr/local/php/var/run/php-fpm.pid`

php-fpm 重启:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

查看php-fpm进程数:

ps aux | grep -c php-fpm

8.命令行下执行php,提示找不到命令

-bash: /usr/bin/php: No such file or directory

vi /etc/profile

在文件底部增长一行配置
export PATH=/usr/local/php/bin:$PATH

保存退出

source /etc/profile

13.nginx屏蔽ip

采集和防止采集是一个经久不息的话题,一方面都想搞别人的东西,另外一方面不想本身的东西被别人搞走。

本文介绍如何利用nginx屏蔽ip来实现防止采集,固然也能够经过iptable来实现。

1.查找要屏蔽的ip

awk '{print $1}' nginx.access.log |sort |uniq -c|sort -n

nginx.access.log 为日志文件,

会到以下结果,前面是ip的访问次数,后面是ip,很明显咱们须要把访问次数多的ip而且不是蜘蛛的ip屏蔽掉,本例当中咱们屏蔽掉165.91.122.67

 ...   13610 202.112.113.192   95772 180.169.22.135  337418 219.220.141.2  558378 165.91.122.67

2.在nginx的安装目录下面,新建屏蔽ip文件,命名为blockip.conf,之后新增长屏蔽ip只需编辑这个文件便可。 加入以下内容

deny 165.91.122.67; 

保存一下。

3.在nginx的配置文件nginx.conf中加入以下配置,能够放到http, server, location, limit_except语句块,须要注意相对路径,本例当中nginx.conf,blocksip.conf在同一个目录中。

include blockip.conf; 

 

4.重启一下nginx的服务:/usr/local/nginx/nginx -s reload 就能够生效了。

高级用法:

屏蔽ip的配置文件既能够屏蔽单个ip,也能够屏蔽ip段,或者只容许某个ip或者某个ip段访问。

# 屏蔽单个ip访问

deny IP; 

# 容许单个ip访问

allow IP; 

# 屏蔽全部ip访问

deny all; 

# 容许全部ip访问

allow all; 

#屏蔽整个段即从123.0.0.1到123.255.255.254访问的命令

deny 123.0.0.0/8

#屏蔽IP段即从123.45.0.1到123.45.255.254访问的命令

deny 124.45.0.0/16

#屏蔽IP段即从123.45.6.1到123.45.6.254访问的命令

deny 123.45.6.0/24

若是你想实现这样的应用,除了几个IP外,其余所有拒绝,
那须要你在blockip.conf中这样写

allow 1.1.1.1;  allow 1.1.1.2; deny all; 

单独网站屏蔽IP的方法,把include blocksip.conf; 放到网址对应的在server{}语句块,全部网站屏蔽IP的方法,把include blocksip.conf; 放到http {}语句块。

相关文章
相关标签/搜索