一、Nginx简介
Nginx(engine X)是一个高性能的HTTP和反向代理服务器,Nginx是模块化设计、较好的扩展性、高可靠、低内存消耗、支持热部署、支持事件驱动、AIO、mmap等特性。
二、Nginx功能
从功能上来Nginx分为基本功能 和扩展功能:
基本功能:
(1)静态资源的Web服务器,能缓存打开的文件描述符;
(2)http、smtp、pop3协议的反向代理服务器;
(3)缓存加速、负载均衡;
(4)支持FastCGI(fpm ,LNMP)、uWSGI(python)等;
(5)模块化(非DSO机制)、过滤器zip、SSI及图像的大小调整;
(6)支持SSL;
扩展功能:
(1)基本名称和IP的虚拟主机;
(2)支持Keepalive
(3)支持平滑升级
(4)定制访问日志、支持使用日志缓存区提供日志存储性能;
(5)支持url rewrite
(6)支持路径别名
(7)支持基本IP及用户的访问控制
(8)支持速率限制、支持并发数限制
三、Nginx工作模式
Nginx是非阻塞、事件驱动、由一个master进程,生成一个或多个worker进行;每个worker响应多个请求;
Nginx由众多的模块组成分别有核心模块、Standard HTTP Module、Options HTTP Module、Mail Module、3rd party modules;
四、Nginx安装
Nginx支持编译安装和制作好的程序包两种方式安装。
4.1、rpm包安装
rpm包安装配置好epel源以后可以直接用yum进行安装即可
4.2、编译安装
1)首先安装编译环境
yum groupinstall "Developmenttools" "Server Platform Development" -y
2)下载nginx程序包
[root@node-3 ~]# mkdir source [root@node-3 ~]# cd source/ [root@node-3 source]# wget http://nginx.org/download/nginx-1.6.3.tar.gz --2015-05-24 20:18:24-- http://nginx.org/download/nginx-1.6.3.tar.gz Resolving nginx.org... 206.251.255.63 Connecting tonginx.org|206.251.255.63|:80... connected. HTTP request sent, awaiting response... 200OK Length: 805253 (786K)[application/octet-stream] Saving to: ?.ginx-1.6.3.tar.gz? 100%[================================================================>]805,253 71.0K/s in 12s 2015-05-24 20:18:37 (66.1 KB/s) -?.ginx-1.6.3.tar.gz?.saved [805253/805253]
3)编译安装
[root@node-3 source]# tar xfnginx-1.6.3.tar.gz
[root@node-3 source]# cd nginx-1.6.3
[root@node-3 nginx-1.6.3]# useradd -r nginx
[root@node-3 nginx-1.6.3]# ./configure --prefix=/usr/local/nginx--conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log--pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock--with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module--with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/var/tmp/nginx/client--http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
[root@node-3 nginx-1.6.3]# make &&make install
[root@node-3 nginx-1.6.3]# mkdir -pv/var/tmp/nginx/{client,proxy,fastcgi,uwsgi}
安装选项解释:
--prefix=/usr/local/nginx #Nginx安装路径。如果没有指定,默认为 /usr/local/nginx
--conf-path=/etc/nginx/nginx.conf #在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。
--user=nginx #在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。
--group=nginx #在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。
--error-log-path=/var/log/nginx/error.log#nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。
--http-log-path=/var/log/nginx/access.log #在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为<prefix>/logs/access.log。
--pid-path=/var/run/nginx/nginx.pid #在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为<prefix>/logs/nginx.pid。
--lock-path=/var/lock/nginx.lock #nginx.lock文件的路径。
--with-http_ssl_module #开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL
--with-http_stub_status_module #启用 "server status" 页
--with-http_gzip_static_module #用户gzip压缩
--with-http_flv_module #启用 ngx_http_flv_module
--with-http_mp4_module #启用ngx_http_mp4_moduel
--http-client-body-temp-path=/var/tmp/nginx/client#Set path to the http client request body temporary files
--http-proxy-temp-path=/var/tmp/nginx/proxy#Set path to the http proxy temporary files
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi#Set path to the http fastcgi temporary files
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi #set path to store http uwsgi temporary files4)编译报错
checking for PCRE library in /usr/local/... not found checking for PCRE library in/usr/include/pcre/ ... not found checking for PCRE library in /usr/pkg/ ...not found checking for PCRE library in /opt/local/... not found ./configure: error: the HTTP rewrite modulerequires the PCRE library. You can either disable the module by using--without-http_rewrite_module option, or install the PCRE library intothe system, or build the PCRE library statically from the source with nginx byusing --with-pcre=<path> option.
解决方法:
[root@node-3 nginx-1.6.3]# yum installpcre-devel -y
五、Nginx配置解释
[root@node-3 ~]# egrep -v"^$|^.*#" /etc/nginx/nginx.conf
worker_processes 1; #Nginx工作的worker进行数
events { #定义event模式工作特性
worker_connections 1024; #设定单个worker进程所能够处理的最大并发连接数数量,(最大连接=连接数x进程数)
}
http { #定义http协议相关的配置
include mime.types;
default_type application/octet-stream;
sendfile on; #开启高效文件传输模式
keepalive_timeout 65; #长链接超时时间
server { #定义一个虚拟机
listen 80; #指定监听的地址和端口
server_name localhost; #指定一个主机名
location / { #允许根据用户请求的URI来匹配定义的各location:匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能;
root html; #设置资源路径映射,用于指明请求的URL所对应的资源所在的文件系统上的起始路径;
index index.html index.htm; #指定默认主页面
}
error_page 500 502 503 504 /50x.html; #根据http响应码来指明特用的错误页面
location = /50x.html {
root html;
}
}
}
六、Nginx简单配置
6.1启动Nginx
[root@node-3 ~]#/usr/local/nginx/sbin/nginx [root@node-3 ~]# lsof -n -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 4735 root 6u IPv4 14326 0t0 TCP *:http (LISTEN) nginx 4736 nginx 6u IPv4 14326 0t0 TCP *:http (LISTEN)
在浏览器中输入http://Server_ip
6.2新建一个虚拟主机
新建一个虚拟主机端口设置成8080,主机名www.wuday.com,站点文件存放在/web/wuday.com
6.2.1 nginx.conf配置
在http{}标签中添加server{}标准;
[root@node-3 ~]# cd /etc/nginx/
[root@node-3 nginx]# vim nginx.conf
server {
listen 8080;
server_name www.wuday.com;
location / {
root "/web/wuday";
index index.html;
}6.2.2提供站点文件
[root@node-3 ~]# mkdir /web/wuday -p [root@node-3 ~]# vim /web/wuday/index.html [root@node-3 ~]# cat /web/wuday/index.html <h1>www.wuday.com</h1>
6.2.3 检查配置文件并重新加载配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.2.4 使用浏览器进行访问
6.3 为6.2提供一个错误优雅显示页面
6.3.1 配置nginx.conf文件
在nginx.conf文件中6.2的server{}标签添加如下内容
error_page 500 502 503 504 =200/50x.html;
location = /50x.html {
root "/web/wuday/error";
}
error_page 400 402 403 404=200 /40x.html;
location = /40x.html {
root "/web/wuday/error";
}6.3.2 创建错误目标及页面
[root@node-3 ~]# mkdir /web/wuday/error [root@node-3 ~]# cd /web/wuday/error [root@node-3 error]# cat 40x.html <h1>Server is Move...Plz mail toadmin@wuday.com</h1> <h1>40x</h1> [root@node-3 error]# cat 50x.html <h1>Server is fle.....</h1> <h1>50x</h1>
6.3.3 检查语法并重新加载配置文件
[root@node-3 ~]# /usr/local/nginx/sbin/nginx-t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.3.4 在浏览器中进行访问测试
在http://Server_ip:8080后输入一个不存在的页面,可以看出状态码为200,并不是403
6.4 设计站点基于IP的访问控制
6.4.1 配置nginx.conf文件
站点不能让172.16.0.0/16网段的用户访问,其它用户都可以随意的访问。
在nginx.conf文件中6.2的server{}标签添加如下内容
deny 172.16.0.0/16; allow all;
6.4.2 检查语法并重新加载配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.4.4 在浏览器中进行访问测试
在http://Server_ip:8080进行测试,根据测试来看为什么没有被定位至错误页面呢?
6.5 基本用户的访问控制
6.5.1 配置nginx.conf文件
注释上面设置基于IP的访问控制,并添加以下内容
#deny 172.16.0.0/16; #allow all; auth_basic "Plz Username and Password..."; auth_basic_user_file "/etc/nginx/user/.ngpasswd";
6.5.2 创建密码文件
[root@node-3 ~]# mkdir /etc/nginx/user [root@node-3 ~]# htpasswd -c/etc/nginx/user/.ngpasswd wuday New password: Re-type new password: Adding password for user wuday
6.5.3检查语法并重新加载配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.5.4在浏览器中进行访问测试
在http://Server_ip:8080进行测试,提示输入用户名和密码。输入正确验证通过后就可以访问站点;
6.6 为www.wuday.com提供https服务
6.6.1 配置nginx.conf
在nginx.conf文件注释前面的用户认证,在文件后添加如下内容:
server {
listen 443 ssl;
server_name localhost;
ssl_certificate "/etc/nginx/ssl/httpd.crt";
ssl_certificate_key "/etc/nginx/ssl/httpd.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root "/web/wuday";
index index.html index.htm;
}
}6.6.2 创建私钥文件
这里就不给出创建步骤,请参考我的相关博文。
6.6.3 修改本地的hosts文件
在本地电脑上的hosts文件中添加一行用于解析www.wuday.com
172.16.9.35 www.wuday.com
6.6.4检查语法并重新加载配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.6.5在浏览器中进行访问测试
在https://www.wuday.com进行测试,在没有把根证书添加到受信任的根证书颁发机构时,会出现以下提示,点“继续浏览此网站(不推荐)。”就能查看访问站点了。
添加CA的证书到受信任的根证书颁发机构后,访问的效果如下:
6.7 为站点提供状态页
6.7.1 配置nginx.conf
站点的状态面应该是只能特定的用户访问。在server {}的8080端口的那个server {}标签后面添加如下内容:
location /status {
stub_statuson;
allow172.16.0.0/16;
denyall;
}6.7.2检查语法并重新加载配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.7.3 在浏览器中进行访问测试
在http://Server_ip:8080/status进行测试
状态信息解析:
Active connections: 1 #当前所有处于打开状态的连接数;
server accepts handled requests
85 85 236
#第一个数:已经接受的连接数;
#第二个数:已经处理过的连接数;
#第三个数:已经处理过的请求数,在“保持连接”模式下,请求数量可能会多于连接数量;
Reading: 0 Writing: 1 Waiting: 0
Reading:正处于接收请求状态的连接数;
Writing:请求已经接收完成,正处于处理请求或发送响应的过程中的连接数;
Waiting:保护连接模式,且处于活动状态的连接数;
6.8 搭建一个LNMP平台
在Nginx上搭建一个LNMP平台,对于php和MySQL的安装这里就不说明了,请参考我相关的博文。
6.8.1配置nginx.conf
在index中添加一个index.php的主页,并放至前面。
index index.php index.html;
location ~ \.php$ {
root "/web/wp";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}6.8.2 创建用于测试的index.php
<?php phpinfo(); ?>
6.8.3 检查语法并重新加载配置文件
[root@node-3 ~]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file/etc/nginx/nginx.conf syntax is ok nginx: configuration file/etc/nginx/nginx.conf test is successful [root@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload
6.8.4 在浏览器中进行访问测试
在http://Server_ip:8080/进行测试,只能出现以下测试页面,说明php就没有问题了。
本文出自 “乌大宛” 博客,请务必保留此出处http://wuday.blog.51cto.com/2623944/1655809
原文地址:http://wuday.blog.51cto.com/2623944/1655809