源码安装nginx
cat /etc/redhat-release
uname -rm
yum install pcre-devel openssl-devel -y
rpm -qa pcre pcre-devel openssl openssl-devel
groupadd -g 888 nginx
useradd nginx -u 888 -g nginx -s /sbin/nologin -M
mkdir -p /server/tools
cd /server/tools
wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
tar xf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3 --with-http_stub_status_module --with-http_ssl_module
make
make install
ln -s /application/nginx-1.6.3 /application/nginx
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx
ps -ef | grep nginx
lsof -i :80
curl 10.0.0.41
单节点池转发
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_server_pools {
server 172.16.1.13:80 weight=1;
server 172.16.1.14:80 weight=1;
}
server {
listen 80;
server_name www.peterwang.cn;
location / {
proxy_pass http://www_server_pools;
include proxy.conf;
}
}
}
vim /application/nginx/conf/proxy.conf
proxy_set_header Host $host; #在请求头中加入host字段信息
proxy_set_header X-Forwarded-For $remote_addr; #在请求头中加入获取的客户端IP字段信息
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
#接收到错误状态码时,会将用户请求转发给正常工作的RS服务器
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
多节点池转发
1.根据URL
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream static_server_pools {
server 10.0.0.7:80 weight=1;
}
upstream upload_server_pools {
server 10.0.0.8:80 weight=1;
}
server {
listen 80;
server_name www.peterwang.org;
location /static/ {
proxy_pass http://static_server_pools;
include proxy.conf;
}
location /upload/ {
proxy_pass http://upload_server_pools;
include proxy.conf;
}
}
}
vim /application/nginx/conf/proxy.conf
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
2.根据用户设备
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream static_server_pools {
server 10.0.0.7:80 weight=1;
}
upstream upload_server_pools {
server 10.0.0.8:80 weight=1;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
if ($http_user_agent ~* "MSIE")
{
proxy_pass http://static_server_pools;
}
if ($http_user_agent ~* "Firefox")
{
proxy_pass http://upload_server_pools;
}
include proxy.conf;
}
}
}
节点监控模块安装
#nginx_upstream_check_module是由淘宝技术团队开发的用于监控集群节点状态的模块。
cd /server/tools
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
unzip master.zip
cd nginx-1.6.3
patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3
--with-http_stub_status_module --with-http_ssl_module --add-module=../nginx_upstream_check_module-master/
make
mv /application/nginx/sbin/nginx{,.ori}
cp ./objs/nginx /application/nginx/sbin/
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -V
cd /application/nginx
vim conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_server_pools {
server 10.0.0.7:80 weight=1;
server 10.0.0.8:80 weight=1;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
root html;
index index.html index.htm;
proxy_pass http://www_server_pools;
include proxy.conf;
}
location /status/ {
check_status;
access_log off;
}
sbin/nginx -t
sbin/nginx -s stop
sbin/nginx