设为首页 收藏本站
查看: 972|回复: 0

[经验分享] Nginx详解

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-5-29 09:24:05 | 显示全部楼层 |阅读模式
一、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)首先安装编译环境
1
yum groupinstall "Developmenttools" "Server Platform Development" -y





2)下载nginx程序包
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@node-3 ~]# mkdir source
[iyunv@node-3 ~]# cd source/
[iyunv@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)编译安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[iyunv@node-3 source]# tar xfnginx-1.6.3.tar.gz
[iyunv@node-3 source]# cd nginx-1.6.3
[iyunv@node-3 nginx-1.6.3]# useradd -r nginx
[iyunv@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
[iyunv@node-3 nginx-1.6.3]# make &&make install
[iyunv@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的路径。如果没有指定,默认为/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指令的情况下,默认的错误日志的路径。如果没有指定,默认为 /logs/error.log。
--http-log-path=/var/log/nginx/access.log #在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为/logs/access.log。
--pid-path=/var/run/nginx/nginx.pid #在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为/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 files





4)编译报错
1
2
3
4
5
6
7
8
9
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= option.





解决方法:
1
[iyunv@node-3 nginx-1.6.3]# yum installpcre-devel -y





五、Nginx配置解释
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[iyunv@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
1
2
3
4
5
[iyunv@node-3 ~]#/usr/local/nginx/sbin/nginx
[iyunv@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
wKioL1Vl3tihGX30AAiiAOXsV4Y484.bmp

6.2新建一个虚拟主机
  新建一个虚拟主机端口设置成8080,主机名www.wuday.com,站点文件存放在/web/wuday.com
6.2.1 nginx.conf配置
  在http{}标签中添加server{}标准;
1
2
3
4
5
6
7
8
9
[iyunv@node-3 ~]# cd /etc/nginx/
[iyunv@node-3 nginx]# vim nginx.conf
  server {
       listen 8080;
       server_name www.wuday.com;
       location / {
            root "/web/wuday";
            index index.html;
       }





6.2.2提供站点文件
1
2
3
4
[iyunv@node-3 ~]# mkdir /web/wuday -p
[iyunv@node-3 ~]# vim /web/wuday/index.html
[iyunv@node-3 ~]# cat /web/wuday/index.html
www.wuday.com





6.2.3 检查配置文件并重新加载配置文件
1
2
3
4
[iyunv@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
[iyunv@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload





6.2.4 使用浏览器进行访问
wKiom1Vl3XLhTFtsAAOHmO5HqLQ320.bmp

6.3 为6.2提供一个错误优雅显示页面
6.3.1 配置nginx.conf文件
  在nginx.conf文件中6.2的server{}标签添加如下内容
1
2
3
4
5
6
7
8
       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 创建错误目标及页面
1
2
3
4
5
6
7
8
[iyunv@node-3 ~]# mkdir /web/wuday/error
[iyunv@node-3 ~]# cd /web/wuday/error
[iyunv@node-3 error]# cat 40x.html
Server is Move...Plz mail toadmin@wuday.com
40x
[iyunv@node-3 error]# cat 50x.html
Server is fle.....
50x





6.3.3 检查语法并重新加载配置文件
1
2
3
4
[iyunv@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
[iyunv@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload





6.3.4 在浏览器中进行访问测试
  在http://Server_ip:8080后输入一个不存在的页面,可以看出状态码为200,并不是403
wKioL1Vl3zKh_tHYAAmmiNVWLiM832.bmp

6.4 设计站点基于IP的访问控制
6.4.1 配置nginx.conf文件
  站点不能让172.16.0.0/16网段的用户访问,其它用户都可以随意的访问。
在nginx.conf文件中6.2的server{}标签添加如下内容
1
2
       deny 172.16.0.0/16;
       allow all;





6.4.2 检查语法并重新加载配置文件
1
2
3
4
[iyunv@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
[iyunv@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload





6.4.4 在浏览器中进行访问测试
  在http://Server_ip:8080进行测试,根据测试来看为什么没有被定位至错误页面呢?
wKiom1Vl3duAWT-LAAbJrOnzffU214.bmp

6.5 基本用户的访问控制
6.5.1 配置nginx.conf文件
  注释上面设置基于IP的访问控制,并添加以下内容
1
2
3
4
#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 创建密码文件
1
2
3
4
5
[iyunv@node-3 ~]# mkdir /etc/nginx/user
[iyunv@node-3 ~]# htpasswd -c/etc/nginx/user/.ngpasswd wuday
New password:
Re-type new password:
Adding password for user wuday





6.5.3检查语法并重新加载配置文件
1
2
3
4
[iyunv@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
[iyunv@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload





6.5.4在浏览器中进行访问测试
  在http://Server_ip:8080进行测试,提示输入用户名和密码。输入正确验证通过后就可以访问站点;
wKioL1Vl36awslGdAAtzqO7X-so053.bmp

6.6 为www.wuday.com提供https服务
6.6.1 配置nginx.conf
在nginx.conf文件注释前面的用户认证,在文件后添加如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
1
172.16.9.35 www.wuday.com





6.6.4检查语法并重新加载配置文件
1
2
3
4
[iyunv@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
[iyunv@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload





6.6.5在浏览器中进行访问测试
  在https://www.wuday.com进行测试,在没有把根证书添加到受信任的根证书颁发机构时,会出现以下提示,点“继续浏览此网站(不推荐)。”就能查看访问站点了。
wKiom1Vl3mzjf8g5AAq-kELVLEM969.bmp
wKioL1Vl4Aby_b9GAARE8FiLXRw305.bmp

添加CA的证书到受信任的根证书颁发机构后,访问的效果如下:
wKioL1Vl4CaTEkOqAATVuO5jo0I789.bmp

6.7 为站点提供状态页
6.7.1 配置nginx.conf
  站点的状态面应该是只能特定的用户访问。在server {}的8080端口的那个server {}标签后面添加如下内容:
1
2
3
4
5
location /status  {
    stub_statuson;
    allow172.16.0.0/16;
    denyall;
}





6.7.2检查语法并重新加载配置文件
1
2
3
4
[iyunv@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
[iyunv@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload





6.7.3 在浏览器中进行访问测试
  在http://Server_ip:8080/status进行测试
wKioL1Vl4ECCX9o4AARfpOLCeuY817.bmp
状态信息解析:
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的主页,并放至前面。
1
2
3
4
5
6
7
8
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
1
2
3
phpinfo();
?>





6.8.3 检查语法并重新加载配置文件
1
2
3
4
[iyunv@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
[iyunv@node-3 ~]#/usr/local/nginx/sbin/nginx -s reload






6.8.4 在浏览器中进行访问测试
  在http://Server_ip:8080/进行测试,只能出现以下测试页面,说明php就没有问题了。
wKiom1Vl3tXR3hbSAAewPGNXqZI969.bmp



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-71777-1-1.html 上篇帖子: nginx中配置虚拟主机 下篇帖子: I/O模型与Nginx配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表