您的位置:首页 > 运维架构 > Nginx

nginx配置ajp

2015-11-08 22:08 591 查看
很久之前就听说nginx很强悍,不过一直没有动力尝试。

自己做点小项目,决定用nginx了。

对于一个用java,在tomcat里面跑应用的人,整合nginx和tomcat就势在必行了。

根据之前整合apache和tomcat的经验,有一个ajp的模块用于web服务器和tomcat之间连接。

但是看了好多nginx的帖子,都在讲proxy。

proxy和ajp之间还是有很大区别的。ajp有些优势是proxy的方式做不到的。

1.ajp和tomcat之间通讯给予长链接,能够减少频繁请求的消耗。

2.ajp协议比http协议报文更小,请求和应答效率更高。

3.ajp协议将http协议的解析过程放在web服务器中进行。对于parameter、attribute还有http头,其他一些内容的解析都是由apache或nginx这样的web服务器完成的。这样,很多解析字符串的操作就不用java进行了。即便不看java内存情况还有cpu调用情况,你也能知道,java解析个报文会浪费多少空间。所以,这个过程放在web服务器中,用c语言这样的代码处理报文的基本解析,效率是非常高的。

以上三点,是代理方式实现不了的。所以还是选择ajp比较好。

然后发现nginx的ajp竟然是国人贡献的,感谢一下姚伟斌。

github在:

https://github.com/yaoweibin/nginx_ajp_module

最初我用了yum安装了一个nginx,这样是没有ajp模块的。所以需要重新编译。

编译过程在:

http://jingyan.baidu.com/article/4b52d70294e0b1fc5d774b7c.html

一下内容来自上面连接,做个备份:

1.准备编译工具

yum -y install gcc automake autoconf libtool make gcc gcc-c++

2.下载资源

mkdir -p /usr/src/nginx

cd /usr/src/nginx

wget http://zlib.net/zlib-1.2.8.tar.gz

wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz

wget http://nginx.org/download/nginx-1.6.2.tar.gz

wget https://codeload.github.com/yaoweibin/nginx_ajp_module/zip/master -O nginx_ajp_module-master.zip

tar zxvf zlib-1.2.8.tar.gz

tar zxvf openssl-1.0.1j.tar.gz

tar zxvf pcre-8.36.tar.gz

tar zxvf nginx-1.6.2.tar.gz

unzip nginx_ajp_module-master.zip

3.编译安装

./configure  --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error/error.log --pid-path=/usr/local/nginx/logs/nginx.pid --user=nginx --group=nginx --http-log-path=/usr/local/nginx/logs/httpd.log --http-client-body-temp-path=/usr/local/nginx/temp/http-client-body-temp --http-proxy-temp-path=/usr/local/nginx/temp/http-proxy-temp --without-http_fastcgi_module --http-uwsgi-temp-path=/usr/local/nginx/temp/http-uwsgi-temp --http-scgi-temp-path=/usr/local/nginx/temp/http-scgi-temp --with-pcre=/usr/src/nginx/pcre-8.36 --with-zlib=/usr/src/nginx/zlib-1.2.8  --add-module=/usr/src/nginx/nginx_ajp_module-master/ --with-http_ssl_module

make
make install


4.配置

xx修改配置文件
user  daemon;          #指定子进程的用户

worker_processes  2;     #指定处理进程,跟CPU的个数是一样的

worker_cpu_affinity 0001 0010;   #分配进程到指定的CPU

error_log  logs/error.log;    指定error日志的位置

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

pid        logs/nginx.pid;    #指定PID的位置

worker_rlimit_nofile 65535     #限制文件句柄,操作系统也是需要用ulimit修改

events {

use epoll;          #指定I/O方式

worker_connections  65535;   #指定连接数

}

http {

include       mime.types;

default_type  application/octet-stream;

server_names_hash_bucket_size 128;

client_header_buffer_size 2k;   #默认使用该值来读取header值

large_client_header_buffers 4 4k; #如果header过大就是用这个参数,4k是根据文件系统的块设置的

#限制客户端上传文件大小

client_max_body_size 8m;

#日志格式

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;

sendfile       on;

tcp_nopush     on;

tcp_nodelay    on;

server_tokens off;

underscores_in_headers on;    #headers信息中带有下划线的header为合法

ignore_invalid_headers off;   #关闭忽略不可用的header

#keepalive_timeout  0;

keepalive_timeout  120;

open_file_cache max=204800 inactive=20s;

open_file_cache_min_uses 1;

open_file_cache_valid 30s;

gzip  on;    #启用压缩

gzip_min_length  1k;   #设置最小压缩页面

gzip_buffers     4 16k;

gzip_http_version 1.0;

gzip_comp_level 2;    #压缩级别,压缩比越大,消耗CPU更高,一般为3

#指定要压缩类型

gzip_types       text/plain application/x-javascript text/css application/xml;

gzip_vary on;

server {

#指定监听

listen       192.168.122.10:80;

server_name  127.0.0.1:80;

#charset koi8-r;

access_log  logs/host.access.log  main;

location / {

root   /web;

#Nginx默认是不允许列出整个目录的。如果有需要可以加autoindex on;

#index  index.html index.htm;

}

#指定留言板应用的位置和后端tomcat的ajp1.3

location /liuyan {

alias /web/liluyan/;

ajp_connect_timeout 120s;   #连接超时时间

ajp_keep_conn on;      #保持回话

ajp_pass 127.0.0.1:8009;    #后端tomcat的ajp1.3

}

#alias指向文件

location = /test.tar.gz {

alias  /web/test.tar.gz;

}

#安全加固

location ~ ^.*/WEB-INF/.*${

deny all;

}

location ~ /\.ht {

deny  all;

}

error_page  404              /404.html;

location = /50x.html {

alias   /web/no_found.html;

}

# redirect server error pages to the static page /50x.html

#

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   html;

}

}




所需文件见:http://pan.baidu.com/s/1kT8Ndf5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: