nginx札记-反向代理
作者:admin 发布于:2013-11-23 23:48 Saturday 分类:网络转载
nginx学习手札(二)反向代理
(2011-07-04 13:08:11)
如果服务器是Nginx负载均衡器或用于Nginx+PHP5的web服务器,此二项必须打开:
net.ipv4.tcp_tw_reuse = 1 #允许重新用于新的TCP连接
net.ipv4.tcp_tw_recycle = 1 #开启TCP连接快速回收
执行以下命令使内核配置立马生效:
/sbin/sysctl –p
配置:
#使用哪个用户启动nginx 前面是用户,后面是组
user nobody;
#nginx工作的进程数量,根据硬件调整,一般个CPU个数相等
worker_processes 20;
# [ debug | info | notice | warn | error | crit ] 错误日志的位置
error_log logs/error.log crit;
#进程号保存文件
pid logs/nginx.pid;
#最大文件描述符
worker_rlimit_nofile 65535;
events {
use epoll; #使用epoll(linux2.6的高性能方式)
worker_connections 65535; #每个进程最大连接数(最大连接=连接数x进程数)
}
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错误)
指定客户端一些比较大的请求头使用的缓冲区数量和大小。请求字
段不能大于一个缓冲区大小,如果客户端发送一个比较大的
头,nginx将返回"Request URI too large" (414)同样,请求
的头部最长字段不能大于一个缓冲区,否则服务器将返回"Bad
request" (400)。默认一个缓冲区大小为操作系统中分页文件大
小,通常是4k或8k,如果一个连接请求最终将状态转换为keep-
alive,它所占用的缓冲区将被释放。
client_max_body_size 8m; #允许客户端请求的最大单文件字节数,413错误,上传限制。
client_body_buffer_size 32k; #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存
到本地再传给用户
client_body_timeout 600; #客户端请求内容超时时间(状态码:408)
client_header_timeout 600; #客户端请求header头信息的超时时间(状态码:408)
proxy_connect_timeout 600; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 600; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_send_timeout 600; #后端服务器数据回传时间(代理发送超时)
proxy_buffer_size 32k; #代理服务器(nginx)保存用户头信息的缓冲区大小(400)
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;
tcp_nodelay on;
keepalive_timeout 65; #长链接超时时间
#开启压缩
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k; 一个缓存区的大小为分页大小,可用 getconf PAGESIZE获得。
gzip_http_version 1.0; 前端是squid的话
gzip_comp_level 4;
gzip_types text/* application/javascript application/x-javascript
application/xml text/css text/plain text/xml;
gzip_proxied any; 很重要!不加这个squid依然不缓存gzip文件
gzip_vary on;
open_file_cache max=65535 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
server_tokens off; #隐藏版本号
upstream tomcat {
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
server {
listen 80 default;
server_name _;
return 500;
access_log off;
}
server {
listen 80;
server_name www.test.com;
#charset utf-8;
charset GB2312;
access_log logs/host.access.log main;
index index.jsp index.html index.htm; #这里指www.test.com默认页面为index.jsp
location ~ ^/NginxStatus/ {
stub_status on; #监控Nginx的运行信息
access_log off;
auth_basic "NginxStatus";
auth_basic_user_file /opt/nginx/conf/htpasswd; #密码文件用apache的到htpasswd 工具生成
#if (-d $request_filename){
#rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
#}
}
location ~ ^/(WEB-INF)/ {
deny all;
}
#对于静态页面的请求,nginx直接处理并返回结果:(以下的针对后缀名做的判断,也可以定义某一路径下
的文件,即:location ~ ^/images/的格式)
location ~ \.(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ { 静态文件nginx自己处理,目录为html
root html;
expires 24h; 24小时过期
}
#对于动态页面的请求,nginx交给后端tomcat或weblogic等去处理:
location / {
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服
务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
#保留用户真实信息
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://tomcat; 动态文件交给upstream定义的集群处理
}
error_page 404 http://crm.zhaopin.com/tomcat.gif; #设置404转发页面
location /page-error {
rewrite ^(.*) http://crm.zhaopin.com/tomcat.gif permanent;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
#重点:如果要配置upstream 的话,配置中红色部分很重要!否则请求传给tomcat时,不会将域名的请求传过去,这样tomcat就只会根据http://IP:8080来处理!而不是http://域名:8080,这样就会访问不到所要的页面了!
如果不使用upstream,而直接写: proxy_pass http://www.domain.com:8080; 那就没关系了!
proxy_set_header Host $host; 当后端Web服务器上也配置有多个虚拟主机时,需要用该Header来区分反向代理哪个主机名。
proxy_set_header X-Forwarded-For $remote_addr; 如果后端Web服务器上的程序需要获取用户IP,请从该Header头获取
后端apache要想获得用户真实IP,网上说需要另加模块
参考:http://hi.baidu.com/171892549/blog/item/ae88d93fab568dc97d1e71a9.html
另一种:
在httpd.conf中修改日志的记录形式:
1.LogFormat "%{X-Forwarded-For}i %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %T" combined
192.168.2.68 - - [23/Jul/2010:13:16:56 +0800] "GET......... (2.68为客户真实IP)
2.LogFormat "%{X-Forwarded-For}i %h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
192.168.2.68 192.168.2.187 - - [23/Jul/2010:13:17:33 +0800] "GET...
(2.68为客户真实IP,2.187为nginx反向代理的IP)
测试结果正常,上面的例子是所有的都交给tomcat处理,除了处理定义的html之类。
如果:所有文件都交给nginx,而仅仅动态的交给tomcat,那么配置如下:
location / { #所有文件都交给nginx,除了下面定义的那些
root html;
expires 24h;
}
location ~ \.(jsp|asp|do) { #jsp,asp,do等servlet交给tomcat处理(有其他servlet另行添加)
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_pass http://tomcat;
}
监控到 Nginx 的运行信息含义:
Active connections: 70
server accepts handled requests
1000 1000 2300
Reading: 0 Writing: 3 Waiting: 67
NginxStatus 显示的内容意思如下:
· active connections – 当前 Nginx 正处理的活动连接数。
· server accepts handled requests -- 总共处理了1000个连接 , 成功创建 1000次握手 ( 证明中间没有失败的 ), 总共处理了 2300 个请求 ( 平均每次握手处理了 2.3 个数据请求 )。
· reading -- nginx 读取到客户端的 Header 信息数。
· writing -- nginx 返回给客户端的 Header 信息数。
· waiting -- 开启 keep-alive 的情况下,这个值等于 active - (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。
·
顺便附上FastCGI在nginx配置的几个指令:
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;
这个指令为nginx配置FastCGI缓存指定一个路径,目录结构等级,关键字区域存储时间和非活动删除时间。
fastcgi_connect_timeout 300;
指定连接到后端FastCGI的超时时间。
fastcgi_send_timeout 300;
向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。
fastcgi_read_timeout 300;
接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。
fastcgi_buffer_size 4k;
指定读取FastCGI应答第一部分需要用多大的缓冲区,一般第一部分应答不会超过1k,由于页面大小为4k,所以这里设置为4k。
fastcgi_buffers 8 4k;
指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答。
fastcgi_busy_buffers_size 8k;
这个指令我也不知道是做什么用,只知道默认值是fastcgi_buffers的两倍。
fastcgi_temp_file_write_size 8k;
在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍。
fastcgi_cache TEST
开启FastCGI缓存并且为其制定一个名称。个人感觉开启缓存非常有用,可以有效降低CPU负载,并且防止502错误。
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
为指定的应答代码指定缓存时间,如上例中将200,302应答缓存一小时,301应答缓存1天,其他为1分钟。
fastcgi_cache_min_uses 1;
缓存在fastcgi_cache_path指令inactive参数值时间内的最少使用次数,如上例,如果在5分钟内某文件1次也没有被使用,那么这个文件将被移除。
fastcgi_cache_use_stale error timeout invalid_header http_500;
不知道这个参数的作用,猜想应该是让nginx知道哪些类型的缓存是没用的。
这里有一篇网友的配置方案。原文地址:http://blog.chinaunix.net/u2/71owart_2060689.html
配置中有两个虚拟主机,都做到了动静分离。
安装nginx
[root@webserver src]# tar zxvf nginx-0.7.62.tar.gz
[root@webserver nginx-0.7.62]# yum install pcre-devel
[root@webserver nginx-0.7.62]# yum install openssl*
[root@webserver nginx-0.7.62]# yum install zlib*
[root@webserver nginx-0.7.62]# ./configure --with-http_stub_status_module --prefix=/usr/local/nginx
[root@webserver nginx-0.7.62]# make
[root@webserver nginx-0.7.62]# make install
[root@webserver nginx-0.7.62]# /usr/local/nginx/sbin/nginx 启动nginx
安装tomcat与jdk
[root@webserver src]# mv apache-tomcat-6.0.18.tar.gz jdk-6u13-linux-i586.bin /opt
[root@webserver src]# cd /opt
[root@webserver opt]# tar zxvf apache-tomcat-6.0.18.tar.gz ; chmod +x jdk-6u13-linux-i586.bin
[root@webserver opt]# ./jdk-6u13-linux-i586.bin
[root@webserver opt]# mv apache-tomcat-6.0.18 tomcat ; mv jdk1.6.0_13 java
[root@webserver opt]# vi /etc/profile
JAVA_HOME=/opt/java
CATALINA_HOME=/opt/tomcat
CATALINA_BASE=/opt/tomcat
CLASSPATH=./:${JAVA_HOME}/lib:${JAVA_HOME}/jre/lib/ext:${CATALINA_HOME}/common/lib
export JAVA_HOME CATALINA_HOME CATALINA_BASE CLASSPATH
export PATH=${JAVA_HOME}/bin:${CATALINA_HOME}/bin:${PATH}
[root@webserver opt]# source /etc/profile
[root@webserver opt]# cd /usr/local/src/
[root@webserver src]# tar xvf mysql-connector-java-5.1.7.tar.gz.tar
[root@webserver src]# cp mysql-connector-java-5.1.7/mysql-connector-java-5.1.7-bin.jar /opt/tomcat/lib/
测试nginx与tomcat虚拟主机
1.客户端测试环境搭建
A:
客户端xp的配置:C:\WINDOWS\system32\drivers\etc\hosts添加
192.168.1.33 www.test.com
192.168.1.33 good.test.com
2:服务器的配置:
地址:192.168.1.33
第一步:
添加地址域名对应关系,如没有启动nginx会报错
#vi /etc/hosts
127.0.0.1 www.test.com
127.0.0.1 good.test.com
第二步:
nginx添加基于域名的虚拟主机
#vi nginx.conf
user nobody;
worker_processes 2;
events {
use epoll;
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log;
sendfile on;
tcp_nodelay on;
keepalive_timeout 65;
server {
listen 80;
server_name www.test.com;
charset utf-8;
location ~ ^/NginxStatus/ {
stub_status on;
access_log off;
}
location ~ ^/(WEB-INF)/ {
deny all;
}
location ~ \.(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ {
root html/test;
expires 24h;
}
location / {
root html/test;
index index.jsp index.html index.htm;
proxy_pass http://www.test.com:8080;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 404 /html/404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name good.test.com;
charset utf-8;
location ~ ^/NginxStatus/ {
stub_status on;
access_log off;
}
location ~ ^/(WEB-INF)/ {
deny all;
}
location ~ \.(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ {
root html/good;
expires 24h;
}
location / {
root html/good;
index index.jsp index.html index.htm;
proxy_pass http://good.test.com:8080;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 404 /html/404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
第三步:
添加tomcat的虚拟主机
#vi server.xml
<Host name="www.test.com" appBase="webapps"
unpackWARs="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/usr/local/nginx/html/test" debug="0" reloadable="true" crossContext="true"/>
</Host>
<Host name="good.test.com" appBase="webapps"
unpackWARs="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/usr/local/nginx/html/good" debug="0" reloadable="true" crossContext="true"/>
</Host>
第四步:
检测语法正确与启动服务
#/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
#killall nginx
#/usr/local/nginx/sbin/nginx
#/opt/tomcat/bin/startup.sh
如果都没错的话
然后在IE地址栏输入
是ok的了
标签: nginx