http://nginx.org/en/download.html 下载 Stable version 1.10.1
解压 tar -zxvf nginx-1.10.1.tar.gz 这是源文件,配置属性后通过make编译安装
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel 一键安装依赖
cd nginx-1.10.1
./configure --prefix=/usr/local/nginx
make
make install
安装完成之后,检查nginx的配置文件是否正确
cd /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
配置proxy.conf
# proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
配置 nginx.conf
user root; #运行用户
worker_processes 1; #启动进程,通常设置成和cpu的数量相等
#全局错误日志及PID文件
error_log /usr/local/nginx/logs/error.log;
error_log /usr/local/nginx/logs/error.log notice;
error_log /usr/local/nginx/logs/error.log info;
pid /usr/local/nginx/logs/nginx.pid;
# 工作模式及连接数上线
events {
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
worker_connections 1024; #单个后台worker process进程的最大并发链接数
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
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_max_body_size 8m;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩,降低传输流量
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#添加tomcat列表,负载均衡的服务器都放在这
upstream tomcat_pool {
#server tomcat地址:端口号 weight表示权值,权值越大,被分配的几率越大;
server 101.201.233.141:4001 weight=4 max_fails=2 fail_timeout=30s;
server 101.201.233.141:5001 weight=4 max_fails=2 fail_timeout=30s;
}
server {
listen 80; #监听端口
server_name 101.201.233.141; #对外提供服务的网址(域名或者ip)
index index.html;
location = / {
proxy_pass http://tomcat_pool/index.html;
}
#默认请求设置
location / {
proxy_pass http://tomcat_pool;
# index index.jsp index.html index.htm; #设定访问的默认首页
# root /usr/local/tomcat/4001_tomcat/webapps; #站点根目录,此目录下存放我们的web项目
}
#charset koi8-r;
#access_log logs/host.access.log main;
#所有的jsp页面均由tomcat处理
location ~ \.(jsp|jspx|dp)?$
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://tomcat_pool; #转向tomcat处理
}
#所有的静态文件直接读取不经过tomcat,nginx自己处理
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
#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 /usr/local/nginx/logs/ubitechtest.log access;#设定访问日志的存放路径
# redirect server error pages to the static page /50x.html
#
#定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
结果:
http://101.201.233.141/
自动
tomcat——5001
tomcat——4001
切换