首页 > 代码库 > Nginx学习指南之安装与配置

Nginx学习指南之安装与配置

Nginx学习指南之安装与配置

一、简介

Nginx [engine x]Igor Sysoev编写的一个HTTP和反向代理服务器,另外它可以作为邮件代理服务器。Nginx已经在众多流量很大的俄罗斯网站上使用了很长时间,这些网站包括YandexMail.RuVKontakte,以及Rambler,在国内使用Nginx作为Web ServerCache ServerProxy Server网站包括百度,新浪,腾讯,网易,机锋网等,根据Netcraft统计,在20128月份,世界上最繁忙的网站中有11.48使用Nginx作为其服务器或者代理服务器。


Nginx是少数写入地址的C10K问题之一的服务器,不同于传统的服务器的是Nginx不依赖于线程来处理请求。相反,它使用了一个更具扩展性的事件驱动(异步)架构。这种结构采用小,但更重要的是,内存负载下可预见的数额。即便你不希望处理成千上万的并发请求,你仍然可以受益于Nginx的高性能和小内存占用。然而促使这些大型互联网企业选择Nginx的原因是什么,不仅仅是因为Nginx开源,而是因为Nginx拥有更多的特性。

11 Nginx 特性

1>处理静态文件,索引文件以及自动索引,能打开文件描述符缓存。

2>作为前端缓存服务器,可以加速用户访问请求,减轻后端应用服务器压力。

3>作为反向代理服务器,根据源地址哈希,权重实现负载均衡以及健康检查的容错功能。

4>远程FastcgiuwsgiSCGI,和memcached服务的缓存加速支持。

Note了解更多特性,请参阅官方文档:http://nginx.org/cn/


二、Nginx工作原理示意图

wKioL1NqDlmRDSa6AAD0YVTFHik568.jpg

启动Nginx服务进程后,socket处于监听状态,当它侦听到一个HTTP请求时,Nginx Core通过查找配置文件将客户端请求映射到一个location blocklocationNginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。

三、安装与配置

31 下载

##提示:

在执行下面的操作前,你必须保证你的服务器或则VMware虚拟机能够连接互联网,不然你看到的只是错误的提示;

[root@webserver1 ~]# mkdir /download/source -p
[root@webserver1 ~]# cd /download/source/
[root@webserver1 source]# wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
--2014-05-07 00:33:45--  http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
Resolving nginx.org... 206.251.255.63
Connecting to nginx.org|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4311 (4.2K) [application/x-redhat-package-manager]
Saving to: °nginx-release-centos-6-0.el6.ngx.noarch.rpm±
100%[==========================================================================================>] 4,311       25.5K/s   in 0.2s   
2014-05-07 00:33:45 (25.5 KB/s) - °nginx-release-centos-6-0.el6.ngx.noarch.rpm± saved [4311/4311]
[root@webserver1 source]# ll nginx-release-centos-6-0.el6.ngx.noarch.rpm
-rw-r--r-- 1 root root 4311 Oct 14  2011 nginx-release-centos-6-0.el6.ngx.noarch.rpm

32 RPM安装

##提示:

若使用Yum的方式安装,Nginx的功能是官方已经定制好的,如果你想使用nginx第三方模块,如Nginx网站加速模块ngx_pagespeed、文件上传模块nginx_upload_module、负载均衡模块nginx_upstream-fair,则需要下载Ngnx源码包后,附加上你需要的功能,然后编译安装;

[root@webserver1 source]# yum install nginx
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
Setting up Install Process
Package nginx-1.6.0-1.el6.ngx.x86_64 already installed and latest version
Note:事先已经安装过,再次安装便会提示already install;

33 启动Nginx服务

[root@webserver1 source]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]
[root@webserver1 source]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State        PID/Program name  
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                  LISTEN      30063/nginx        
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                  LISTEN      1910/sshd          
tcp        0      0 127.0.0.1:631               0.0.0.0:*                  LISTEN      1805/cupsd         
tcp        0      0 127.0.0.1:25                0.0.0.0:*                  LISTEN      2061/master        
tcp        0      0 :::139                      :::*                      LISTEN      29183/smbd         

34 测试

## 本地测试
[root@webserver1 source]# curl -I http://10.16.10.25/index.html
HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Wed, 07 May 2014 08:06:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Apr 2014 13:32:20 GMT
Connection: keep-alive
ETag: "53591264-264"
Accept-Ranges: bytes

## Windowsfirefox浏览器测试

wKiom1NqD7KQ9KYDAAJWiDXRnIc025.jpg

35 官方的编译参数

configure arguments: --prefix=/etc/nginx--sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf--error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid--lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp--http-proxy-temp-path=/var/cache/nginx/proxy_temp--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp--http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module--with-http_realip_module--with-http_addition_module --with-http_sub_module--with-http_dav_module --with-http_flv_module --with-http_mp4_module--with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module--with-http_secure_link_module --with-http_stub_status_module--with-http_auth_request_module --with-mail --with-mail_ssl_module--with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt=‘-O2-g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector--param=ssp-buffer-size=4 -m64 -mtune=generic‘

四、Make编译安装

41 下载

官方下载地址:http://nginx.org/download/nginx-1.6.0.tar.gz#稳定版

[root@webserver1 ~]# mkdir /download/source -p
[root@webserver1 ~]# cd /download/source/
[root@webserver1 source]# wget http://nginx.org/download/nginx-1.6.0.tar.gz
--2014-05-07 01:28:18--  http://nginx.org/download/nginx-1.6.0.tar.gz
Resolving nginx.org... 206.251.255.63
Connecting to nginx.org|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 802956 (784K) [application/octet-stream]
Saving to: °nginx-1.6.0.tar.gz±
100%[==============================================================>] 802,956      145K/s   in 10s    
2014-05-07 01:28:29 (76.5 KB/s) - °nginx-1.6.0.tar.gz± saved [802956/802956]
[root@webserver1 source]# ll nginx-1.6.0.tar.gz
-rw-r--r-- 1 root root 802956 Apr 24 06:03 nginx-1.6.0.tar.gz

42 安装

421 编译参数

[root@webserver1 source]# tar –xf nginx-1.6.0.tar.gz
[root@webserver1 source]# cd nginx-1.6.0
[root@webserver1 nginx-1.6.0]# ./configure --prefix=/application/nginx-1.6.0 --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-ipv6 --with-cc-opt=‘-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic‘
[root@webserver1 nginx-1.6.0]# make
[root@webserver1 nginx-1.6.0]# make install

43 配置

[root@webserver1 nginx-1.6.0]# ln -s /application/nginx-1.6.0/ /application/nginx
[root@webserver1 nginx-1.6.0]# cd /application/nginx
[root@webserver1 nginx]# groupadd nginx
[root@webserver1 nginx]# useradd –r –g nginx nginx
[root@webserver1 nginx]# vim ./conf/nginx.conf
user  nginx nginx;
worker_processes  8;
error_log  logs/error.log  notice;
pid        logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
    use epoll;
    worker_connections  65535;
}
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"‘;
    access_log  logs/access.log  main;
    sendfile        on;
tcp_nopush      on;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
keepalive_timeout  60;
# Load Nginx comperssion module
gzip  on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 3;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/css application/x-javascript text/xml application/xml text/javascript;
gzip_vary on;
server {
    listen       80;
    server_name  localhost;
   #charset koi8-r;
   charset utf-8;
location / {
            root   /data/bbs/program;
            index  index.html index.htm;
        }
   location ~* \.(gif|jpg|jpeg|png|bmp|swf)$ {
       expires 1d;
   }
   location ~* \.(css|js)$ {
       expires 1h;
   }
   error_page  404              /404.html;
   # redirect server error pages to the static page /50x.html
   #
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
      root   html;
   }
    …………………PHP,SSL部分省略……………………
}

44 检查语法,端口

[root@webserver1 nginx]# mkdir /data/bbs/program/ -p
[root@webserver1 nginx]# vim /data/bbs/program/index.html
<h1><center>Welcome to www.gfan.com site</center></h1>
[root@webserver1 nginx]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.0/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.0/conf/nginx.conf test is successful
[root@webserver1 nginx]# /application/nginx/sbin/nginx
[root@webserver1 nginx]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                 LISTEN      37376/nginx  

45 ulimit 调优

##提示

文件句柄数,建议与nginx配置文件的rlimitconntion参数保持一致;

[root@webserver1 nginx]# vim /etc/security/limits.conf              #在文件最后,添加以下两行;
*               soft    nofile          65535
*               hard    nofile          65535
[root@webserver1 nginx]# ulimit -SHn 65535
[root@webserver1 nginx]# ulimit -n
65535

46 测试

##本地测试
[root@webserver1 nginx]# curl -I http://localhost/index.html                       
HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Wed, 07 May 2014 09:42:06 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 38
Last-Modified: Wed, 07 May 2014 09:29:43 GMT
Connection: keep-alive
ETag: "5369fd07-26"

##Windowsfirefox浏览器测试

wKioL1NqEMLxq0PeAAGS3tzEYE8924.jpg

##备注

此篇文档介绍Nginx两种最基础的安装方式与简单的优化过程,希望能带给大家一些帮助,后续会有更多关于Nginx的文章,不足之处,希望大家留言,一起交流,交流,共同进步,在此感谢。


本文出自 “人生有梦而精彩” 博客,请务必保留此出处http://cfwlxf.blog.51cto.com/3966339/1408076