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

安装Nginx过程全解析

2014-09-23 10:27 316 查看
安装Nginx过程

1 建立用户及组


/usr/sbin/groupadd www

/usr/sbin/useradd -g www www

2 安装pcre 让安装Nginx支持rewrite 方便以后所需


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
tar zxvf pcre-7.8.tar.gz

cd pcre-7.8/

./configure

make && make install

3 安装Nginx


wget http://sysoev.ru/nginx/nginx-0.7.58.tar.gz
tar zxvf nginx-0.7.58.tar.gz

cd nginx-0.7.58/

./configure --user=www --group=www --prefix=/usr/local/

webserver/nginx --with-http_stub_status_module

--with-http_ssl_module --with-cc-opt='-O2'

--with-cpu-opt=opteron

make && make install

注意上文中的--with-cc-opt='-O2' --with-cpu-opt=opteron 这是编译器优化,目前最常用的是-02 而不是3.后面对应CPU的型号,可参照:http://wiki.gentoo.tw/index.php/HOWTO_CFLAG

配置及优化配置文件

Nginx.conf 配置文件:


user www www;

worker_processes 4;

# [ debug | info | notice | warn | error | crit ]

error_log /usr/local/webserver/nginx/logs/nginx_error.log

crit;

pid /usr/local/webserver/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that

can be opened by this process.

worker_rlimit_nofile 51200;

events

{

use epoll;

worker_connections 51200;

}

http

{

include mime.types;

default_type application/octet-stream;

source_charset GB2312;

server_names_hash_bucket_size 256;

client_header_buffer_size 256k;

large_client_header_buffers 4 256k;

#size limits

client_max_body_size 50m;

client_body_buffer_size 256k;

client_header_timeout 3m;

client_body_timeout 3m;

send_timeout 3m;

#参数都有所调整.目的是解决代理过程中出现的一些502 499错误

sendfile on;

tcp_nopush on;

keepalive_timeout 120; #参数加大,以解决做代理时502错误

tcp_nodelay on;

include vhosts/upstream.conf;

include vhosts/bbs.linuxtone.conf;

}

Nginx 使用 Unix 下常用的 ./configure && make && make install 过程来编译安装。

configure 脚本确定系统所具有一些特性,特别是 nginx 用来处理连接的方法。然后,它创建 Makefile 文件。

configure 支持下面的选项:

--prefix=<path> - Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。

--sbin-path=<path> - Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。

--conf-path=<path> - 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。

--pid-path=<path> - 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。

--lock-path=<path> - nginx.lock文件的路径。

--error-log-path=<path> - 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。

--http-log-path=<path> - 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。

--user=<user> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。

--group=<group> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。

--builddir=DIR - 指定编译的目录

--with-rtsig_module - 启用 rtsig 模块

--with-select_module --without-select_module - Whether or not to enable the select module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.

//允许或不允许开启SELECT模式,如果 configure 没有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(实时信号)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式--with-poll_module
--without-poll_module - Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.

--with-http_ssl_module - Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev.

//开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl

--with-http_realip_module - 启用 ngx_http_realip_module

--with-http_addition_module - 启用 ngx_http_addition_module

--with-http_sub_module - 启用 ngx_http_sub_module

--with-http_dav_module - 启用 ngx_http_dav_module

--with-http_flv_module - 启用 ngx_http_flv_module

--with-http_stub_status_module - 启用 "server status" 页

--without-http_charset_module - 禁用 ngx_http_charset_module

--without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。

--without-http_ssi_module - 禁用 ngx_http_ssi_module

--without-http_userid_module - 禁用 ngx_http_userid_module

--without-http_access_module - 禁用 ngx_http_access_module

--without-http_auth_basic_module - 禁用 ngx_http_auth_basic_module

--without-http_autoindex_module - 禁用 ngx_http_autoindex_module

--without-http_geo_module - 禁用 ngx_http_geo_module

--without-http_map_module - 禁用 ngx_http_map_module

--without-http_referer_module - 禁用 ngx_http_referer_module

--without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。

--without-http_proxy_module - 禁用 ngx_http_proxy_module

--without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module

--without-http_memcached_module - 禁用 ngx_http_memcached_module

--without-http_limit_zone_module - 禁用 ngx_http_limit_zone_module

--without-http_empty_gif_module - 禁用 ngx_http_empty_gif_module

--without-http_browser_module - 禁用 ngx_http_browser_module

--without-http_upstream_ip_hash_module - 禁用 ngx_http_upstream_ip_hash_module

--with-http_perl_module - 启用 ngx_http_perl_module

--with-perl_modules_path=PATH - 指定 perl 模块的路径--with-perl=PATH - 指定 perl 执行文件的路径

--http-log-path=PATH - Set path to the http access log

--http-client-body-temp-path=PATH - Set path to the http client request body temporary files

--http-proxy-temp-path=PATH - Set path to the http proxy temporary files

--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files

--without-http - 禁用 HTTP server

--with-mail - 启用 IMAP4/POP3/SMTP 代理模块

--with-mail_ssl_module - 启用 ngx_mail_ssl_module

--with-cc=PATH - 指定 C 编译器的路径

--with-cpp=PATH - 指定 C 预处理器的路径

--with-cc-opt=OPTIONS - Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to
increase the number of file descriptors, then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048".

--with-ld-opt=OPTIONS - Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib".

--with-cpu-opt=CPU - 为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64--without-pcre - 禁止
PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也需要 PCRE 。

--with-pcre=DIR - 指定 PCRE 库的源代码的路径。

--with-pcre-opt=OPTIONS - Set additional options for PCRE building.

--with-md5=DIR - Set path to md5 library sources.

--with-md5-opt=OPTIONS - Set additional options for md5 building.

--with-md5-asm - Use md5 assembler sources.

--with-sha1=DIR - Set path to sha1 library sources.

--with-sha1-opt=OPTIONS - Set additional options for sha1 building.

--with-sha1-asm - Use sha1 assembler sources.

--with-zlib=DIR - Set path to zlib library sources.

--with-zlib-opt=OPTIONS - Set additional options for zlib building.

--with-zlib-asm=CPU - Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro

--with-openssl=DIR - Set path to OpenSSL library sources

--with-openssl-opt=OPTIONS

Set additional options for OpenSSL building

--with-debug - 启用调试日志

--add-module=PATH - Add in a third-party module found in directory PATH

在不同版本间,选项可能会有些许变化,请总是使用 ./configure --help 命令来检查一下当前的选项列表。示例 (最好能在同一行):

./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

Example on Ubuntu/debian with libgcrypt11-dev, libpcre3-dev and libssl-dev installed (choose EITHER --with-md5 OR --with-sha1, but not both; on debian and ubuntu, they should both point to /usr/lib)

./configure --with-openssl=/usr/lib/ssl/ --with-md5=/usr/lib

An Ubuntu Edgy .deb for version 0.5.2 can be found here: nginx_0.5.2-1_i386.deb.

(NOTE: According to an October 2006 message md5 was used in a now broken http cache module and sha1 is used in an incomplete mysql library
module and so are currently not needed.)

【Nginx 概述】

HTTP 基础功能:

处理静态文件,索引文件以及自动索引;

反向代理加速(无缓存),简单的负载均衡和容错;

FastCGI,简单的负载均衡和容错;

模块化的结构。过滤器包括gzipping, byte ranges, chunked responses, www.linuxidc.com以及 SSI-filter 。在 SSI 过滤器中,到同一个 proxy 或者 FastCGI
的多个子请求并发处理;

SSL 和 TLS SNI 支持;

IMAP/POP3 代理服务功能:

使用外部 HTTP 认证服务器重定向用户到 IMAP/POP3 后端;

使用外部 HTTP 认证服务器认证用户后连接重定向到内部的 SMTP 后端;

认证方法:

POP3: POP3 USER/PASS, APOP, AUTH LOGIN PLAIN CRAM-MD5;

IMAP: IMAP LOGIN;

SMTP: AUTH LOGIN PLAIN CRAM-MD5;

SSL 支持;

在 IMAP 和 POP3 模式下的 STARTTLS 和 STLS 支持;

支持的操作系统:

FreeBSD 3.x, 4.x, 5.x, 6.x i386; FreeBSD 5.x, 6.x amd64;

Linux 2.2, 2.4, 2.6 i386; Linux 2.6 amd64;

Solaris 8 i386; Solaris 9 i386 and sun4u; Solaris 10 i386;

MacOS X (10.4) PPC;

结构与扩展:

一个主进程和多个工作进程。工作进程是单线程的,且不需要特殊授权即可运行;

kqueue (FreeBSD 4.1+), epoll (Linux 2.6+), rt signals (Linux 2.2.19+), /dev/poll (Solaris 7 11/99+), select, 以及 poll 支持;

kqueue支持的不同功能包括 EV_CLEAR, EV_DISABLE (临时禁止事件), NOTE_LOWAT, EV_EOF, 有效数据的数目,错误代码;

sendfile (FreeBSD 3.1+), sendfile (Linux 2.2+), sendfile64 (Linux 2.4.21+), 和 sendfilev (Solaris 8 7/01+) 支持;

输入过滤 (FreeBSD 4.1+) 以及 TCP_DEFER_ACCEPT (Linux 2.4+) 支持;

10,000 非活动的 HTTP keep-alive 连接仅需要 2.5M 内存。

最小化的数据拷贝操作;

其他HTTP功能:

基于IP 和名称的虚拟主机服务;

Memcached 的 GET 接口;

支持 keep-alive 和管道连接;

灵活简单的配置;

重新配置和在线升级而无须中断客户的工作进程;

可定制的访问日志,日志写入缓存,以及快捷的日志回卷;

4xx-5xx 错误代码重定向;

基于 PCRE 的 rewrite 重写模块;

基于客户端 IP 地址和 HTTP 基本认证的访问控制;

PUT, DELETE, 和 MKCOL 方法;

支持 FLV (Flash 视频);

带宽限制;

安装之前需要3个支持:gzip,pcre,openssl

【安装 Nginx】

模块依赖性

gzip 模块需要 zlib 库

rewrite 模块需要 pcre 库

ssl 功能需要 openssl 库

预先编译好的安装包

1 gzip支持,需要zlib http://www.zlib.net/ 下载最新版即可

2 rewrite module requires pcre library http://www.pcre.org/ 下载最新版即可

3 ssl 功能需要 openssl 库 http://www.openssl.org/ => http://www.openssl.org/source/LASTEST版本即可

我在安装过程中遇到了两个问题:

1../configure: error: the HTTP cache module requires md5 functions

from OpenSSL library. You can either disable the module by using

--without-http-cache option, or install the OpenSSL library into the system,

or build the OpenSSL library statically from the source with nginx by using

--with-http_ssl_module --with-openssl=<path> options.

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

第一个问题是需要安装openSSl,第二个需要安装pcre

安装过程:

1.安装编译openssl

tar zxvf openssl.tar.gz

cd openssl

./confbigure

make

make install

2.安装编译gzip

tar zxvf gzip.tar.gz

cd gzip

./confbigure

make

make install

3.安装编译pcre

tar zxvf pcre.tar.gz

cd pcre

./confbigure

make

make install

4.安装nginx

tar zxvf nginx.tar.gz

cd nginx

./configure --with-pcre=../pcre --with-zlib=../zlib --with-http_ssl_module --with-openssl=../openssl

make

make install

成功了。。。

nginx目前还不能直接支持php,必须先借助于fastcgi来驱动php。现在fastcgi较好的办法有2种,一个是spawn-fcgi,另外一个就是php-fpm,一般来说可能php-fpm更强大一点.

#定义Nginx运行的用户和用户组

user www www;

(1)#nginx进程数,建议设置为等于CPU总核心数。

worker_processes 8;

(2)#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]

error_log ar/loginx/error.log info;

(3)#进程文件

pid ar/runinx.pid;

(4)#一个nginx进程打开的最多文件描述符数目,

理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并

不均匀,所以建议与ulimit -n的值保持一致。

worker_rlimit_nofile 65535;

(5)#工作模式与连接数上限

events

{

#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。

use epoll;

#单个进程最大连接数(最大连接数=连接数*进程数)

worker_connections 65535;

}

#设定http服务器

http

{

include mime.types; #文件扩展名与文件类型映射表

default_type application/octet-stream; #默认文件类型

#charset utf-8; #默认编码

server_names_hash_bucket_size 128; #服务器名字的hash表大小

client_header_buffer_size 32k; #上传文件大小限制

large_client_header_buffers 4 64k; #设定请求缓

client_max_body_size 8m; #设定请求缓

sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对

于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O

处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。

autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。

tcp_nopush on; #防止网络阻塞

tcp_nodelay on; #防止网络阻塞

keepalive_timeout 120; #长连接超时时间,单位是秒

#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

#gzip模块设置

gzip on; #开启gzip压缩输出

gzip_min_length 1k; #最小压缩文件大小

gzip_buffers 4 16k; #压缩缓冲区

gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)

gzip_comp_level 2; #压缩等级

gzip_types text/plain application/x-javascript text/css application/xml;

#压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。

gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用

upstream blog.ha97.com {

#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。

server 192.168.80.121:80 weight=3;

server 192.168.80.122:80 weight=2;

server 192.168.80.123:80 weight=3;

}

#虚拟主机的配置

server

{

#监听端口

listen 80;

#域名可以有多个,用空格隔开

server_name www.ha97.com ha97.com;

index index.html index.htm index.php;

root /data/www/ha97;

location ~ .*.(php|php5)?$

{

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

#图片缓存时间设置

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 10d;

}

#JS和CSS缓存时间设置

location ~ .*.(js|css)?$

{

expires 1h;

}

#日志格式设定

log_format access '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" $http_x_forwarded_for';

#定义本虚拟主机的访问日志

access_log ar/loginx/ha97access.log access;

#对 "/" 启用反向代理

location / {

proxy_pass http://127.0.0.1:88;
proxy_redirect off;

proxy_set_header X-Real-IP $remote_addr;

#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#以下是一些反向代理的配置,可选。

proxy_set_header Host $host;

client_max_body_size 10m; #允许客户端请求的最大单文件字节数

client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,

proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)

proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)

proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)

proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小

proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置

proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)

proxy_temp_file_write_size 64k;

#设定缓存文件夹大小,大于这个值,将从upstream服务器传

}

#设定查看Nginx状态的地址

location /NginxStatus {

stub_status on;

access_log on;

auth_basic "NginxStatus";

auth_basic_user_file confpasswd;

#htpasswd文件的内容可以用apache提供的htpasswd工具来产生。

}

#本地动静分离反向代理配置

#所有jsp的页面均交由tomcat或resin处理

location ~ .(jsp|jspx|do)?$ {

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_pass http://127.0.0.1:8080;
}

#所有静态文件由nginx直接读取不经过tomcat或resin

location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$

{ expires 15d; }

location ~ .*.(js|css)?$

{ expires 1h; }

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: