一:安装
首先下载:http://nginx.org/
在CentOS系统下用yum安装GCC编译器及相关工具
yum -y install gcc gcc-c++ autoconf automake
yum -y install zlib zlib-dever openssl openssl-devel pcre pcre-devel
通过编译源码的方式来安装
tar -zxvf nginx-x.x.x.tar.gz
cd nginx-x.x.x
./configure
make
sudo make install
二:启动行命令
1.启动
cd usr/local/nginx/sbin
./nginx
2.重启
cd /usr/local/nginx/sbin
./nginx -s reload
3.关闭
查询nginx主进程号
ps -ef | grep nginx
从容停止 kill -QUIT 主进程号
快速停止 kill -TERM 主进程号
强制停止 kill -9 nginx
三:nginx+tomcat部署
在/var/loacl/nginx/conf/nginx.cog中配置
#user www www;
worker_processes 2;
error_log logs/error.log info;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
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 logs/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_body_buffer_size 8m;
server_tokens off;
ignore_invalid_headers on;
recursive_error_pages on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
upstream hostname {
server 121.42.13.51:8080 max_fails=0 weight=1;
}
server {
listen 800;
server_name hostname;
charset utf-8;
access_log logs/121.42.13.51.access.log main;
location / {
proxy_pass http://hostname;
proxy_set_header X-Real-IP $remote_addr;
}
# location /nginxstatus {
# stub_status on;
# access_log on;
# }
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
worker_processes 2;
error_log logs/error.log info;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
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 logs/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_body_buffer_size 8m;
server_tokens off;
ignore_invalid_headers on;
recursive_error_pages on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
upstream hostname {
server 121.42.13.51:8080 max_fails=0 weight=1;
}
server {
listen 800;
server_name hostname;
charset utf-8;
access_log logs/121.42.13.51.access.log main;
location / {
proxy_pass http://hostname;
proxy_set_header X-Real-IP $remote_addr;
}
# location /nginxstatus {
# stub_status on;
# access_log on;
# }
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
四:nginx的upstream目前支持5种方式的分配
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。例如:
upstream bakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
3、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。例如:
upstream bakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。upstream backend {
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}