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

Linux CentOS 6.x 开发配置文档 - Nginx

2013-08-15 15:54 281 查看
1,安装准备:

a,yum -y install pcre-devel(If appear: ./configure: error: the HTTP rewrite module requires the PCRE library.)

b,yum -y install openssl openssl-devel(If appear: ./configure: error: the HTTP cache module requires md5 functions from OpenSSL library.)

c,yum -y install libssl-dev(If appear: ./configure: error: the HTTP gzip module requires the zlib library.)

d,yum -y install gcc-c++(libtool:
unrecognized option `-DHAVE_CONFIG_H'/compile: Try `libtool --help' for more information.)

2,先安装 pcre

a,下载:wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
b,解压:tar zxvf pcre-8.33.tar.gz

c,进入:cd pcre-8.33

d,配置:./configure

e,编译:make

f,安装:make install

3,安装 Nginx

a,下载:wget http://nginx.org/download/nginx-1.4.1.tar.gz
b,解压:tar zxvf nginx-1.4.1.tar.gz

c,进入:cd nginx-1.4.1

d,配置:./configure

e,编译:make

f,安装:make install

4,启动 Nginx:/usr/local/nginx/sbin/nginx

5,验证 Nginx 是否安装正确:/usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf
syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is
successful

6,关闭 Nginx:/usr/local/nginx/sbin/nginx -s stop

7,将 Nginx 作为系统服务启动:

a,编辑:gedit /etc/rc.local

b,添加:/usr/local/nginx/sbin/nginx

8,整合 Tomcat

a,修改精简版的 nginx.conf

worker_processes 8; #官方的建议是:与CPU的内核数相等,实际情况下开4个或8个就可以了。

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

include gzip.conf;

upstream www.abc.com {

server www.abc.com:8080;

}

server {

listen 80;

server_name localhost;

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 off;

location / {

root html;

index index.html index.htm;

proxy_pass http://www.abc.com;
include proxy.conf;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

b,创建:gzip.conf,内容如下:

gzip on;

gzip_min_length 1000;

gzip_buffers 4 8k;

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

output_buffers 1 32k;

postpone_output 1460;

c,创建: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; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP

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

client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数 可以理解为先保存到本地再传给用户

proxy_connect_timeout 300; #跟后端服务器连接的超时时间_发起握手等候响应超时时间

proxy_send_timeout 300; #后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据

proxy_read_timeout 300; #连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理

proxy_buffer_size 4k; #代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可

proxy_buffers 4 32k; #同上 告诉Nginx保存单个用的几个Buffer 最大用多大空间

proxy_busy_buffers_size 64k; #如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2

proxy_temp_file_write_size 64k; #proxy缓存临时文件的大小

9,Nginx 升级到 Tengine:

(Tengine是由淘宝网发起的Web服务器开源项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。)

1,下载:wget -c http://tengine.taobao.org/download/tengine-1.4.3.tar.gz
2,tar -zxvf tengine-1.4.3.tar.gz

3,cd tengine-1.4.3

4,./configure --prefix=/usr/local/nginx --without-http_autoindex_module --without-http_geo_module --without-http_map_module --without-http_browser_module --with-http_stub_status_module --with-http_realip_module --with-pcre=/root/pcre-8.32(这里先下载并解压pcre)

5,make

6,make install

7,cp -r objs/nginx /usr/local/nginx/sbin/nginx

8,验证一下:/usr/local/nginx/sbin/nginx -t

9,重启服务(最好重启机器):service nginx restart

10,检查版本:/usr/local/nginx/sbin/nginx -V,第一行应该会出现:Tengine version: Tengine/1.4.3 (nginx/1.2.5) ...

10,问题:

1,因频繁重启,自己把自己的端口占用了,会出现:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use),解决方法:
killall -9 nginx #杀掉nginx 进程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: