文档库 最新最全的文档下载
当前位置:文档库 › nginx 反向代理+Tomcat集群

nginx 反向代理+Tomcat集群

nginx 安装+反向代理设置

前序:请耐性子看完,前面的这个配置可以达到按后缀名进行缓存,但无法被purge。后面的配置可以被purge。

安装 nginx

依赖的软件包

1. gzip module requires zlib library
2. rewrite module requires pcre library
3. ssl support requires openssl library

4.nginx

下载源码包

1. wget https://www.wendangku.net/doc/7015292528.html,/source/openssl-1.0.0l.tar.gz
2. wget ftp://https://www.wendangku.net/doc/7015292528.html,/pub/software/programming/pcre/pcre-8.35.tar.bz2
3. wget https://www.wendangku.net/doc/7015292528.html,/zlib-1.2.8.tar.gz
4. wget https://www.wendangku.net/doc/7015292528.html,/download/nginx-1.6.0.tar.gz
5. wget https://www.wendangku.net/doc/7015292528.html,/files/ngx_cache_purge-2.0.tar.gz

解压

cd nginx-0.8.30

./configure –prefix=/local/nginx --add-module=/local/ngx_cache_purge --with-openssl=/local/openssl --with-pcre=/local/pcre --with-zlib=/local/zlib --with-http_stub_status_module --with-http_ssl_module
make
make install

nginx.conf

user nobody;
worker_processes 1;

pid logs/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_method $scheme://$host$request_uri $server_protocol" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';

server_names_hash_bucket_size 128; #指定服务器名称哈希表的框大小
client_header_buffer_size 32k;

large_client_header_buffers 4 128k; #以上两个是设定客户端请求的Header头缓冲区大小,对于cookie内容较大的请求,应增大改值。(400或414错误)
client_max_body_size 8m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 32k; #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户
proxy_connect_timeout 600; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 600; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_send_timeout 600; #后端服务器数据回传时间(代理发送超时)
proxy_buffer_size 32k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 1024m; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 #递请求,而不缓冲到磁盘
proxy_ignore_client_abort on; #不允许代理端主动关闭连接


sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_proxied a

ny; #前端是squid的情况下要加此参数,否则squid上不缓存gzip文件
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;


server_tokens off;

proxy_temp_path /cache/proxy_temp_path;
proxy_cache_path /cache/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
##注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区

upstream my_server_pool {
ip_hash;
server 10.0.0.51:8080;
server 10.0.0.51:8080;
}

server {
listen 80 default;
server_name _;
return 500;
access_log off;
}


server {
listen 80;
server_name https://www.wendangku.net/doc/7015292528.html, https://www.wendangku.net/doc/7015292528.html, https://www.wendangku.net/doc/7015292528.html, https://www.wendangku.net/doc/7015292528.html,;
access_log logs/access.log;

location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server_pool;
expires 12h;
}

#扩展名以.gif、.jpg、.css等结尾的静态文件缓存。
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
proxy_next_upstream http_502 http_504 error timeout invalid_header;#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_cache cache_one; #进行缓存,使用Web缓存区cache_one
proxy_cache_valid 200 304 12h; #对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;
proxy_cache_key $host$uri$is_args$args; #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Accept-Encoding "none"; #设定proxy_set_header Accept-Encoding ''; (或是后台服务器关闭gzip),这样这台机器才不会缓存被压缩的文件,造成乱码
#proxy_set_header Accept-Encoding ""; 这个也可
proxy_ignore_headers "Cache-Control" "Expires"; #这段配置加上后,proxy_cache就能支持后台设定的expires。

proxy_pass http://my_server_pool;

expires 1h;
}

#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
location ~ .*\.(php|jsp|cgi)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server_pool;
}


location ~ ^/NginxStatus {
stub_status on;
access_log off;
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}

}

location ~ ^/(WEB-INF)/ {
deny all;
}

#设置只允许指定的IP或IP段才可以清除URL缓存。
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.0.0/16;
allow all;
proxy_cache_purge cache_one $host$1$is_args$args;
}

error_p

age 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

测试结果正常,第一次访问时,nginx和后端反向squid都有请求日志。当请求过一次后,nginx的/cache目录下多出缓存文件,并且再次请求页面(请过浏览器缓存),后端squid没有请求日志,说明是nginx提供的cache。

记录一下:
问题一:开始的实验环境是nginx自己处理静态文件,及:
upstream my_server_pool {
ip_hash;
server 10.0.0.41:80;
}
但是始终都缓存不上。
改成nginx作为负载均衡,反向代理时:
upstream my_server_pool {
ip_hash;
server 10.0.0.51:8080;
server 10.0.0.51:8080;
}
发现可以缓存上了。
[root@test1 data0]# ll *
proxy_cache_path:
total 4
drwx------ 3 nobody nobody 4096 Feb 2 14:09 3
是我的操作失误?还是nginx作为web服务器时,不能缓存自己?

问题二:
比如缓存了https://www.wendangku.net/doc/7015292528.html,/css.css
但是使用https://www.wendangku.net/doc/7015292528.html,/purge/css.css却由后端tomcat返回了404页面。
因为nginx提供的过期控制是针对http_status_code的,我本想通过location中限定类型的方法完成曲线救国,结果发现:一旦location中限定了文件类型,缓存过期的定义就失效!也就是说,限定文件类型后的哈希缓存,是绝绝对对的强制永久缓存——不单过期失效,下面的purge也失效——或许换一个场景,这个刚好有用。
所以换了一个配置:
server {
listen 80;
server_name https://www.wendangku.net/doc/7015292528.html, https://www.wendangku.net/doc/7015292528.html, https://www.wendangku.net/doc/7015292528.html, https://www.wendangku.net/doc/7015292528.html,;
access_log logs/access.log;

location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Accept-Encoding "";
proxy_pass http://my_server_pool;

proxy_headers_hash_max_size 1024;
proxy_headers_hash_bucket_size 128;
expires 12h;
}


# location ~ .*\.(html|gif|jpg|jpeg|png|bmp|swf|js|css)$
# {
# proxy_cache cache_one;
# proxy_cache_valid 200 304 12h;
# proxy_cache_valid 301 302 1m;
# proxy_cache_valid any 1m;
# proxy_cache_key $host$uri$is_args$args;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_set_header Accept-Encoding "";
# proxy_pass http://my_server_pool;
# expires 1h;
# }

#这部分定义不缓存而是透传的请求类型。介于无法通过类型来控制缓存,那么这里不缓存的控制就必须确保严格正确了
location ~ .*\.(php|jsp|cgi)?$ {
proxy_set_header Host $host;
proxy_set

_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server_pool;
}
这样一来的意思就是说缓存所有,除了我定义的php,jsp,cgi,当然能不能被缓存还要决定web服务器的header头了,至此。终于ok




在conf文件夹中新建proxy.conf,用于配置一些代理参数,内容如下:

#!nginx (-)
# proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #获取真实ip
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #获取代理者的真实ip
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;


tomcat其他配置
location ~ .*.jsp$ #所有jsp的页面均交由tomcat处理
{
index index.jsp;
proxy_pass http://localhost:8080;#转向tomcat处理
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #设定访问静态文件直接读取不经过tomcat
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}




管理nginx
#/usr/local/nginx/sbin/nginx -t
#/usr/local/nginx/sbin/nginx
#ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
#/usr/local/nginx/sbin/nginx -s stop
#kill -HUP pid
#kill -HUP `cat /local/nginx/logs/nginx.pid`

相关文档
相关文档 最新文档