您的位置:首页 > 运维架构 > 反向代理

Nginx+Tomcat实现反向代理、页面缓存、动静分离

2014-05-06 17:23 806 查看
http://www.2cto.com/os/201301/185533.html Nginx+Tomcat实现反向代理与动静分离

Nginx+Tomcat实现反向代理、页面缓存、动静分离nginx.conf示例:
user  apache apache;
worker_processes  8;
error_log /usr/local/nginx/logs/error.log crit;
pid        /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile  65535;
events {
use epoll;
worker_connections  1024;
}
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"'
'"$upstream_cache_status"';
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 128k;
client_max_body_size 8m;
client_body_buffer_size 32k;
proxy_ignore_client_abort on;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 32k;
proxy_buffers  4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 1024m;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
server_tokens off;
sendfile        on;
tcp_nopush     on;
keepalive_timeout  60;
tcp_nodelay on;
#gzip  on;
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;
#proxy_cache
proxy_cache_path  /usr/local/nginx/conf/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
server {
listen       8000;
server_name  localhost;
location / {
proxy_pass    http://hk.lotto888.mn:8000; #add_header  Nginx-Cache "$upstream_cache_status";
#proxy_set_header http://hk.lotto888.mn:8000; #proxy_redirect default ;
}
location ~.*\.(gif|jpg|jpeg|png|bmp|sqf|js|css)$
{
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Accept-Encoding "none";
proxy_ignore_headers "Cache-Control" "Expires";
add_header  Nginx-Cache "$upstream_cache_status";
if ( !-e  $request_filename )
{
proxy_pass  http://hk.lotto888.mn:8000; }
expires 1h;
}
location ~.*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://hk.lotto888.mn:8000; }
location ~ .*\.(js|css)
{
expires  1h;
}
location ~/purge(/.*)
{
allow   127.0.0.1;
allow  192.168.1.0/24;
deny    all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
}
access_log /usr/local/nginx/logs/wenquan.log main;
}
注:location ~ .*\.(gif|jpg|jpeg|png|bmp|sqf|js)$ 不加css访问192.168.1.123:8000页面会显示不全

nginx编译参数
./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre=/usr/local/pcre/ --add-module=/soft/ngx_cache_purge-2.1

查看缓存命中率
cat access.log |grep "HIT"|wc -l
awk '{if($NF=="\"-\"\"HIT\"")hit++}END{printf "%.2f%",hit/NR}' access.log

――――――――――――――――――

location基本语法

location [=|~|~*|^~] /uri/ { … }
= 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配
^~ 如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式。

例:
location = / { # 只匹配 / 查询。 location / { # 匹配任何查询,因为所有请求都已 / 开头。但正则表达式规则和长的块规则将被优先和查询匹配。
location ^~ /images/ { # 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
location ~* \.(gif|jpg|jpeg)$ { # 匹配任何已 gif、jpg 或 jpeg 结尾的请求。

* -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行

location可以使用前缀字符串或者正则表达式定义路径。使用正则表达式需要在路径开始添加“~*”前缀 (不区分大小写),或者“~”前缀(区分大小写)。为了根据请求URI查找路径,nginx先检查前缀字符串定义的路径 (前缀路径),在这些路径中找到能最精确匹配请求URI的路径。然后nginx按在配置文件中的出现顺序检查正则表达式路径, 匹配上某个路径后即停止匹配并使用该路径的配置,否则使用最大前缀匹配的路径的配置
让我们用一个例子解释上面的说法:

location = / {
[ configuration A ]
}

location / {
[ configuration B ]
}

location /documents/ {
[ configuration C ]
}

location ^~ /images/ {
[ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
请求“/”匹配配置A, 请求“/index.html”匹配配置B, 请求“/documents/document.html”匹配配置C, 请求“/images/1.gif”匹配配置D, 请求“/documents/1.jpg”匹配配置E。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: