安装nginx
安装nginx版本为0.8.36
一。下载nginx
下载地址:http://www.nginx.org/
选择nginx-0.8.36
将该下载包拷贝到/usr/local/下(随意了,找个地方就好)
二。安装
cd /usr/local/
tar zxvf nginx-0.8.36.tar.gz
cd nginx-0.8.36
按照一些网络资料的介绍,执行如下命令即可完成安装
./configure
make
make install
但在实际安装过程中会,执行./configure时,根据系统的配置不同会有不同的错误提示,这里不罗嗦了,安装nginx需要安装openssl和 pcre,
openssl在linux下svn的安装中有过介绍,这里不再赘述,下面只介绍一下pcre的安装,如下:
下载pcre:http://sourceforge.net/projects/pcre/files/ ,选择pcre-8.02.tar.gz,拷贝到/usr/local/下
tar -zxvf pcre-8.02.tar.gz
cd pcre-8.02
./configure --prefix=/usr/local/pcre
make
make install
ok,pcre安装完成
接着我们安装nginx,
cd /usr/local/nginx-0.8.36
./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre-8.02 --with-http_ssl_module --with-openssl=/usr/local/openssl-0.9.8o
make
make install
ok,nginx安装完成。
三。配置
修改 /usr/local/pcre/conf/nginx.conf 来满足自己的需求,下面给一个负载的小实例
Java代码 复制代码 收藏代码
user nginx;#确保存在这个用户
worker_processes 2;
error_log /var/log/nginx/error.log info;#确保路径存在
pid logs/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 30;
#gzip on;
server_names_hash_bucket_size 128;
upstream tomcats {
server 192.168.0.104:8888 weight=3;
server 192.168.2.94:8888 weight=2;
ip_hash;
}
server {
listen 80;
charset gb2312;
add_header test private;
location / {
root /usr/local/test/boss/test;
index index.html index.htm index.jsp;
proxy_pass http://tomcats;
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 10;
proxy_send_timeout 15;
proxy_read_timeout 15;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
注意,这里nginx监听80端口,所以要在iptables里打开80端口。
启动nqinx:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
接着访问这台机器的80的端口,如果请求成功,则说明配置成功。
为了操作方便,可以自己写一个nginx命令脚本,放到/etc/init.d下,并赋予其执行权限即可,详见附件,执行方法如下:
启动:service nginx start
停止:service nginx stop
重启:service nginx reconfigure
查看状态:service nginx status