NGINX 中文站

NGINX安装,最新下载,配置,优化等

  • 首页
  • About NGINX
  • NGINX 最新版本下载
  • 看看都有哪些网站在使用Nginx

存档

作者存档

nginx和squid配合搭建的web服务器前端系统

2012年2月20日 iNginx 没有评论

这个架构是目前我个人觉得比较稳妥并且最方便的架构,易于多数人接受:

前端的lvs和squid,按照安装方法,把epoll打开,配置文件照搬,基本上问题不多。

这个架构和app_squid架构的区别,也是关键点就是:加入了一级中层代理,中层代理的好处实在太多了:

1、gzip压缩

压缩可以通过nginx做,这样,后台应用服务器不管是apache、resin、lighttpd甚至iis或其他古怪服务器,都不用考虑压缩的功能问题。

2、负载均衡和故障屏蔽

nginx可以作为负载均衡代理使用,并有故障屏蔽功能,这样,根据目录甚至一个正则表达式来制定负载均衡策略变成了小case。

3、方便的运维管理,在各种情况下可以灵活制订方案。

例如,如果有人用轻量级的ddos穿透squid进行攻击,可以在中层代理想办法处理掉;访问量和后台负载突变时,可以随时把一个域名或一个目录的请求扔入二级cache服务器;可以很容易地控制no-cache和expires等header。等等功能。。。

4、权限清晰

这台机器就是不写程序的维护人员负责,程序员一般不需要管理这台机器,这样假如出现故障,很容易能找到正确的人。

对于应用服务器和数据库服务器,最好是从维护人员的视线中消失,我的目标是,这些服务只要能跑得起来就可以了,其它的事情全部可以在外部处理掉。

分类: Nginx 成功分享 标签:

Nginx中文手册下载

2012年2月20日 iNginx 没有评论

Nginx 常见应用技术指南[Nginx Tips] 第二版

作者:NetSeek http://www.linuxtone.org (IT运维专家网|集群架构|性能调优)
欢迎转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明.
首发时间: 2008-11-25 更新时间:2009-1-14

目 录

一、 Nginx 基础知识
二、 Nginx 安装及调试
三、 Nginx Rewrite
四、 Nginx Redirect
五、 Nginx 目录自动加斜线:
六、 Nginx Location
七、 Nginx expires
八、 Nginx 防盗链
九、 Nginx 访问控制
十、 Nginx日志处理
十一、 Nginx Cache
十二、 Nginx 负载均衡
十三、 Nginx简单优化
十四、 如何构建高性能的LEMP环境
十五、 Nginx服务监控
十六、 常见问题与错误处理.
十七、 相关资源下载

【前言】:

编写此技术指南在于推广普及NGINX在国内的使用,更方便的帮助大家了解和掌握NGINX
的一些使用技巧。本指南很多技巧来自于网络和工作中或网络上朋友们问我的问题.在此对
网络上愿意分享的朋友们表示感谢和致意!欢迎大家和我一起丰富本技术指南提出更好的建
议!请朋友们关注: http://www.linuxtone.org 技术分享社区! 互想学习共同进步!

一、 Nginx 基础知识

1、简介
Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服
务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超
过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳
定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

更多的请见官方wiki: http://wiki.codemongers.com/

2、 Nginx的优点
nginx做为HTTP服务器,有以下几项基本特性:
1) 处理静态文件,索引文件以及自动索引;打开文件描述符缓冲.
2) 无缓存的反向代理加速,简单的负载均衡和容错.
3) FastCGI,简单的负载均衡和容错.
4) 模块化的结构。包括gzipping, byte ranges, chunked responses, 以及 SSI-filter等filter。
如果由FastCGI或其它代理服务器处理单页中存在的多个SSI,则这项处理可以并行运行,而不
需要相互等待。
5) 支持SSL 和 TLS SNI.
Nginx专为性能优化而开发,性能是其最重要的考量, 实现上非常注重效率 。它支持内核Poll模型,
能经受高负载的考验, 有报告表明能支持高达 50,000 个并发连接数。

Nginx具有很高的稳定性。其它HTTP服务器,当遇到访问的峰值,或者有人恶意发起慢速连接时,
也很可能会导致服务器物理内存耗尽频繁交换,失去响应,只能重启服务器。例如当前apache一旦上到
200个以上进程,web响应速度就明显非常缓慢了。而Nginx采取了分阶段资源分配技术,使得它的CPU与
内存占用率非常低。nginx官方表示保持10,000个没有活动的连接,它只占2.5M内存,所以类似DOS这
样的攻击对nginx来说基本上是毫无用处的。就稳定性而言, nginx比lighthttpd更胜一筹。

Nginx支持热部署。它的启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不
需要重新启动。你还能够在不间断服务的情况下,对软件版本进行进行升级。

Nginx采用master-slave模型, 能够充分利用SMP的优势,且能够减少工作进程在磁盘I/O的阻
塞延迟。当采用select()/poll()调用时,还可以限制每个进程的连接数。

Nginx代码质量非常高,代码很规范, 手法成熟, 模块扩展也很容易。特别值得一提的是强大
的Upstream与Filter链。 Upstream为诸如reverse proxy, 与其他服务器通信模块的编写奠定了很好的
基础。而Filter链最酷的部分就是各个filter不必等待前一个filter执行完毕。它可以把前一个filter
的输出做为当前filter的输入,这有点像Unix的管线。这意味着,一个模块可以开始压缩从后端服务器
发送过来的请求,且可以在模块接收完后端服务器的整个请求之前把压缩流转向客户端。

Nginx采用了一些os提供的最新特性如对sendfile (Linux 2.2+),accept-filter (FreeBSD
4.1+),TCP_DEFER_ACCEPT (Linux 2.4+) 的支持,从而大大提高了性能

二、 Nginx 安装及调试

1、Pcre 安装

./configure
make && make install
cd ../

2. nginx 编译安装

/configure –user=www –group=www –prefix=/usr/local/nginx/ –with-http_stub_status_module
–with-openssl=/usr/local/openssl

make && make install

更详细的模块定制与安装请参照官方wiki.

3、Nginx 配置文件测试:

# /usr/local/nginx/sbin/nginx -t //Debug 配置文件的关键命令需要重点撑握.

2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf
syntax is ok
2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf was
tested successfully

3、 Nginx 启动:

# /usr/local/nginx/sbin/nginx

4、 Nginx 配置文件修改重新加载:

# kill -HUP `cat /usr/local/nginx/logs/nginx.pid `

三、 Nginx Rewrite

1. Nginx Rewrite 基本标记(flags)
last – 基本上都用这个Flag。
※相当于Apache里的[L]标记,表示完成rewrite,不再匹配后面的规则
break – 中止Rewirte,不再继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
※原有的url支持正则 重写的url不支持正则

2. 正则表达式匹配,其中:
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~ 和 !~* 分别为区分大小写不匹配及不区分大小写不匹配

3. 文件及目录匹配,其中:
-f 和 !-f 用来判断是否存在文件
-d 和 !-d 用来判断是否存在目录
-e 和 !-e 用来判断是否存在文件或目录
-x 和 !-x 用来判断文件是否可执行

3. Nginx 的一些可用的全局变量,可用做条件判断:

$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri

四、 Nginx Redirect

将所有linuxtone.org与netseek.linuxtone.org域名全部自跳转到http://www.linuxtone.org

server {
    listen 80;
    server_name linuxtone.org netseek.linuxtone.org;
    index index.html index.php;
    root /data/www/wwwroot;
    if ($host !~ “^www\.linxtone\.org$”) {
        rewrite ^(.*) http://www.linuxtone.org$1 redirect;
    }
    …………………
}

五、 Nginx 目录自动加斜线:

if (-d $request_filename){
    rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}

六、 Nginx Location

1.基本语法:[和上面rewrite正则匹配语法基本一致]
location [=|~|~*|^~] /uri/ { … }
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~ 和 !~* 分别为区分大小写不匹配及不区分大小写不匹配

示例1:

location = / {
    # matches the query / only.
    # 只匹配 / 查询。
}

匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配

示例2:

location ^~ /images/ {
    # matches any query beginning with /images/ and halts searching,
    # so regular expressions will not be checked.
    # 匹配任何以 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
}

示例3:

location ~* \.(gif|jpg|jpeg)$ {
    # matches any request ending in gif, jpg, or jpeg. However, all
    # requests to the /images/ directory will be handled by
    # 匹配任何以 gif、jpg 或 jpeg 结尾的请求。
}

七、 Nginx expires

1.根据文件类型判断,添加expires

# Add expires header for static content
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
    if (-f $request_filename) {
        root /data/www/wwwroot/bbs;
        expires 1d;
        break;
    }
}

2、根据某个目录判断,添加expires

# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
    root /data/www/wwwroot/down;
    expires 30d;
}

八、 Nginx 防盗链

1. 针对不同的文件类型

#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
    valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost baidu.com;
    if ($invalid_referer) {
        rewrite ^/ http://www.linuxtone.org/images/default/logo.gif;
        # return 403;
    }
}

2. 针对不同的目录

location /img/ {
    root /data/www/wwwroot/bbs/img/;
    valid_referers none blocked server_names *.linuxtone.org http://localhost baidu.com;
    if ($invalid_referer) {
        rewrite ^/ http://www.linuxtone.org/images/default/logo.gif;
        #return 403;
    }
}

3. 同实现防盗链和expires的方法

#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
    valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost ;
    if ($invalid_referer) {
        rewrite ^/ http://www.linuxtone.org/images/default/logo.gif;
    }
    access_log off;
    root /data/www/wwwroot/bbs;
    expires 1d;
    break;
}

九、 Nginx 访问控制

1. Nginx 身份证验证

#cd /usr/local/nginx/conf
#mkdir htpasswd
/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji linuxtone

#添加用户名为linuxtone
New password: (此处输入你的密码)
Re-type new password: (再次输入你的密码)
Adding password for user

http://count.linuxtone.org/tongji/data/index.html(目录存在/data/www/wwwroot/tongji/data/目录

下)

将下段配置放到虚拟主机目录,当访问http://count.linuxtone/tongji/即提示要密验证:

location ~ ^/(tongji)/ {
    root /data/www/wwwroot/count;
    auth_basic “LT-COUNT-TongJi”;
    auth_basic_user_file /usr/local/nginx/conf/htpasswd/tongji;
}

2. Nginx 禁止访问某类型的文件.

如,Nginx下禁止访问*.txt文件,配置方法如下.

location ~* \.(txt|doc)$ {
    if (-f $request_filename) {
        root /data/www/wwwroot/linuxtone/test;
        #rewrite …..可以重定向到某个URL
        break;
    }
}

方法2:

location ~* \.(txt|doc)${
    root /data/www/wwwroot/linuxtone/test;
    deny all;
}

实例:
禁止访问某个目录

location ~ ^/(WEB-INF)/ {
    deny all;
}

3. 使用ngx_http_access_module限制ip访问

location / {
    deny 192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    deny all;
}

详细参见wiki: http://wiki.codemongers.com/NginxHttpAccessModule#allow

4. Nginx 下载限制并发和速率

limit_zone linuxtone $binary_remote_addr 10m;
server {
    listen 80;
    server_name down.linuxotne.org;
    index index.html index.htm index.php;
    root /data/www/wwwroot/down;
    #Zone limit
    location / {
        limit_conn linuxtone 1;
        limit_rate 20k;
    }
    ……….
}

只允许客房端一个线程,每个线程20k.

【注】limit_zone linuxtone $binary_remote_addr 10m; 这个可以定义在主的

5. Nginx 实现Apache一样目录列表

location / {
    autoindex on;
}

6. 上文件大小限制
主配置文件里加入如下,具体大小根据你自己的业务做调整。

client_max_body_size 10m;

十、 Nginx 日志处理

1.Nginx 日志切割

#contab -e
59 23 * * * /usr/local/sbin/logcron.sh /dev/null 2>&1

# cat /usr/local/sbin/logcron.sh

#!/bin/bash

log_dir=”/data/logs”

time=`date +%Y%m%d`

/bin/mv ${log_dir}/access_linuxtone.org.log ${log_dir}/access_count.linuxtone.org.$time.logkill -USR1 `cat /var/run/nginx.pid`

更多的日志分析与处理就关注(同时欢迎你参加讨论):http://bbs.linuxtone.org/forum-8-1.html

2.利用AWSTATS分析NGINX日志
设置好Nginx日志格式,仍后利用awstats进行分析.
请参考: http://bbs.linuxtone.org/thread-56-1-1.html

3. Nginx 如何不记录部分日志
日志太多,每天好几个G,少记录一些,下面的配置写到server{}段中就可以了

location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {
    access_log off;
}

十一、Nginx Cache服务配置

如果需要将文件缓存到本地,则需要增加如下几个子参数:

proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path 缓存目录;

其中,
proxy_store on 用来启用缓存到本地的功能,
proxy_temp_path 用来指定缓存在哪个目录下,如:proxy_temp_path html;

在经过上一步配置之后,虽然文件被缓存到了本地磁盘上,但每次请求仍会向远端拉取
文件,为了避免去远端拉取文件,必须修改proxy_pass:

if ( !-e $request_filename) {
    proxy_pass http://mysvr;
}

即改成有条件地去执行proxy_pass,这个条件就是当请求的文件在本地的
proxy_temp_path指定的目录下不存在时,再向后端拉取。

更多更高级的应用可以研究ncache,官方网站: http://code.google.com/p/ncache/

详细安装请参照http://bbs.linuxtone.org 应用加速版ncache相关的贴子.

十二、Nginx 负载均衡

1. Nginx 负载均衡基础知识

nginx的upstream目前支持4种方式的分配

1)、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2)、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

2)、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

3)、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。

4)、url_hash(第三方)

2. Nginx 负载均衡实例1

upstream bbs.linuxtone.org { #定义负载均衡设备的Ip及设备状态
    server 127.0.0.1:9090 down;
     server 127.0.0.1:8080 weight=2;
     server 127.0.0.1:6060;
     server 127.0.0.1:7070 backup;
}

在需要使用负载均衡的server中增加

proxy_pass http://bbs.linuxtone.org/;

每个设备的状态设置为:
a) down 表示单前的server暂时不参与负载
b) weight 默认为1.weight越大,负载的权重就越大。
c) max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
d) fail_timeout:max_fails次失败后,暂停的时间。
e) backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录
location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

3. Nginx 负载均衡实例 2
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓
存时比较有效,也可以用作提高Squid缓存命中率.

简单的负载均等实例:
#vi nginx.conf //nginx主配置文件核心配置
………

#loadblance my.linuxtone.org
upstream my.linuxtone.org {
    ip_hash;
    server 127.0.0.1:8080;
    server 192.168.169.136:8080;
    server 219.101.75.138:8080;
    server 192.168.169.117;
    server 192.168.169.118;
    server 192.168.169.119;
}
………
include vhosts/linuxtone_lb.conf;
………

# vi proxy.conf

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;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_store on;
proxy_store_access user:rw group:rw all:r;
#nginx cache
#client_body_temp_path /data/nginx_cache/client_body 1 2;
proxy_temp_path /data/nginx_cache/proxy_temp 1 2;

#vi linuxtone_lb.conf

server {

    listen 80;
    server_name my.linuxtone.org;
    index index.php;
    root /data/www/wwwroot/mylinuxtone;

    if (-f $request_filename) {
        break;
    }

    if (-f $request_filename/index.php) {
        rewrite (.*) $1/index.php break;
    }

    error_page 403 http://my.linuxtone.org/member.php?m=user&a=login;

    location / {
        if ( !-e $request_filename) {
            proxy_pass http://my.linuxtone.org;
            break;
        }
        include /usr/local/nginx/conf/proxy.conf;
    }
}

十三、Nginx简单优化

1. 减小nginx编译后的文件大小 (Reduce file size of nginx)
默认的nginx编译选项里居然是用debug模式(-g)的(debug模式会插入很多跟踪和
ASSERT之类),编译以后一个nginx有好几兆。去掉nginx的debug模式编译,编译以
后只有几百K

在 auto/cc/gcc,最后几行有:

# debug
CFLAGS=”$CFLAGS -g”

注释掉或删掉这几行,重新编译即可。

2. 修改Nginx的header伪装服务器

1) 修改nginx.h

#vi nginx-0.7.30/src/core/nginx.h
#define NGINX_VERSION “1.8″
#define NGINX_VER “LTWS/” NGINX_VERSION
#define NGINX_VAR “NGINX”
#define NGX_OLDPID_EXT “.oldbin”

2) 修改nginx_http_header_filter_module
#vi nginx-0.7.30/src/http/ngx_http_header_filter_module.c
将如下

static char ngx_http_server_string[] = “Server: nginx” CRLF;

修改为

static char ngx_http_server_string[] = “Server: LTWS” CRLF;

a) 修改nginx_http_header_filter_module
#vi nginx-0.7.30/src/http/ngx_http_special_response.c
将如下:

static u_char ngx_http_error_full_tail[] =
“<hr><center>” NGINX_VER “</center>” CRLF
“</body>” CRLF
“</html>” CRLF
;

static u_char ngx_http_error_tail[] =
“<hr><center>nginx</center>” CRLF
“</body>” CRLF
“</html>” CRLF
;

修改为:

static u_char ngx_http_error_full_tail[] =
“<center> “NGINX_VER” </center>” CRLF
“<hr><center>http://www.linuxtone.org</center>” CRLF
“</body>” CRLF
“</html>” CRLF
;

static u_char ngx_http_error_tail[] =
“<hr><center>LTWS</center>” CRLF
“</body>” CRLF
“</html>” CRLF
;

修改后重新编译一下环境,

404错误的时候显示效果图(如果没有指定错误页的话):

利用curl命令查看服务器header

3. 为特定的CPU指定CPU类型编译优化.

默认nginx使用的GCC编译参数是-O

需要更加优化可以使用以下两个参数

–with-cc-opt=’-O3′ \

–with-cpu-opt=opteron \

使得编译针对特定CPU以及增加GCC的优化.

此方法仅对性能有所改善并不会有很大的性能提升,供朋友们参考.

CPUD类型确定:

# cat /proc/cpuinfo | grep “model name”

编译优化参数参考:http://en.gentoo-wiki.com/wiki/Safe_Cflags

4. Tcmalloc优化Nginx 性能

# wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gz
# tar zxvf libunwind-0.99-alpha.tar.gz
# cd libunwind-0.99-alpha/
# CFLAGS=-fPIC ./configure
# make CFLAGS=-fPIC
# make CFLAGS=-fPIC install
# wget http://google-perftools.googlecode.com/files/google-perftools-0.98.tar.gz
# tar zxvf google-perftools-0.98.tar.gz
# cd google-perftools-0.98/
# ./configure
# make && make install
# echo “/usr/local/lib” > /etc/ld.so.conf.d/usr_local_lib.conf
# ldconfig
# lsof -n | grep tcmalloc

编译nginx 加载google_perftools_module:

./configure –with-google_perftools_module

在主配置文件加入nginx.conf 添加:

google_perftools_profiles /path/to/profile;

5. 内核参数优化
# vi /etc/sysctl.conf

#在末尾增加以下内容:
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000

#使配置立即生效
/sbin/sysctl -p

十四、如何构建高性的LEMP

请参见: http://www.linuxtone.org/lemp/lemp.pdf

1、 提供完整的配置脚本下载:http://www.linuxtone.org/lemp/scripts.tar.gz
2、 提供NGINX常见配置范例含(虚拟主机,防盗链,Rewrite,访问控制,负载均衡Discuz相关程序静态化及等等),你只要稍稍修改即可线上应用。
3、将原版的xcache替换成EA,并提供相关简单调优脚本及配置文件。

更多的及更新资料请关注: http://www.linuxtone.org

十五、Nginx监控

1、 RRDTOOL+Perl脚本画图监控
先安装好rrdtool ,关于rrdtool本文不作介绍,具体安装请参照linuxtone监控版块.

#cd /usr/local/sbnin
#wget http://blog.kovyrin.net/files/mrtg/rrd_nginx.pl.txt
#mv rrd_nginx.pl.txt rrd_nginx.pl
#chmod a+x rrd_nginx.pl

#vi rrd_nginx.pl //配置脚本文件设置好路径

#!/usr/bin/perl
use RRDs;
use LWP::UserAgent;

# define location of rrdtool databases
my $rrd = ‘/data/www/wwwroot/nginx/rrd’;

# define location of images
my $img = ‘/data/www/wwwroot/nginx/html’;

# define your nginx stats URL
my $URL = “http:// 219.32.205.13/nginx_status”;
…………

【注】根据自己具体的状况修改相应的路径.

#crontab –e //加入如下

* * * * * /usr/local/sbin/rrd_nginx.pl

重启crond后,通过配置nginx虚拟主机指到/data/www/wwwroot/nginx/html目录,通过crond
自动执行perl脚本会生成很多图片.

http://xxx/connections-day.png即可看到服务器状态图。

2、 官方Nginx-rrd 监控服务(多虚拟主机)(推荐)
网址:http://www.nginx.eu/nginx-rrd.html

此解决方案其实是基于上述监控方案的一个改进和增强,同样先安装好rrdtool这个画图工
具和相应的perl模块再做如下操作:

# yum install perl-HTML*

先建立好生成的库存和图片存放录

#mkdir -p /data/www/wwwroot/nginx/{rrd,html}
#cd /usr/local/sbin
#wget http://www.nginx.eu/nginx-rrd/nginx-rrd-0.1.4.tgz
#tar zxvf nginx-rrd-0.1.4.tgz
#cd nginx-rrd-0.1.4
#cd etc/
#cp nginx-rrd.conf /etc
#cd etc/cron.d
#cp nginx-rrd.cron /etc/cron.d
#cd /usr/local/src/nginx-rrd-0.1.4/html
# cp index.php /data/www/wwwroot/nginx/html/
#cd /usr/local/src/nginx-rrd-0.1.4/usr/sbin
#cp * /usr/sbin/

#vi /etc/nginx-rrd.conf

#####################################################
#
# dir where rrd databases are stored

RRD_DIR=”/data/www/wwwroot/nginx/rrd”;

# dir where png images are presented

WWW_DIR=”/data/www/wwwroot/nginx/html”;

# process nice level

NICE_LEVEL=”-19″;

# bin dir

BIN_DIR=”/usr/sbin”;

# servers to test

# server_utl;server_name

SERVERS_URL=”http://219.32.205.13/nginx_status;219.32.205.13

http://www.linuxtone.org/nginx_status;www.linuxtone.org”" //根据你的具体情况做调整.

SEVERS_URL 格式 http://domain1/nginx_status;domain1 http://domain2/nginx_status;domain2

这种格式监控多虚拟主机连接状态:
重点启crond服务,仍后通过http://219.32.205.13/nginx/html/ 即可访问。配置过程很简单!

3、 CACTI模板监控Nginx

利用Nginx_status状态来画图实现CACTI监控
nginx编译时允许http_stub_status_module

# vi /usr/local/nginx/conf/nginx.conf

location /nginx_status {
    stub_status on;
    access_log off;
    allow 192.168.1.37;
    deny all;
}# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
# wget http://forums.cacti.net/download.php?id=12676
# tar xvfz cacti-nginx.tar.gz
# cp cacti-nginx/get_nginx_socket_status.pl /data/cacti/scripts/
# cp cacti-nginx/get_nginx_clients_status.pl /data/cacti/scripts/
# chmod 755 /data/cacti/scripts/get_nginx*

检测插件
# /data/cacti/scripts/get_nginx_clients_status.pl http://192.168.1.37/nginx_status

在cacti管理面板导入

cacti_graph_template_nginx_clients_stat.xml
cacti_graph_template_nginx_sockets_stat.xml

十六、常见问题与错误处理

1、 400 bad request错误的原因和解决办法
配置nginx.conf相关设置如下.

client_header_buffer_size 16k;
large_client_header_buffers 4 64k;

根据具体情况调整,一般适当调整值就可以。

2、 Nginx 502 Bad Gateway错误

proxy_next_upstream error timeout invalid_header http_500 http_503;

或者尝试设置:

large_client_header_buffers 4 32k;

3、 Nginx出现的413 Request Entity Too Large错误

这个错误一般在上传文件的时候会出现,

编辑Nginx主配置文件Nginx.conf,找到http{}段,添加

client_max_body_size 10m; //设置多大根据自己的需求作调整.

如果运行php的话这个大小client_max_body_size要和php.ini中的如下值的最大值
一致或者稍大,这样就不会因为提交数据大小不一致出现的错误。

post_max_size = 10M
upload_max_filesize = 2M

4、 解决504 Gateway Time-out(nginx)

遇到这个问题是在升级discuz论坛的时候遇到的

一般看来, 这种情况可能是由于nginx默认的fastcgi进程响应的缓冲区太小造成的,
这将导致fastcgi进程被挂起, 如果你的fastcgi服务对这个挂起处理的不好, 那么最后就
极有可能导致504 Gateway Time-out

现在的网站, 尤其某些论坛有大量的回复和很多内容的, 一个页面甚至有几百K。

默认的fastcgi进程响应的缓冲区是8K, 我们可以设置大点

在nginx.conf里, 加入:

fastcgi_buffers 8 128k

这表示设置fastcgi缓冲区为8×128k

当然如果您在进行某一项即时的操作, 可能需要nginx的超时参数调大点,例如设置成60秒:

send_timeout 60;

只是调整了这两个参数, 结果就是没有再显示那个超时, 可以说效果不错, 但是也
可能是由于其他的原因, 目前关于nginx的资料不是很多, 很多事情都需要长期的经验
累计才有结果, 期待您的发现哈!

5、 如何使用Nginx Proxy

朋友一台服务器运行tomcat 为8080端口,IP:192.168.1.2:8080,另一台机器
IP:192.168.1.8. 朋友想通过访问http://192.168.1.8即可访问tomcat服务.配置如下:

在192.168.1.8的nginx.conf上配置如下:

server {
    listen 80;
    server_name java.linuxtone.org
    location / {
        proxy_pass http://192.168.1.2:8080;
        include /usr/local/nginx/conf/proxy.conf;
    }
}

6、 如何关闭Nginx的LOG

access_log /dev/null;
error_log /dev/null;

十七、相关资源下载

1. nginx配置示例及脚本下载:
# wget http://www.linuxtone.org/lemp/scripts.tar.gz #此脚本范例定期更新.

【附】:
本文档定期更新,同时欢迎朋友多提宝贵意见,丰富nginx tips内容.
最新的文档发布.请朋友们关注: http://bbs.linuxtone.org

分类: Nginx 最新下载 标签:

Nginx+tomcat 做负载均衡

2011年9月2日 iNginx 没有评论

 

一、

1、将tomcat 的server.xml文件中所有端口号都改为不同。
2、Nginx 的nginx.conf文件中
    http {}增加如下内容

     upstream sp.imichat.com{ 
       server 127.0.0.1:8080 weight=2; 
       server 127.0.0.1:8088 weight=2; 
       ip_hash;
     }

server {} 修改信息:


listen 80;
server_name sp.imichat.com;

#charset koi8-r;

#access_log logs/host_access_log main;

        location /{
          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_pass http://sp.imichat.com;
        }

例:
#user  nobody;
worker_processes  1;

events {
    worker_connections  51024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

     upstream localhost { 
       server 127.0.0.1:8080 weight=2; 
       server 127.0.0.1:8088 weight=2; 
       ip_hash;
     }

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location /{
                        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_pass http://localhost;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }}


二、

参数描述

检测nginx配置文件是否正确
/usr/local/nginx/sbin/nginx -t -c nginx.conf
-c 配置文件路径

-g Set global directives. (version >=0.7.4)

-t 检测文件是否正确不执行

-v Print version.

-V Print nginx version, compiler version and configure parameters.

编译时如果使用了–with-debug编译,还可以使用error_log file [ debug_core| debug_http | debug_event …] 来获得debug信息

通过信号对 Nginx 进行控制

Nginx 支持下表中的信号:

信号名 作用描述 
TERM, INT 快速关闭程序,中止当前正在处理的请求 
QUIT 处理完当前请求后,关闭程序 
HUP 重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求 
USR1 重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件 
USR2 平滑升级可执行程序 
WINCH 从容关闭工作进程

有两种方式来通过这些信号去控制 Nginx,第一是通过 logs 目录下的 nginx.pid 查看当前运行的 Nginx 的进程 ID,通过 kill – XXX <pid> 来控制 Nginx,其中 XXX 就是上表中列出的信号名。如果您的系统中只有一个 Nginx 进程,那您也可以通过 killall 命令来完成,例如运行 killall – s HUP nginx 来让 Nginx 重新加载配置。

配置:

use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];FreeBSD使用kqueue,Linux选epoll.
worker_connections number    每个worker的最大连接数
Maxclient = work_processes * worker_connections

nginx的upstream目前支持4种方式的分配

1、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2、weight

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

2、ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

3、fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

4、url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

代理
只需要在nginx的配置文件中增加虚拟主机,然后加入
\proxy_pass http://localhost:8000;

负载均衡:
只需要在http中增加
upstream tgcluster {#定义负载均衡设备的Ip及设备状态
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用负载均衡的server中增加
proxy_pass http://tgcluster/;

每个设备的状态设置为:
1.down 表示单前的server暂时不参与负载
2.weight 默认为1.weight越大,负载的权重就越大。
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录

location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

FASTCGI配置:

请将以下内容保存为fastcgi_params文件,保存于/usr/local/nginx/conf下(Ubuntu可保存于/etc/nginx下),他为我们的FastCGI模块设置了基本的环境变量:

#fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE    nginx;
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_param SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param REQUEST_URI        $request_uri;
fastcgi_param DOCUMENT_URI       $document_uri;
fastcgi_param DOCUMENT_ROOT      $document_root;
fastcgi_param SERVER_PROTOCOL    $server_protocol;
fastcgi_param REMOTE_ADDR        $remote_addr;
fastcgi_param REMOTE_PORT        $remote_port;
fastcgi_param SERVER_ADDR        $server_addr;
fastcgi_param SERVER_PORT        $server_port;
fastcgi_param SERVER_NAME        $server_name;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS    200;请特别注意加粗的一行,PHP-CGI特别需要此行信息来确定PHP文件的位置。

另外需要在PHP-CGI的配置文件(Ubuntu 上此配置文件位于/etc/php5/cgi/php.ini)中,打开cgi.fix_pathinfo选项:

cgi.fix_pathinfo=1;这样php-cgi方能正常使用SCRIPT_FILENAME这个变量。

接下来在nginx的配置中针对php文件配置其利用FastCGI进程来执行:

server {
index index.php;
root /usr/local/nginx/html;

    location ~ .*.php$ {
include /usr/local/nginx/conf/fastcgi_params; #请根据自己保存的路径进行设置
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000; #请根据自己的FastCGI绑定的地址和端口进行配置
}
}通知Nginx重新载入配置:

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`Ubuntu用户可以使用init脚本:sudo /etc/init.d/nginx reload

然后启动php-cgi -b 127.0.0.1:9000

如果出现No input file specified表示SCRIPT_FILENAME设置的有问题。
使用lighttpd的 spawn-fcgi

get http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2 #获取Lighttpd的源码包
tar -xvjf lighttpd-1.4.18.tar.bz2
cd lighttpd-1.4.18
./configure #编译
make
cp src/spawn-fcgi /usr/local/bin/spawn-fcgi #取出spawn-fcgi的程序下面我们就可以使用 spawn-fcgi 来控制php-cgi的FastCGI进程了

/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi参数含义如下

-f <fcgiapp> 指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置 
-a <addr> 绑定到地址addr 
-p <port> 绑定到端口port 
-s <path> 绑定到unix socket的路径path 
-C <childs> 指定产生的FastCGI的进程数,默认为5(仅用于PHP) 
-P <path> 指定产生的进程的PID文件路径 
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等

#运行用户
user   nobody nobody;
#启动进程
worker_processes   2;
#全局错误日志及PID文件
error_log   logs/error.log notice;
pid        logs/nginx.pid;
#工作模式及连接数上限
events {
use epoll;
worker_connections    1024;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型
include    conf/mime.types;
default_type   application/octet-stream;
#设定日志格式
log_format main        '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers   4 4k;
#开启gzip模块
gzip on;
gzip_min_length   1100;
gzip_buffers 4 8k;
gzip_types    text/plain;
output_buffers   1 32k;
postpone_output   1460;
#设定access log
access_log   logs/access.log   main;
client_header_timeout   3m;
client_body_timeout 3m;
send_timeout       3m;
sendfile             on;
tcp_nopush              on;
tcp_nodelay          on;
keepalive_timeout   65;
#设定负载均衡的服务器列表
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
#本机上的Squid开启3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80   weight=1;
server 192.168.8.3:80   weight=6;
}
#设定虚拟主机
server {
listen       80;
server_name 192.168.8.1 www.yejr.com;
charset gb2312;
#设定本虚拟主机的访问日志
access_log   logs/www.yejr.com.access.log   main;
#如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid
#如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好
location ~ ^/(img|js|css)/   {
root /data3/Html;
expires 24h;
}

分类: Nginx 安装使用 标签: 负载均衡

nginx编译参数选项详解

2011年9月2日 iNginx 没有评论

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-dev

–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 命令来检查一下当前的选项列表

示例 1

Tips: 这只是一行!

Shell代码 复制代码 收藏代码
  • ./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   
  • ./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

    示例 2

    Debian Lenny 默认设置

    Shell代码 复制代码 收藏代码
  • ./configure \   
  •   –conf-path=/etc/nginx/nginx.conf \   
  •   –error-log-path=/var/log/nginx/error.log \   
  •   –pid-path=/var/run/nginx.pid \   
  •   –lock-path=/var/lock/nginx.lock \   
  •   –http-log-path=/var/log/nginx/access.log \   
  •   –with-http_dav_module \   
  •   –http-client-body-temp-path=/var/lib/nginx/body \   
  •   –with-http_ssl_module \   
  •   –http-proxy-temp-path=/var/lib/nginx/proxy \   
  •   –with-http_stub_status_module \   
  •   –http-fastcgi-temp-path=/var/lib/nginx/fastcgi \   
  •   –with-debug \   
  •   –with-http_flv_module   
  • ./configure \
      --conf-path=/etc/nginx/nginx.conf \
      --error-log-path=/var/log/nginx/error.log \
      --pid-path=/var/run/nginx.pid \
      --lock-path=/var/lock/nginx.lock \
      --http-log-path=/var/log/nginx/access.log \
      --with-http_dav_module \
      --http-client-body-temp-path=/var/lib/nginx/body \
      --with-http_ssl_module \
      --http-proxy-temp-path=/var/lib/nginx/proxy \
      --with-http_stub_status_module \
      --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
      --with-debug \
      --with-http_flv_module

    示例 3

    Shell代码 复制代码 收藏代码
  • ./configure \   
  •   –prefix=/usr \   
  •   –conf-path=/etc/nginx/nginx.conf \   
  •   –http-log-path=/var/log/nginx/access_log \   
  •   –error-log-path=/var/log/nginx/error_log \   
  •   –pid-path=/var/run/nginx.pid \   
  •   –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 \   
  •   –with-md5-asm –with-md5=/usr/include \   
  •   –with-sha1-asm \   
  •   –with-sha1=/usr/include \   
  •   –with-http_realip_module \   
  •   –with-http_ssl_module \   
  •   –with-http_perl_module \   
  •   –with-http_stub_status_module  
  • ./configure \
      --prefix=/usr \
      --conf-path=/etc/nginx/nginx.conf \
      --http-log-path=/var/log/nginx/access_log \
      --error-log-path=/var/log/nginx/error_log \
      --pid-path=/var/run/nginx.pid \
      --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 \
      --with-md5-asm --with-md5=/usr/include \
      --with-sha1-asm \
      --with-sha1=/usr/include \
      --with-http_realip_module \
      --with-http_ssl_module \
      --with-http_perl_module \
      --with-http_stub_status_module

     

    示例 4

    在装有 libgcrypt11-dev, libpcre3-dev and libssl-dev 的 Ubuntu/debian 上 (选择 –with-md5 或者 –with-sha1, 不要同时选择两个; 在 debian 和 ubuntu 上, 通常被指定到 /usr/lib)

    Shell代码 复制代码 收藏代码
  • ./configure –with-openssl=/usr/lib/ssl/ –with-md5=/usr/lib   
  • ./configure --with-openssl=/usr/lib/ssl/ --with-md5=/usr/lib 

    示例 5

    用于 RedHat 系发行版 (RHEL, CentOS, Fedora). 本维基站点的服务器就是以此选项编译.

    首先安装需求包:

    Shell代码 复制代码 收藏代码
  • yum install gcc openssl-devel pcre-devel zlib-devel  
  • yum install gcc openssl-devel pcre-devel zlib-devel

    配置:

    Shell代码 复制代码 收藏代码
  • ./configure    
  •   –prefix=/usr \   
  •   –sbin-path=/usr/sbin/nginx \   
  •   –conf-path=/etc/nginx/nginx.conf \   
  •   –error-log-path=/var/log/nginx/error.log \   
  •   –pid-path=/var/run/nginx/nginx.pid  \   
  •   –lock-path=/var/lock/nginx.lock \   
  •   –user=nginx \   
  •   –group=nginx \   
  •   –with-http_ssl_module \   
  •   –with-http_flv_module \   
  •   –with-http_gzip_static_module \   
  •   –http-log-path=/var/log/nginx/access.log \   
  •   –http-client-body-temp-path=/var/tmp/nginx/client/ \   
  •   –http-proxy-temp-path=/var/tmp/nginx/proxy/ \   
  •   –http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   
  • ./configure
      --prefix=/usr \
      --sbin-path=/usr/sbin/nginx \
      --conf-path=/etc/nginx/nginx.conf \
      --error-log-path=/var/log/nginx/error.log \
      --pid-path=/var/run/nginx/nginx.pid  \
      --lock-path=/var/lock/nginx.lock \
      --user=nginx \
      --group=nginx \
      --with-http_ssl_module \
      --with-http_flv_module \
      --with-http_gzip_static_module \
      --http-log-path=/var/log/nginx/access.log \
      --http-client-body-temp-path=/var/tmp/nginx/client/ \
      --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
      --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/

      

    最后编译并安装

    make && make install
    分类: Nginx 安装使用 标签:

    nginx 在 centos 环境下安装

    2011年5月12日 iNginx 没有评论
    yum install mysql mysql-server php php-mysql php-gd
    cp /var/lib/mysql /www/mysql
    mysql_install_db 
    service mysql start
    安装nginx
    安装 nginx 出错 缺少 PCRE library 库
    安装 openssl-devel
    ./configure –prefix=/www/nginx –prefix=/www/nginx
    /usr/sbin/groupadd www
    /usr/sbin/useradd -g www www
    sudo chown www:www -R /www
    sudo ./nginx
    killall – s HUP nginx
    nginx 安装成功!
    ~/spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi

     

    分类: Nginx 安装使用 标签:
    Page 1 of 9123456789»
    订阅
    • Google
    • 有道
    • 鲜果
    • 抓虾
    • My Yahoo!
    • newsgator
    • Bloglines
    • 哪吒

    最新文章

    • nginx和squid配合搭建的web服务器前端系统
    • Nginx中文手册下载
    • Nginx+tomcat 做负载均衡
    • nginx编译参数选项详解
    • nginx 在 centos 环境下安装
    • nginx轻松搭建自己的flv流媒体服务器
    • DirectAdmin安装Nginx方法
    • Nginx 最新版本 0.8.38 发布
    • Nginx重大漏洞,文件类型错误解析
    • Nginx 目录访问末尾自动加斜线 /

    标签云

    上传 下载 中文URL 升级 密码 新闻 漏洞 独立IP 目录浏览 缓存 编码 脚本 虚拟主机 认证 负载均衡 配置文件 重定向 错误 防盗链 限制 301 404 502 apache cache conf DirectAdmin discuz fastcgi ip Mysql Nginx nginx电子书 nginx log PHP rewrite supesite URL vps WEB服务器 windows wordpress

    分类

    • Nginx 优化技巧
    • Nginx 安装使用
    • Nginx 成功分享
    • Nginx 最新下载
    • Nginx 疑难解决

    链接

    • Nginx 官方网站
    • UnixBeta.com
    • 手机图片
    • 神印王座

    存档

    • 2012年二月
    • 2011年九月
    • 2011年五月
    • 2010年九月
    • 2010年五月
    • 2010年四月
    • 2010年三月
    • 2010年二月

    Meta

    • 注册
    • 登录
    回到顶部
    版权所有 © 2010 NGINX 中文站
    Powered by wordpress. Theme by NeoEase . 通过 XHTML 1.1 和 CSS 3 验证.