Nginx使用总结,nginx总结


 

Nginx使用总结 

 718人阅读 评论(0) 收藏 举报  分类:  

目录(?)[+]

Nginx中文手册


一、 Nginx 基础知识
二、 Nginx 安装及调试
三、 Nginx Rewrite
四、 Nginx Redirect
五、 Nginx 目录自动加斜线:
六、 Nginx Location
七、 Nginx expires
八、 Nginx 防盗链
九、 Nginx 访问控制
十、 Nginx日志处理
十一、 Nginx Cache
十二、 Nginx 负载均衡
十三、 Nginx简单优化
十四、 如何构建高性能的LEMP环境
十五、 Nginx服务监控
十六、 常见问题与错误处理.
十七、 相关资源下载

【前言】:

编写此技术指南在于推广普及NGINX在国内的使用,更方便的帮助大家了解和掌握NGINX
的一些使用技巧。本指南很多技巧来自于网络和工作中或网络上朋友们问我的问题.在此对
网络上愿意分享的朋友们表示感谢和致意!欢迎大家和我一起丰富本技术指南提出更好的建
议!请朋友们关注: http://www.linuxtone.org 技术分享社区! 互想学习共同进步!

一、 Nginx 基础知识

1、简介
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 

代理服
务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该

站点运行超
过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经

因为它的稳
定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

更多的请见官方wiki: http://wiki.codemongers.com/

2、 Nginx的优点
nginx做为HTTP服务器,有以下几项基本特性:
1) 处理静态文件,索引文件以及自动索引;打开文件描述符缓冲.
2) 无缓存的反向代理加速,简单的负载均衡和容错.
3) FastCGI,简单的负载均衡和容错.
4) 模块化的结构。包括gzipping, byte ranges, chunked responses, 以及 SSI-filter等filter。
如果由FastCGI或其它代理服务器处理单页中存在的多个SSI,则这项处理可以并行运行,而不
需要相互等待。
5) 支持SSL 和 TLS SNI.
Nginx专为性能优化而开发,性能是其最重要的考量, 实现上非常注重效率 。它支持内核Poll模

型,
能经受高负载的考验, 有报告表明能支持高达 50,000 个并发连接数。

Nginx具有很高的稳定性。其它HTTP服务器,当遇到访问的峰值,或者有人恶意发起慢速连接

时,
也很可能会导致服务器物理内存耗尽频繁交换,失去响应,只能重启服务器。例如当前apache

一旦上到
200个以上进程,web响应速度就明显非常缓慢了。而Nginx采取了分阶段资源分配技术,使得

它的CPU与
内存占用率非常低。nginx官方表示保持10,000个没有活动的连接,它只占2.5M内存,所以类

似DOS这
样的攻击对nginx来说基本上是毫无用处的。就稳定性而言, nginx比lighthttpd更胜一筹。

Nginx支持热部署。它的启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月

也不
需要重新启动。你还能够在不间断服务的情况下,对软件版本进行进行升级。

Nginx采用master-slave模型, 能够充分利用SMP的优势,且能够减少工作进程在磁盘I/O的阻
塞延迟。当采用select()/poll()调用时,还可以限制每个进程的连接数。

Nginx代码质量非常高,代码很规范, 手法成熟, 模块扩展也很容易。特别值得一提的是强大
的Upstream与Filter链。 Upstream为诸如reverse proxy, 与其他服务器通信模块的编写奠定了

很好的
基础。而Filter链最酷的部分就是各个filter不必等待前一个filter执行完毕。它可以把前一个filter
的输出做为当前filter的输入,这有点像Unix的管线。这意味着,一个模块可以开始压缩从后端

服务器
发送过来的请求,且可以在模块接收完后端服务器的整个请求之前把压缩流转向客户端。

Nginx采用了一些os提供的最新特性如对sendfile (Linux 2.2+),accept-filter (FreeBSD
4.1+),TCP_DEFER_ACCEPT (Linux 2.4+) 的支持,从而大大提高了性能

二、 Nginx 安装及调试

1、Pcre 安装

./configure
make && make install
cd ../
2. nginx 编译安装

/configure --user=www --group=www --prefix=/usr/local/nginx/ --with-

http_stub_status_module
--with-openssl=/usr/local/openssl

make && make install
更详细的模块定制与安装请参照官方wiki.

3、Nginx 配置文件测试:

# /usr/local/nginx/sbin/nginx -t //Debug 配置文件的关键命令需要重点撑握.
2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf
syntax is ok
2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf

was
tested successfully

3、 Nginx 启动:

# /usr/local/nginx/sbin/nginx
4、 Nginx 配置文件修改重新加载:

# kill -HUP `cat /usr/local/nginx/logs/nginx.pid `
三、 Nginx Rewrite

1. Nginx Rewrite 基本标记(flags)
last - 基本上都用这个Flag。
※相当于Apache里的[L]标记,表示完成rewrite,不再匹配后面的规则
break - 中止Rewirte,不再继续匹配
redirect - 返回临时重定向的HTTP状态302
permanent - 返回永久重定向的HTTP状态301
※原有的url支持正则 重写的url不支持正则

2. 正则表达式匹配,其中:
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~ 和 !~* 分别为区分大小写不匹配及不区分大小写不匹配

3. 文件及目录匹配,其中:
-f 和 !-f 用来判断是否存在文件
-d 和 !-d 用来判断是否存在目录
-e 和 !-e 用来判断是否存在文件或目录
-x 和 !-x 用来判断文件是否可执行

3. Nginx 的一些可用的全局变量,可用做条件判断:

$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
四、 Nginx Redirect

将所有linuxtone.org与netseek.linuxtone.org域名全部自跳转到http://www.linuxtone.org

server {
    listen 80;
    server_name linuxtone.org netseek.linuxtone.org;
    index index.html index.php;
    root /data/www/wwwroot;
    if ($host !~ "^www\.linxtone\.org$") {
        rewrite ^(.*) http://www.linuxtone.org$1 redirect;
    }
    .....................
}
五、 Nginx 目录自动加斜线:

if (-d $request_filename){
    rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
六、 Nginx Location

1.基本语法:[和上面rewrite正则匹配语法基本一致]
location [=|~|~*|^~] /uri/ { … }
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~ 和 !~* 分别为区分大小写不匹配及不区分大小写不匹配

示例1:

location = / {
    # matches the query / only.
    # 只匹配 / 查询。
}
匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询

匹配

示例2:

location ^~ /images/ {
    # matches any query beginning with /images/ and halts searching,
    # so regular expressions will not be checked.
    # 匹配任何以 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
}
示例3:

location ~* \.(gif|jpg|jpeg)$ {
    # matches any request ending in gif, jpg, or jpeg. However, all
    # requests to the /images/ directory will be handled by
    # 匹配任何以 gif、jpg 或 jpeg 结尾的请求。
}
七、 Nginx expires

1.根据文件类型判断,添加expires

# Add expires header for static content
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
    if (-f $request_filename) {
        root /data/www/wwwroot/bbs;
        expires 1d;
        break;
    }
}
2、根据某个目录判断,添加expires

# serve static files
location ~ ^/(images|JavaScript|js|css|flash|media|static)/ {
    root /data/www/wwwroot/down;
    expires 30d;
}
八、 Nginx 防盗链

1. 针对不同的文件类型

#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
    valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost

baidu.com;
    if ($invalid_referer) {
        rewrite ^/ http://www.linuxtone.org/images/default/logo.gif;
        # return 403;
    }
}
2. 针对不同的目录

location /img/ {
    root /data/www/wwwroot/bbs/img/;
    valid_referers none blocked server_names *.linuxtone.org http://localhost baidu.com;
    if ($invalid_referer) {
        rewrite ^/ http://www.linuxtone.org/images/default/logo.gif;
        #return 403;
    }
}
3. 同实现防盗链和expires的方法

#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
    valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost ;
    if ($invalid_referer) {
        rewrite ^/ http://www.linuxtone.org/images/default/logo.gif;
    }
    access_log off;
    root /data/www/wwwroot/bbs;
    expires 1d;
    break;
}
九、 Nginx 访问控制

1. Nginx 身份证验证

#cd /usr/local/nginx/conf
#mkdir htpasswd
/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji linuxtone
#添加用户名为linuxtone
New password: (此处输入你的密码)
Re-type new password: (再次输入你的密码)
Adding password for user

http://count.linuxtone.org/tongji/data/index.html(目录存在/data/www/wwwroot/tongji/data/

目录

下)

将下段配置放到虚拟主机目录,当访问http://count.linuxtone/tongji/即提示要密验证:

location ~ ^/(tongji)/ {
    root /data/www/wwwroot/count;
    auth_basic "LT-COUNT-TongJi";
    auth_basic_user_file /usr/local/nginx/conf/htpasswd/tongji;
}
2. Nginx 禁止访问某类型的文件.

如,Nginx下禁止访问*.txt文件,配置方法如下.

location ~* \.(txt|doc)$ {
    if (-f $request_filename) {
        root /data/www/wwwroot/linuxtone/test;
        #rewrite …..可以重定向到某个URL
        break;
    }
}
方法2:

location ~* \.(txt|doc)${
    root /data/www/wwwroot/linuxtone/test;
    deny all;
}
实例:
禁止访问某个目录

location ~ ^/(WEB-INF)/ {
    deny all;
}
3. 使用ngx_http_access_module限制ip访问

location / {
    deny 192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    deny all;
}
详细参见wiki: http://wiki.codemongers.com/NginxHttpAccessModule#allow

4. Nginx 下载限制并发和速率

limit_zone linuxtone $binary_remote_addr 10m;
server {
    listen 80;
    server_name down.linuxotne.org;
    index index.html index.htm index.php;
    root /data/www/wwwroot/down;
    #Zone limit
    location / {
        limit_conn linuxtone 1;
        limit_rate 20k;
    }
    ..........
}
只允许客房端一个线程,每个线程20k.

【注】limit_zone linuxtone $binary_remote_addr 10m; 这个可以定义在主的

5. Nginx 实现Apache一样目录列表

location / {
    autoindex on;
}
6. 上文件大小限制
主配置文件里加入如下,具体大小根据你自己的业务做调整。

client_max_body_size 10m;
十、 Nginx 日志处理

1.Nginx 日志切割

#contab -e
59 23 * * * /usr/local/sbin/logcron.sh /dev/null 2>&1
# cat /usr/local/sbin/logcron.sh

#!/bin/bash

log_dir="/data/logs"

time=`date +%Y%m%d`

/bin/mv ${log_dir}/access_linuxtone.org.log 

${log_dir}/access_count.linuxtone.org.$time.logkill -USR1 `cat /var/run/nginx.pid`
更多的日志分析与处理就关注(同时欢迎你参加讨论):http://bbs.linuxtone.org/forum-8-1.html

2.利用AWSTATS分析NGINX日志
设置好Nginx日志格式,仍后利用awstats进行分析.
请参考: http://bbs.linuxtone.org/thread-56-1-1.html

3. Nginx 如何不记录部分日志
日志太多,每天好几个G,少记录一些,下面的配置写到server{}段中就可以了

location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {
    access_log off;
}
十一、Nginx Cache服务配置

如果需要将文件缓存到本地,则需要增加如下几个子参数:

proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path 缓存目录;
其中,
proxy_store on 用来启用缓存到本地的功能,
proxy_temp_path 用来指定缓存在哪个目录下,如:proxy_temp_path html;

在经过上一步配置之后,虽然文件被缓存到了本地磁盘上,但每次请求仍会向远端拉取
文件,为了避免去远端拉取文件,必须修改proxy_pass:

if ( !-e $request_filename) {
    proxy_pass http://mysvr;
}
即改成有条件地去执行proxy_pass,这个条件就是当请求的文件在本地的
proxy_temp_path指定的目录下不存在时,再向后端拉取。

更多更高级的应用可以研究ncache,官方网站: http://code.google.com/p/ncache/

详细安装请参照http://bbs.linuxtone.org 应用加速版ncache相关的贴子.

十二、Nginx 负载均衡

1. Nginx 负载均衡基础知识

nginx的upstream目前支持4种方式的分配

1)、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2)、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

2)、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session

的问题。

3)、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。

4)、url_hash(第三方)

2. Nginx 负载均衡实例1

upstream bbs.linuxtone.org { #定义负载均衡设备的Ip及设备状态
    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中增加

proxy_pass http://bbs.linuxtone.org/;
每个设备的状态设置为:
a) down 表示单前的server暂时不参与负载
b) weight 默认为1.weight越大,负载的权重就越大。
c) max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 

模块定义的错误
d) fail_timeout:max_fails次失败后,暂停的时间。
e) backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器

压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录
location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

3. Nginx 负载均衡实例 2
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓
存时比较有效,也可以用作提高Squid缓存命中率.

简单的负载均等实例:
#vi nginx.conf //nginx主配置文件核心配置
.........

#loadblance my.linuxtone.org
upstream my.linuxtone.org {
    ip_hash;
    server 127.0.0.1:8080;
    server 192.168.169.136:8080;
    server 219.101.75.138:8080;
    server 192.168.169.117;
    server 192.168.169.118;
    server 192.168.169.119;
}
.........
include vhosts/linuxtone_lb.conf;
.........
# vi proxy.conf

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_store on;
proxy_store_access user:rw group:rw all:r;
#nginx cache
#client_body_temp_path /data/nginx_cache/client_body 1 2;
proxy_temp_path /data/nginx_cache/proxy_temp 1 2;
#vi linuxtone_lb.conf

server {

    listen 80;
    server_name my.linuxtone.org;
    index index.php;
    root /data/www/wwwroot/mylinuxtone;

    if (-f $request_filename) {
        break;
    }

    if (-f $request_filename/index.php) {
        rewrite (.*) $1/index.php break;
    }

    error_page 403 http://my.linuxtone.org/member.php?m=user&a=login;

    location / {
        if ( !-e $request_filename) {
            proxy_pass http://my.linuxtone.org;
            break;
        }
        include /usr/local/nginx/conf/proxy.conf;
    }
}
十三、Nginx简单优化

1. 减小nginx编译后的文件大小 (Reduce file size of nginx)
默认的nginx编译选项里居然是用debug模式(-g)的(debug模式会插入很多跟踪和
ASSERT之类),编译以后一个nginx有好几兆。去掉nginx的debug模式编译,编译以
后只有几百K

在 auto/cc/gcc,最后几行有:

# debug
CFLAGS=”$CFLAGS -g”
注释掉或删掉这几行,重新编译即可。

2. 修改Nginx的header伪装服务器

1) 修改nginx.h

#vi nginx-0.7.30/src/core/nginx.h
#define NGINX_VERSION "1.8"
#define NGINX_VER "LTWS/" NGINX_VERSION
#define NGINX_VAR "NGINX"
#define NGX_OLDPID_EXT ".oldbin"
2) 修改nginx_http_header_filter_module
#vi nginx-0.7.30/src/http/ngx_http_header_filter_module.c
将如下

static char ngx_http_server_string[] = "Server: nginx" CRLF;
修改为

static char ngx_http_server_string[] = "Server: LTWS" CRLF;
a) 修改nginx_http_header_filter_module
#vi nginx-0.7.30/src/http/ngx_http_special_response.c
将如下:

static u_char ngx_http_error_full_tail[] =

--------------------------------------------------------------------------------

" NGINX_VER "
" CRLF
"" CRLF
"" CRLF
;

static u_char ngx_http_error_tail[] =

--------------------------------------------------------------------------------

nginx
" CRLF
"" CRLF
"" CRLF
;
修改为:

static u_char ngx_http_error_full_tail[] =

"NGINX_VER"
" CRLF

--------------------------------------------------------------------------------

http://www.linuxtone.org
" CRLF
"" CRLF
"" CRLF
;

static u_char ngx_http_error_tail[] =

--------------------------------------------------------------------------------

LTWS
" CRLF
"" CRLF
"" CRLF
;
修改后重新编译一下环境,

404错误的时候显示效果图(如果没有指定错误页的话):

利用curl命令查看服务器header

3. 为特定的CPU指定CPU类型编译优化.

默认nginx使用的GCC编译参数是-O

需要更加优化可以使用以下两个参数

--with-cc-opt='-O3' \

--with-cpu-opt=opteron \
使得编译针对特定CPU以及增加GCC的优化.

此方法仅对性能有所改善并不会有很大的性能提升,供朋友们参考.

CPUD类型确定:

# cat /proc/cpuinfo | grep "model name"
编译优化参数参考:http://en.gentoo-wiki.com/wiki/Safe_Cflags

4. Tcmalloc优化Nginx 性能

# wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gz
# tar zxvf libunwind-0.99-alpha.tar.gz
# cd libunwind-0.99-alpha/
# CFLAGS=-fPIC ./configure
# make CFLAGS=-fPIC
# make CFLAGS=-fPIC install
# wget http://google-perftools.googlecode.com/files/google-perftools-0.98.tar.gz
# tar zxvf google-perftools-0.98.tar.gz
# cd google-perftools-0.98/
# ./configure
# make && make install
# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
# ldconfig
# lsof -n | grep tcmalloc
编译nginx 加载google_perftools_module:

./configure --with-google_perftools_module
在主配置文件加入nginx.conf 添加:

google_perftools_profiles /path/to/profile;
5. 内核参数优化
# vi /etc/sysctl.conf

#在末尾增加以下内容:
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000
#使配置立即生效
/sbin/sysctl -p

十四、如何构建高性的LEMP

请参见: http://www.linuxtone.org/lemp/lemp.pdf

1、提供完整的配置脚本下载:http://www.linuxtone.org/lemp/scripts.tar.gz
2、提供NGINX常见配置范例含(虚拟主机,防盗链,Rewrite,访问控制,负载均衡Discuz相关

程序静态化及等等),你只要稍稍修改即可线上应用。
3、将原版的xcache替换成EA,并提供相关简单调优脚本及配置文件。

更多的及更新资料请关注: http://www.linuxtone.org

十五、Nginx监控

1、 RRDTOOL+Perl脚本画图监控
先安装好rrdtool ,关于rrdtool本文不作介绍,具体安装请参照linuxtone监控版块.

#cd /usr/local/sbnin
#wget http://blog.kovyrin.net/files/mrtg/rrd_nginx.pl.txt
#mv rrd_nginx.pl.txt rrd_nginx.pl
#chmod a+x rrd_nginx.pl
#vi rrd_nginx.pl //配置脚本文件设置好路径

#!/usr/bin/perl
use RRDs;
use LWP::UserAgent;

# define location of rrdtool databases
my $rrd = '/data/www/wwwroot/nginx/rrd';

# define location of images
my $img = '/data/www/wwwroot/nginx/html';

# define your nginx stats URL
my $URL = "http:// 219.32.205.13/nginx_status";
…………
【注】根据自己具体的状况修改相应的路径.

#crontab –e //加入如下

* * * * * /usr/local/sbin/rrd_nginx.pl
重启crond后,通过配置nginx虚拟主机指到/data/www/wwwroot/nginx/html目录,通过crond
自动执行perl脚本会生成很多图片.

http://xxx/connections-day.png即可看到服务器状态图。

2、 官方Nginx-rrd 监控服务(多虚拟主机)(推荐)
网址:http://www.nginx.eu/nginx-rrd.html

此解决方案其实是基于上述监控方案的一个改进和增强,同样先安装好rrdtool这个画图工
具和相应的perl模块再做如下操作:

# yum install perl-HTML*
先建立好生成的库存和图片存放录

#mkdir -p /data/www/wwwroot/nginx/{rrd,html}
#cd /usr/local/sbin
#wget http://www.nginx.eu/nginx-rrd/nginx-rrd-0.1.4.tgz
#tar zxvf nginx-rrd-0.1.4.tgz
#cd nginx-rrd-0.1.4
#cd etc/
#cp nginx-rrd.conf /etc
#cd etc/cron.d
#cp nginx-rrd.cron /etc/cron.d
#cd /usr/local/src/nginx-rrd-0.1.4/html
# cp index.php /data/www/wwwroot/nginx/html/
#cd /usr/local/src/nginx-rrd-0.1.4/usr/sbin
#cp * /usr/sbin/
#vi /etc/nginx-rrd.conf

#####################################################
#
# dir where rrd databases are stored

RRD_DIR="/data/www/wwwroot/nginx/rrd";

# dir where png images are presented

WWW_DIR="/data/www/wwwroot/nginx/html";

# process nice level

NICE_LEVEL="-19";

# bin dir

BIN_DIR="/usr/sbin";

# servers to test

# server_utl;server_name

SERVERS_URL="http://219.32.205.13/nginx_status;219.32.205.13
http://www.linuxtone.org/nginx_status;www.linuxtone.org"" //根据你的具体情况做调整.

SEVERS_URL 格式 http://domain1/nginx_status;domain1

http://domain2/nginx_status;domain2

这种格式监控多虚拟主机连接状态:
重点启crond服务,仍后通过http://219.32.205.13/nginx/html/ 即可访问。配置过程很简单!

3、 CACTI模板监控Nginx

利用Nginx_status状态来画图实现CACTI监控
nginx编译时允许http_stub_status_module

# vi /usr/local/nginx/conf/nginx.conf

location /nginx_status {
    stub_status on;
    access_log off;
    allow 192.168.1.37;
    deny all;
}# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
# wget http://forums.cacti.net/download.php?id=12676
# tar xvfz cacti-nginx.tar.gz
# cp cacti-nginx/get_nginx_socket_status.pl /data/cacti/scripts/
# cp cacti-nginx/get_nginx_clients_status.pl /data/cacti/scripts/
# chmod 755 /data/cacti/scripts/get_nginx*
检测插件
# /data/cacti/scripts/get_nginx_clients_status.pl http://192.168.1.37/nginx_status

在cacti管理面板导入

cacti_graph_template_nginx_clients_stat.xml
cacti_graph_template_nginx_sockets_stat.xml
十六、常见问题与错误处理

1、 400 bad request错误的原因和解决办法
配置nginx.conf相关设置如下.

client_header_buffer_size 16k;
large_client_header_buffers 4 64k;
根据具体情况调整,一般适当调整值就可以。

2、 Nginx 502 Bad Gateway错误

proxy_next_upstream error timeout invalid_header http_500 http_503;
或者尝试设置:

large_client_header_buffers 4 32k;
3、 Nginx出现的413 Request Entity Too Large错误

这个错误一般在上传文件的时候会出现,

编辑Nginx主配置文件Nginx.conf,找到http{}段,添加

client_max_body_size 10m; //设置多大根据自己的需求作调整.
如果运行php的话这个大小client_max_body_size要和php.ini中的如下值的最大值
一致或者稍大,这样就不会因为提交数据大小不一致出现的错误。

post_max_size = 10M
upload_max_filesize = 2M
4、 解决504 Gateway Time-out(nginx)

遇到这个问题是在升级discuz论坛的时候遇到的

一般看来, 这种情况可能是由于nginx默认的fastcgi进程响应的缓冲区太小造成的,
这将导致fastcgi进程被挂起, 如果你的fastcgi服务对这个挂起处理的不好, 那么最后就
极有可能导致504 Gateway Time-out

现在的网站, 尤其某些论坛有大量的回复和很多内容的, 一个页面甚至有几百K。

默认的fastcgi进程响应的缓冲区是8K, 我们可以设置大点

在nginx.conf里, 加入:

fastcgi_buffers 8 128k
这表示设置fastcgi缓冲区为8×128k

当然如果您在进行某一项即时的操作, 可能需要nginx的超时参数调大点,例如设置成60秒:

send_timeout 60;
只是调整了这两个参数, 结果就是没有再显示那个超时, 可以说效果不错, 但是也
可能是由于其他的原因, 目前关于nginx的资料不是很多, 很多事情都需要长期的经验
累计才有结果, 期待您的发现哈!

5、 如何使用Nginx Proxy

朋友一台服务器运行tomcat 为8080端口,IP:192.168.1.2:8080,另一台机器
IP:192.168.1.8. 朋友想通过访问http://192.168.1.8即可访问tomcat服务.配置如下:

在192.168.1.8的nginx.conf上配置如下:

server {
    listen 80;
    server_name Java.linuxtone.org
    location / {
        proxy_pass http://192.168.1.2:8080;
        include /usr/local/nginx/conf/proxy.conf;
    }
}
6、 如何关闭Nginx的LOG

access_log /dev/null;
error_log /dev/null;

 

Nginx反向代理tomcat服务器 


一、Tomcat
基本配置

1.为Tomcat提供SysV脚本
2.catalina 脚本讲解
3.telnet 登录管理Tomcat
4.配置Tomcat虚拟主机
5.Tomcat图形管理接口

6.部署JSP网站案例

 


二、Nginx反向代理Tomcat服务器 

 

 


1.Nginx将请求反向代理到后端Tomcat
2.Nginx将图片缓存到本地
3.Nginx将请求实现动静分离 

 

注,实验环境说明,操作系统:CentOS 6.4 x86_64,软件版本:jdk-7u40、apache-tomcat-

7.0.42、Nginx-1.4.2,博客中所用到的软件请到这里下载:http://yunpan.cn/QGBCLwrZnpLMS

一、Tomcat 基本配置

1.为Tomcat提供SysV脚本

注,在上一篇博文中我们已经演示安装了Tomcat,这里我们就不在演示,不清楚的博友可以

参考这篇博文,http://freeloda.blog.51cto.com/2033581/1299644,在上一篇博文中我们没有

增加,SysV脚本,在这篇博文中我们来增加一下,下面我们就来具体演示一下。

[root@tomcat ~]# vim /etc/init.d/tomcat
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP Container.
CATALINA_HOME=/usr/local/tomcat #注意你的脚本路径
export CATALINA_HOME
# export CATALINA_OPTS="-Xms128m -Xmx256m"
exec $CATALINA_HOME/bin/catalina.sh $*

下面我们来增加执行权限,并加入服务列表设置开机自启动,

[root@tomcat ~]# chmod +x /etc/init.d/tomcat
[root@tomcat ~]# chkconfig --add tomcat
[root@tomcat ~]# chkconfig tomcat --list

tomcat 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

下面我们来启动一下Tomcat并测试一下,

[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

查看一下启动的端口号,

[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name
tcp    0   0 0.0.0.0:22         0.0.0.0:*          LISTEN   1044/sshd
tcp    0   0 127.0.0.1:25        0.0.0.0:*          LISTEN   1121/master
tcp    0   0 127.0.0.1:6010       0.0.0.0:*          LISTEN   12988/sshd
tcp    0   0 127.0.0.1:6011       0.0.0.0:*          LISTEN   13053/sshd
tcp    0   0 :::8080           :::*            LISTEN   13088/java
tcp    0   0 :::22            :::*            LISTEN   1044/sshd
tcp    0   0 ::1:25           :::*            LISTEN   1121/master
tcp    0   0 ::1:6010          :::*            LISTEN   12988/sshd
tcp    0   0 ::1:6011          :::*            LISTEN   13053/sshd
tcp    0   0 ::ffff:127.0.0.1:8005    :::*            LISTEN   13088/java
tcp    0   0 :::8009           :::*            LISTEN   13088/java

用浏览器访问一下,

 

好了,到这里Tomcat的SysV脚本增加完成,下面我们来说一下catalina脚本。

2.catalina 脚本讲解

首先我们来查看一下这个脚本,

[root@tomcat bin]# catalina.sh -h
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr/java/jdk1.7.0_40
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Usage: catalina.sh ( commands ... )
commands:
 debug       Start Catalina in a debugger
 debug -security  Debug Catalina with a security manager
 jpda start    Start Catalina under JPDA debugger
 run        Start Catalina in the current window
 run -security   Start in the current window with security manager
 start       Start Catalina in a separate window
 start -security  Start in a separate window with security manager
 stop       Stop Catalina, waiting up to 5 seconds for the process to end
 stop n      Stop Catalina, waiting up to n seconds for the process to end
 stop -force    Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
 stop n -force   Stop Catalina, wait up to n seconds and then use kill -KILL if still running
 configtest    Run a basic syntax check on server.xml - check exit code for result
 version      What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that 

$CATALINA_PID is defined

注,从帮助上来看,这个脚本使用还是挺简单的。下面我们来说几个常用的选项,

catalina.sh

下面我们就来具体演示一下,

[root@tomcat ~]# catalina.sh configtest
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr/java/jdk1.7.0_40
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
九月 21, 2013 11:08:26 下午 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in

production environments was not found on the java.library.path: 

/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
九月 21, 2013 11:08:27 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
九月 21, 2013 11:08:27 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8009"]
九月 21, 2013 11:08:27 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1269 ms

注,使用configtest选项时,得关闭Tomcat,不然会报错。

启动Tomcat,

[root@tomcat ~]# catalina.sh start
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr/java/jdk1.7.0_40
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

关闭Tomcat,

[root@tomcat ~]# catalina.sh stop
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr/java/jdk1.7.0_40
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

查看Tomcat版本,

[root@tomcat ~]# catalina.sh version
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr/java/jdk1.7.0_40
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.42
Server built:  Jul 2 2013 08:57:41
Server number: 7.0.42.0
OS Name:    Linux
OS Version:   2.6.32-358.el6.x86_64
Architecture:  amd64
JVM Version:  1.7.0_40-b43
JVM Vendor:   Oracle Corporation

好了,catalina脚本,我们就说到这里了,下面我们来说一下telnet管理Tomcat。

3.telnet 登录管理Tomcat

注,在说telnet管理Tomcat之前,我们得先看一下默认的配置文件,这里面定义了默认的管理

端口,

[root@tomcat ~]# vim /usr/local/tomcat/conf/server.xml
<Server port="8005" shutdown="SHUTDOWN">

说明,定义了一个管理端口为8005,我们可以用telnet直接登录进本机的8005端口,来执行

SHUTDOWN命令,来关闭Tomcat实例。下面我们来具体演示一下,

先安装telnet客户端,

[root@tomcat ~]# yum install -y telnet

下面我们一测试并查看,

[root@tomcat ~]# telnet localhost 8005
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
SHUTDOWN #输入SHOWDOWN就可以直接关闭Tomcat服务。
Connection closed by foreign host.
[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name
tcp    0   0 0.0.0.0:22         0.0.0.0:*          LISTEN   1044/sshd
tcp    0   0 127.0.0.1:25        0.0.0.0:*          LISTEN   1121/master
tcp    0   0 127.0.0.1:6010       0.0.0.0:*          LISTEN   12988/sshd
tcp    0   0 127.0.0.1:6011       0.0.0.0:*          LISTEN   13053/sshd
tcp    0   0 :::22            :::*            LISTEN   1044/sshd
tcp    0   0 ::1:25           :::*            LISTEN   1121/master
tcp    0   0 ::1:6010          :::*            LISTEN   12988/sshd
tcp    0   0 ::1:6011          :::*            LISTEN   13053/sshd

注,大家可以看到Tomcat服务器已经关闭。好了,telnet管理我们就说到这里,下面我们来说

一下,Tomcat虚拟主机的配置。

4.配置Tomcat虚拟主机

注,在说Tomcat虚拟主机之前,咱们来详细的看看默认的配置文件,虽然在上一篇博客中全

部有讲解,在这篇博客中我还是再和大家简单说一下,下面是默认配置文件。大家可以看到,

绝大部分的配置文件是注释,包含在<!-- -->、全是注释。下面我们就来具体的看看,注释我

们就不说了,说具体的定义的内容

[root@tomcat ~]# cat /usr/local/tomcat/conf/server.xml
<?xml version='1.0' encoding='utf-8'?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements. See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
   define subcomponents such as "Valves" at this level.
   Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN"> #大家可以看到,这里是我们刚才讲解的,

定义一个管理接口
 <!-- Security listener. Documentation at /docs/config/listeners.html
 <Listener className="org.apache.catalina.security.SecurityListener" />
 -->
 <!--APR library loader. Documentation at /docs/apr.html -->
 <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
 <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-

howto.html -->
 <Listener className="org.apache.catalina.core.JasperListener" />
 <!-- Prevent memory leaks due to use of particular java/javax APIs-->
 <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
 <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
 <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
 <!-- Global JNDI resources
    Documentation at /docs/jndi-resources-howto.html
 -->
 <GlobalNamingResources>
  <!-- Editable user database that can also be used by
     UserDatabaseRealm to authenticate users
  -->
  <Resource name="UserDatabase" auth="Container"
       type="org.apache.catalina.UserDatabase"
       description="User database that can be updated and saved"
       factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
       pathname="conf/tomcat-users.xml" />
 </GlobalNamingResources>
 <!-- A "Service" is a collection of one or more "Connectors" that share
    a single "Container" Note: A "Service" is not itself a "Container",
    so you may not define subcomponents such as "Valves" at this level.
    Documentation at /docs/config/service.html
  -->
 <Service name="Catalina"> #定义一个Service命令为Catalina
  <!--The connectors can use a shared executor, you can define one or more named thread

pools-->
  <!--
  <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
    maxThreads="150" minSpareThreads="4"/>
  -->
  <!-- A "Connector" represents an endpoint by which requests are received
     and responses are returned. Documentation at :
     Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
     Java AJP Connector: /docs/config/ajp.html
     APR (HTTP/AJP) Connector: /docs/apr.html
     Define a non-SSL HTTP/1.1 Connector on port 8080
  -->
  <Connector port="8080" protocol="HTTP/1.1"
        connectionTimeout="20000"
        redirectPort="8443" /> #这里定义了一个连接器,协议为http,端口为8080,最大连接

超时为20s,这里还定义了一个SSL的重定向端口8443。我们可以根据需要进行修改。一般我

们都用80端口与443端口。
  <!-- A "Connector" using the shared thread pool-->
  <!--
  <Connector executor="tomcatThreadPool"
        port="8080" protocol="HTTP/1.1"
        connectionTimeout="20000"
        redirectPort="8443" />
  -->
  <!-- Define a SSL HTTP/1.1 Connector on port 8443
     This connector uses the JSSE configuration, when using APR, the
     connector should be using the OpenSSL style configuration
     described in the APR documentation -->
  <!--
  <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
        maxThreads="150" scheme="https" secure="true"
        clientAuth="false" sslProtocol="TLS" /> #这里定义了一个SSL的案例,主要定义相关密

钥与证书。
  -->
  <!-- Define an AJP 1.3 Connector on port 8009 -->
  <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> #这里定义了一个支持

AJP协议的连接器。
  <!-- An Engine represents the entry point (within Catalina) that processes
     every request. The Engine implementation for Tomcat stand alone
     analyzes the HTTP headers included with the request, and passes them
     on to the appropriate Host (virtual host).
     Documentation at /docs/config/engine.html -->
  <!-- You should set jvmRoute to support load-balancing via AJP ie :
  <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
  -->
  <Engine name="Catalina" defaultHost="localhost"> #这里定义了一个名为Catalina的引擎,

并定义了一个默认主机为localhost。
   <!--For clustering, please take a look at documentation at:
     /docs/cluster-howto.html (simple how to)
     /docs/config/cluster.html (reference documentation) -->
   <!--
   <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
   -->
   <!-- Use the LockOutRealm to prevent attempts to guess user passwords
      via a brute-force attack -->
   <Realm className="org.apache.catalina.realm.LockOutRealm">
    <!-- This Realm uses the UserDatabase configured in the global JNDI
       resources under the key "UserDatabase". Any edits
       that are performed against this UserDatabase are immediately
       available for use by the Realm. -->
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
        resourceName="UserDatabase"/>
   </Realm>
   <Host name="localhost" appBase="webapps"
      unpackWARs="true" autoDeploy="true"> #这里就是定义的虚拟主机。
    <!-- SingleSignOn valve, share authentication between web applications
       Documentation at: /docs/config/valve.html -->
    <!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    -->
    <!-- Access log processes all example.
       Documentation at: /docs/config/valve.html
       Note: The pattern used is equivalent to using pattern="common" -->
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
        prefix="localhost_access_log." suffix=".txt"
        pattern="%h %l %u %t &quot;%r&quot; %s %b" /> #这里定义了一个日志相关的属

性。
   </Host>
  </Engine>
 </Service>
</Server>

好了,在这里我们又简单的说明一下,配置文件下面我们就来具体演示一下怎么配置虚拟主机

首先,我们来修改一下配置文件,

[root@tomcat conf]# vim server.xml

#增加下面几行

<Host name="www.test.com" appBase="/web/webapp"
   unpackWARs="true" autoDeploy="true">
     <Context path="/" docBase="/web/webapp" reloadable="true"/>
</Host>

接下来我们来创建文档目录与测试页面,

[root@tomcat ~]# mkdir -pv /web/webapp
[root@tomcat ~]# cd /web/webapp
[root@tomcat webapp]# vim index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
 <head>
  <title>JSP test page.</title>
 </head>
 <body>
  <% out.println("Welcome to test. Site, http://www.test.com"); %>
 </body>
</html>

现在我们来测试一下我们修改的配置文件,

[root@tomcat ~]# service tomcat stop
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
^[[A[root@tomcat service tomcat configtest
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Sep 22, 2013 2:15:47 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in

production environments was not found on the java.library.path: 

/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Sep 22, 2013 2:15:47 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-80"]
Sep 22, 2013 2:15:47 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Sep 22, 2013 2:15:47 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1294 ms

注,大家可以看到,我们这里没有报错,说明配置都是正确的,若配置有错误,会在最后一行

提醒你。

再下面我们来启动Tomcat并测试一下,

[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp Active Internet connections (only servers) Proto Recv-Q

Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:*

LISTEN 1044/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1121/master tcp 0 0 

127.0.0.1:6010 0.0.0.0:* LISTEN 13368/sshd tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN

13387/sshd tcp 0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 13407/sshd tcp 0 0 :::80 :::* LISTEN

13557/java tcp 0 0 :::22 :::* LISTEN 1044/sshd tcp 0 0 ::1:25 :::* LISTEN 1121/master tcp

0 0 ::1:6010 :::* LISTEN 13368/sshd tcp 0 0 ::1:6011 :::* LISTEN 13387/sshd tcp 0 0

::1:6012 :::* LISTEN 13407/sshd tcp 0 0 :::8009 :::* LISTEN 13557/java 
注,还有一点得说明一下,我这里为了方便测试,将默认端口8080修改为了80端口。下面是

测试效果,

 

为了帮助大家理解,我们这里再次讲解一下,Host组件与Context组件以及相关属性。

Host组件:

位于Engine容器中用于接收请求并进行相应处理的主机或虚拟主机,如前面我们自定义的内容

<Host name="www.server110.com" appBase="/web/webapp"
   unpackWARs="true" autoDeploy="true">
     <Context path="/" docBase="/web/webapp" reloadable="true"/>
</Host>

常用属性说明:

name:定义虚拟主机的域名

appBase:此Host的webapps目录,即存放非归档的web应用程序的目录或归档后的WAR文件

的目录路径;可以使用基于$CATALINA_HOME的相对路径;

autoDeploy:在Tomcat处于运行状态时放置于appBase目录中的应用程序文件是否自动进行

deploy;默认为true;

unpackWars:在启用此webapps时是否对WAR格式的归档文件先进行展开;默认为true;

主机别名定义:

如果一个主机有两个或两个以上的主机名,额外的名称均可以以别名的形式进行定义,如下:

<Host name="www.test.com" appBase="webapps" unpackWARs="true">
 <Alias>web.test.com</Alias>
</Host>
Context组件:
Context在某些意义上类似于apache中的路径别名,一个Context定义用于标识tomcat实例中的

一个Web应用程序。如下面的定义:
  <!-- Tomcat Root Context -->
  <Context path="" docBase="/web/webapps"/>
  <!-- buzzin webapp -->
  <Context path="/bbs"
   docBase="/web/threads/bbs"
   reloadable="true">
  </Context>
  <!-- chat server -->
   <Context path="/chat" docBase="/web/chat"/>
  <!-- darian web -->
  <Context path="/darian" docBase="darian"/>

在Tomcat中,每一个context定义也可以使用一个单独的XML文件进行,其文件的目录为

$CATALINA_HOME/conf/<engine name>/<host name>。可以用于Context中的XML元素有

Loader,Manager,Realm,Resources和WatchedResource。

常用的属性定义有:

docBase:相应的Web应用程序的存放位置;也可以使用相对路径,起始路径为此Context所属

Host中appBase定义的路径;切记,docBase的路径名不能与相应的Host中appBase中定义的路

径名有包含关系,比如,如果appBase为deploy,而docBase绝不能为deploy-bbs类的名字;

path:相对于Web服务器根路径而言的URI;如果为空“”,则表示为此webapp的根路径;如果

context定义在一个单独的xml文件中,此属性不需要定义;

reloadable:是否允许重新加载此context相关的Web应用程序的类;默认为false;

为了便于大家理解,我们这里再定义一个Context并测试一下,

我们先来修改一下配置文件

[root@tomcat conf]# vim server.xml
<Host name="www.test.com" appBase="/web/webapp"
    unpackWARs="true" autoDeploy="true">
      <Context path="/" docBase="/web/webapp" reloadable="true"/>
      <Context path="/test" docBase="/web/test" reloadable="true"/> #增加这一行
 </Host>

下面来增加目录文档与测试文件,

[root@tomcat webapp]# mkdir /web/test
[root@tomcat webapp]# cd /web/test
[root@tomcat test]# vim index.jsp
<%@ page language="java" %>
<html>
 <head><title>TomcatA</title></head>
 <body>
  <h1><font color="red">TomcatA </h1>
  <table align="centre" >
   <tr>
    <td>Session ID</td>
  <% session.setAttribute("abc","abc"); %>
    <td><%= session.getId() %></td>
   </tr>
   <tr>
    <td>Created on</td>
    <td><%= session.getCreationTime() %></td>
   </tr>
  </table>
 </body>
</html>

测试一下配置文件是否有错并启动Tomcat,

[root@tomcat ~]# service tomcat configtest Using CATALINA_BASE: /usr/local/tomcat Using

CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using

JRE_HOME: /usr Using CLASSPATH: 

/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Sep 22, 2013 2:40:57

AM org.apache.catalina.core.AprLifecycleListener init INFO: The APR based Apache Tomcat

Native library which allows optimal performance in production environments was not found

on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib Sep 22,

2013 2:40:58 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler

["http-bio-80"] Sep 22, 2013 2:40:58 AM org.apache.coyote.AbstractProtocol init INFO:

Initializing ProtocolHandler ["ajp-bio-8009"] Sep 22, 2013 2:40:58 AM 

org.apache.catalina.startup.Catalina load INFO: Initialization processed in 1352 ms
[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp Active Internet connections (only servers) Proto Recv-Q

Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:*

LISTEN 1044/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1121/master tcp 0 0 

127.0.0.1:6010 0.0.0.0:* LISTEN 13587/sshd tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN

13387/sshd tcp 0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 13407/sshd tcp 0 0 :::80 :::* LISTEN

13945/java tcp 0 0 :::22 :::* LISTEN 1044/sshd tcp 0 0 ::1:25 :::* LISTEN 1121/master tcp

0 0 ::1:6010 :::* LISTEN 13587/sshd tcp 0 0 ::1:6011 :::* LISTEN 13387/sshd tcp 0 0

::1:6012 :::* LISTEN 13407/sshd tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 13945/java tcp 0

0 :::8009 :::* LISTEN 13945/java 
下面我们就用浏览器测试一下,

 

好了,到这里我们的Tomcat虚拟主机的讲解就到这里了,下面我们来说一下Tomcat图形管理

接口。

5.Tomcat图形管理接口

我们先来看一下默认的图形配置界面,

 

注,大家注意看右上角,我用红色方框标记出来的,大家可以看有三个按钮,分别为

Server Status 主要用来查看服务器的状态

Manager App 主要用来管理应用程序的部署及监控

Host Manager 主要用来管理虚拟主机

下面我们就来具休的配置一下,大家可以看到,你点击任何一个按钮都要输入用户名和密码的

,在我们配置之前我们先来说一下,Tomcat的Manager功能,

Manager的四个管理角色:

manager-gui - allows access to the HTML GUI and the status pages

manager-script - allows access to the text interface and the status pages

manager-jmx - allows access to the JMX proxy and the status pages

manager-status - allows access to the status pages only

注,这里我说一下,上面的英文比较简单我就不在里翻译了,大家自己看一下。

下面我们就来启用manager功能,修改tomcat-user.xml文件,添加如下行:

<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>

简单解释一下,Tomcat有内置的角色,我们这里增加了两个角色一个为manager-gui,另一个

为admin-gui,用户名和密码都为tomcat。

 

注,增加的内容一定要在<tomcat-users></tomcat-users>之间。不然,不会生效。好了,下

面我们一来测试一下配置文件,并重新启动一下Tomcat

[root@tomcat ~]# service tomcat configtest Using CATALINA_BASE: /usr/local/tomcat Using

CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using

JRE_HOME: /usr Using CLASSPATH: 

/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Sep 22, 2013 3:08:44

AM org.apache.catalina.core.AprLifecycleListener init INFO: The APR based Apache Tomcat

Native library which allows optimal performance in production environments was not found

on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib Sep 22,

2013 3:08:44 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler

["http-bio-80"] Sep 22, 2013 3:08:44 AM org.apache.coyote.AbstractProtocol init INFO:

Initializing ProtocolHandler ["ajp-bio-8009"] Sep 22, 2013 3:08:44 AM 

org.apache.catalina.startup.Catalina load INFO: Initialization processed in 1213 ms
[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp Active Internet connections (only servers) Proto Recv-Q

Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:*

LISTEN 1044/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1121/master tcp 0 0 

127.0.0.1:6010 0.0.0.0:* LISTEN 13587/sshd tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN

13387/sshd tcp 0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 13407/sshd tcp 0 0 :::80 :::* LISTEN

14197/java tcp 0 0 :::22 :::* LISTEN 1044/sshd tcp 0 0 ::1:25 :::* LISTEN 1121/master tcp

0 0 ::1:6010 :::* LISTEN 13587/sshd tcp 0 0 ::1:6011 :::* LISTEN 13387/sshd tcp 0 0

::1:6012 :::* LISTEN 13407/sshd tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 14197/java tcp 0

0 :::8009 :::* LISTEN 14197/java 
好了,下面我们用浏览器查看一下,

注,点击Server Status按钮,让你输入用户名和密码。我这里全部设置是tomcat。

 

然后,会出现以下界面。显示全部服务器运行状态!大家可以仔细的看一下,我就不带着大家

看了。

 

下面是应用程序部署管理界面,

 

下面是虚拟主机管理页面,

 

注,我们一般在生产环境中用的最多是应用程序部署界面,可以进行热布署应用程序,很方便

,大家可以尝试一下。好了,图形管理界面我们就说到这里了,下面我们来说一下,Tomcat

的一个小案例。我们说了那么多,有朋友就会说了,怎么一个案例也没有呢?下面我们就来布

署一个社交网站的案例JavaCenter Home。

6.部署JSP网站案例

首页,我们来修改一下配置文件,

[root@tomcat conf]# vim server.xml
<Host name="www.test.com" appBase="/web"
   unpackWARs="true" autoDeploy="true">
 <Context path="/" docBase="webapp" reloadable="true"/>
</Host>

注,增加一下虚拟主机,文件目录为/web/webapp。

下面我们来解压一下我们下载好的JavaCenter Home网站程序,

[root@tomcat src]# tar xf JavaCenter_Home_2.0_GBK.tar.bz2

接下来将解压好的JavaCenter Home程序移动到/web/webapp下,

[root@tomcat src]# cd JavaCenter_Home_2.0_GBK
[root@tomcat JavaCenter_Home_2.0_GBK]# mv * /web/webapp/

下面我们来测试一下配置文件并启动Tomcat服务,

[root@tomcat ~]# service tomcat configtest
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr/java/jdk1.6.0_21
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Sep 23, 2013 5:31:18 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in

production environments was not found on the java.library.path: 

/usr/java/jdk1.6.0_21/jre/lib/amd64/server:/usr/java/jdk1.6.0_21/jre/lib/amd64:/usr/java/jdk

1.6.0_21/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Sep 23, 2013 5:31:20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-80"]
Sep 23, 2013 5:31:20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Sep 23, 2013 5:31:20 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2493 ms
[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr/java/jdk1.6.0_21
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name
tcp    0   0 0.0.0.0:22         0.0.0.0:*          LISTEN   1026/sshd
tcp    0   0 127.0.0.1:25        0.0.0.0:*          LISTEN   1256/master
tcp    0   0 127.0.0.1:6010       0.0.0.0:*          LISTEN   1339/sshd
tcp    0   0 0.0.0.0:3306        0.0.0.0:*          LISTEN   1165/mysqld
tcp    0   0 :::80            :::*            LISTEN   1499/java
tcp    0   0 :::22            :::*            LISTEN   1026/sshd
tcp    0   0 ::1:25           :::*            LISTEN   1256/master
tcp    0   0 ::1:6010          :::*            LISTEN   1339/sshd
tcp    0   0 :::8009           :::*            LISTEN   1499/java

下面我们用浏览器访问一下,http://www.test.com(注,要想用域名访问,必须配置本机有

hosts文件,Windows7hosts文件目录,C:\Windows\System32\drivers\etc\hosts)

 

注,上面的错误说明我们连接MySQL数据库失败。因为我们这里还没有安装与配置嘛。下面我

们赶快来配置一下,

先安装数据库,我们这里就用yum安装一下,

[root@tomcat ~]# yum install -y mysql-server

下面来启动并配置mysql,

[root@tomcat ~]# service mysqld start
Starting mysqld:                      [ OK ]
[root@tomcat ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.69 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

好了,到这里我们mysql就安装完成了,下面我们来看一下我们的程序目录,

[root@tomcat ~]# cd /web/webapp/
[root@tomcat webapp]# ls
admin    avatar.jsp     data     help.jsp  js.jsp    rss.jsp  userapp.jsp
admincp.jsp config.properties do.jsp    image    link.jsp   source   WEB-INF
api     contact.jsp    editor.jsp  index.jsp  magic.jsp  space.jsp xmlrpc.jsp
app.jsp   cp.jsp       errors    install   META-INF   template
attachment  crossdomain.xml  favicon.ico invite.jsp network.jsp theme

大家可以看到里面有个install的目录,下面我们用浏览器访问一下,

http://www.test.com/install,会跳出一个安装界面,如下图

 

从图中,我们可以看出,所以环境配置完成,都符合要求。下面我们点击“接受授权协议,开

始安装JavaCenter Home”,会跳出下一界面,如下图

 

从图中,我们可以看出得输入,数据库名称、数据库用户名、数据库密码。下面我们就来增加

一下,

[root@tomcat ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.69 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database jcenter;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on jcenter.* to jcenter@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on jcenter.* to jcenter@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

上面创建一个jcenter数据库,授权访问一个jcenter用户,密码为123456。下面我们继续配置

 

我们输入刚才设置的数据库用户名、数据库密码、数据库名称。点击“设置完毕,检测我的数

据库配置”,图显示的3和4会看自动进行安装,我们只等一会即可。安装完成的效果如下图,

 

下面我们开通一个管理员空间,用户名和密码都为admin,如下图

 

点击“开通管理员空间”,会跳出另一个界面,如下图

 

我们点击“进入空间首页”,效果如下图

 

好了,到这里我们的JavaCenter Home就全部配置完成了,我们第一阶段的基本配置就这里全

部完成,下面我们主要讲解Nginx反向代理Tomcat服务器。

二、Nginx反向代理Tomcat服务器

0.测试环境准备阶段

下面先看一下实验拓扑,

 

接着来同步各节点时间,

[root@tomcat ~]# ntpdate 202.120.2.101
[root@nginx ~]# ntpdate 202.120.2.101

下面我们来安装nginx服务器,首先来解决nginx的依赖关系,

[root@nginx ~]# yum groupinstall -y "Development Tools" "Server Platform Deveopment"
[root@nginx ~]# yum install -y openssl-devel pcre-devel

下面我们来新建nginx用户,

[root@nginx ~]# groupadd -r -g 108 nginx
[root@nginx ~]# useradd -r -g 108 -u 108 nginx
[root@nginx ~]# id nginx
uid=108(nginx) gid=108(nginx) 组=108(nginx)

接着我们来开始编译和安装,

[root@nginx src]# tar xf nginx-1.4.2.tar.gz
[root@nginx src]# cd nginx-1.4.2
[root@nginx nginx-1.4.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@nginx nginx-1.4.2]# ./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
[root@nginx nginx-1.4.2]# make && make install

说明:

Nginx可以使用Tmalloc(快速、多线程的malloc库及优秀性能分析工具)来加速内存分配,使用

此功能需要事先安装gperftools,而后在编译nginx添加--with-google_perftools_module选项即

可。

如果想使用nginx的perl模块,可以通过为configure脚本添加--with-http_perl_module选项来实

现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里

不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网

上的文档。

下面我们为nginx提供SysV init脚本,

[root@nginx ~]# cat /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:  - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
#        proxy and IMAP/POP3 proxy server
# processname: nginx
# config:   /etc/nginx/nginx.conf
# config:   /etc/sysconfig/nginx
# pidfile:   /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
  # make required directories
  user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=[].*/\1/g'

-`
  options=`$nginx -V 2>&1 | grep 'configure arguments:'`
  for opt in $options; do
    if [ `echo $opt | grep '.*-temp-path'` ]; then
      value=`echo $opt | cut -d "=" -f 2`
      if [ ! -d "$value" ]; then
        # echo "creating" $value
        mkdir -p $value && chown -R $user $value
      fi
    fi
  done
}
start() {
  [ -x $nginx ] || exit 5
  [ -f $NGINX_CONF_FILE ] || exit 6
  make_dirs
  echo -n $"Starting $prog: "
  daemon $nginx -c $NGINX_CONF_FILE
  retval=$?
  echo
  [ $retval -eq 0 ] && touch $lockfile
  return $retval
}
stop() {
  echo -n $"Stopping $prog: "
  killproc $prog -QUIT
  retval=$?
  echo
  [ $retval -eq 0 ] && rm -f $lockfile
  return $retval
}
restart() {
  configtest || return $?
  stop
  sleep 1
  start
}
reload() {
  configtest || return $?
  echo -n $"Reloading $prog: "
  killproc $nginx -HUP
  RETVAL=$?
  echo
}
force_reload() {
  restart
}
configtest() {
 $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
  status $prog
}
rh_status_q() {
  rh_status >/dev/null 2>&1
}
case "$1" in
  start)
    rh_status_q && exit 0
    $1
    ;;
  stop)
    rh_status_q || exit 0
    $1
    ;;
  restart|configtest)
    $1
    ;;
  reload)
    rh_status_q || exit 7
    $1
    ;;
  force-reload)
    force_reload
    ;;
  status)
    rh_status
    ;;
  condrestart|try-restart)
    rh_status_q || exit 0
      ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-

reload|configtest}"
    exit 2
esac

而后为此脚本赋予执行权限,

[root@nginx ~]# chmod +x /etc/init.d/nginx

添加至服务管理列表,并让其开机自动启动,

[root@nginx ~]# chkconfig --add nginx
[root@nginx ~]# chkconfig nginx on

而后就可以启动服务并测试了,

[root@nginx ~]# service nginx start
正在启动 nginx:                      [确定]
[root@nginx ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name
tcp    0   0 0.0.0.0:80         0.0.0.0:*          LISTEN   14006/nginx
tcp    0   0 0.0.0.0:22         0.0.0.0:*          LISTEN   1029/sshd
tcp    0   0 127.0.0.1:25        0.0.0.0:*          LISTEN   1105/master
tcp    0   0 127.0.0.1:6010       0.0.0.0:*          LISTEN   1345/sshd
tcp    0   0 :::22            :::*            LISTEN   1029/sshd
tcp    0   0 ::1:25           :::*            LISTEN   1105/master
tcp    0   0 ::1:6010          :::*            LISTEN   1345/sshd

下面是测试结果,

 

好了,到这里我们准备工作就全部完成了,下面们来简单的配置一下Nginx反向代理Tomcat服

务器。

1.Nginx将请求反向代理到后端Tomcat

首先,我们来修改一下nginx的配置文件,

[root@nginx ~]# cd /etc/nginx/
[root@nginx nginx]# cp nginx.conf nginx.conf.bak
[root@nginx nginx]# vim nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid    logs/nginx.pid;
events {
  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"';
  #access_log logs/access.log main;
  sendfile    on;
  #tcp_nopush   on;
  #keepalive_timeout 0;
  keepalive_timeout 65;
  #gzip on;
  server {
    listen    80;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      #root  html;
      #index index.html index.htm;
      proxy_pass http://192.168.18.201/; #注释默认两行,新增一行。
    }
    #error_page 404       /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }
  }
}

重新加载一下配置文件,

[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:                      [确定]

下面进行测试一下,(注,首先你得保证你的tomcat服务器能正常访问,下面我们先来访问一

下tomcat服务器,如下图)

 

大家可以看到我们的tomcat服务器可以正常访问,下面我们来看测试一下nginx可不可以进行

反向代理。(注,还有问题,这里为了方便测试我们先将tomcat的默认主机设置为

www.test.com)

[root@tomcat ~]# vim /usr/local/tomcat/conf/server.xml
  <Engine name="Catalina" defaultHost="www.test.com">

重新启动一下tomcat并测试,

[root@tomcat ~]# service tomcat stop
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr/java/jdk1.6.0_21
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# service tomcat start
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr/java/jdk1.6.0_21
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name
tcp    0   0 0.0.0.0:22         0.0.0.0:*          LISTEN   1026/sshd
tcp    0   0 127.0.0.1:25        0.0.0.0:*          LISTEN   1256/master
tcp    0   0 127.0.0.1:6010       0.0.0.0:*          LISTEN   1339/sshd
tcp    0   0 127.0.0.1:6011       0.0.0.0:*          LISTEN   2744/sshd
tcp    0   0 0.0.0.0:3306        0.0.0.0:*          LISTEN   2382/mysqld
tcp    0   0 :::80            :::*            LISTEN   3299/java
tcp    0   0 :::22            :::*            LISTEN   1026/sshd
tcp    0   0 ::1:25           :::*            LISTEN   1256/master
tcp    0   0 ::1:6010          :::*            LISTEN   1339/sshd
tcp    0   0 ::1:6011          :::*            LISTEN   2744/sshd
tcp    0   0 :::8009           :::*            LISTEN   3299/java

下面我们来访问测试一下tomcat服务器,

 

下面我们来测试看nginx是否能代理tomcat服务器,

 

好了,大家可以看到我们成功设置了nginx反向代理tomcat服务器。好了,大家可以看到,我

们网站上有很多的图片,每次访问都要去后端的tomcat服务器上去取,很消耗服务器资源。我

们下面将设置在nginx服务器上缓存图片。

2.Nginx将图片缓存到本地

同样的,我们先来修改配置文件,

[root@nginx nginx]# cat nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid    logs/nginx.pid;
events {
  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"';
  #access_log logs/access.log main;
  sendfile    on;
  #tcp_nopush   on;
  #keepalive_timeout 0;
  keepalive_timeout 65;
  #gzip on;
  proxy_cache_path /nginx/cache levels=1:2 keys_zone=first:10m inactive=24h 

max_size=1G; #新建缓存路径与相关属性
  upstream backend { #建立后端tomcat服务器
  server 192.168.18.201 weight=1;
  }
  server {
    listen    80;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      #root  html;
      #index index.html index.htm;
      #proxy_pass http://192.168.18.201/; #注释原来的代理设置
      proxy_pass http://backend/; #启动后端服务器
    }
  location ~* "\.(jpg|jpeg|png|gif|html|css|js)$" { #缓存图片与静态内容
    proxy_pass http://backend;
    proxy_cache first;
    proxy_cache_valid 200 24h; #200状态缓存24小时
    proxy_cache_valid 302 10m; #302状态缓存10分钟
    add_header X-Cache-Status $upstream_cache_status; #在http头部增加一个字段显示是否

命令缓存
  }
    #error_page 404       /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }
  }
}

下面我们来新建缓存目录,

[root@nginx ~]# mkdir -pv /nginx/cache
mkdir: 已创建目录 "/nginx"
mkdir: 已创建目录 "/nginx/cache"

测试一下配置文件是否有错,

[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载配置文件,

[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:                      [确定]

那么下面我们就来测试一下,

 

大家可以看到我们访问的所有的静态内容都是命中的,X-Cache-Status: HIT,下面们来看一下

缓存的目录,

[root@nginx ~]# cd /nginx/cache/
[root@nginx cache]# ls
0 1 2 3 4 5 6 7 8 9 b c d e

大家可以看到,缓存目录当中有我们缓存的内容,好了到这里我们的nginx缓存服务就配置完

成了,下面我们看一下如何实现动静分离。

3.Nginx将请求实现动静分离

首先,我们来说一下我们要实现的效果,上面我们已经将静态内容缓存在nginx服务器上,我

们想让用户请求的静态内容到nginx去取,动态内容到tomcat服务器上去取,这就能实现动静

分享效果。同样的首先我们来修改配置文件,

[root@nginx nginx]# cat nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid    logs/nginx.pid;
events {
  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"';
  #access_log logs/access.log main;
  sendfile    on;
  #tcp_nopush   on;
  #keepalive_timeout 0;
  keepalive_timeout 65;
  #gzip on;
  proxy_cache_path /nginx/cache levels=1:2 keys_zone=first:10m inactive=24h 

max_size=1G;
  upstream backend {
  server 192.168.18.201 weight=1;
  }
  server {
    listen    80;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
  index index.jsp index.html;
    location ~* "\.(jsp|do)$" { #当请求的是jsp或do文件时直接到tomcat上去取
      #root  html;
      #index index.html index.htm;
      #proxy_pass http://192.168.18.201/;
      #proxy_pass http://backend/;
      proxy_pass http://backend;
    }
  location = / {
    root html;
    rewrite ^/ http://192.168.18.201/index.jsp last;
  }
  location ~* "\.(jpg|jpeg|png|gif|html|css|js)$" {
    proxy_pass http://backend;
    proxy_cache first;
    proxy_cache_valid 200 24h;
    proxy_cache_valid 302 10m;
    add_header X-Cache-Status $upstream_cache_status;
  }
    #error_page 404       /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }
  }
}

下面我们来检查一下配置文件是否有误,

[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载一下配置文件,

[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:                      [确定]

下面我们来访问测试一下,

 

大家可以看到我们的静态内容来自缓存,动态内容全部代理到后端的tomcat服务器上了,说明

我们动态分离配置完成,好了到这里我们的tomcat的基本配置与nginx反向代理tomcat的配置

就全部完成了,



Nginx502 bad gateway, 504 Gateway Time-out的彻解决方法 


VPS是256M的内存,CPU是四核心的,所以更多的我会在乎内存。而在我调试服务器的时候通

常会遇到Nginx502 bad gateway和504 Gateway Time-out的错误。分析nginx.conf我发现

server和fastcgi的buffers过多,导致fastcgi请求的数量过大,php-fpm无法及时处理而出错。

循此思路我们可以再总体buffers不变的情况下减少请求数量,具体的ningx.conf改动细节如下


                server_names_hash_bucket_size 128;
                client_header_buffer_size 32k;
                large_client_header_buffers 1 128k;# 4 32k
                client_max_body_size 8m;

                sendfile on;
                tcp_nopush     on;

                keepalive_timeout 60;

                tcp_nodelay on;

                fastcgi_connect_timeout 300;
                fastcgi_send_timeout 300;
                fastcgi_read_timeout 300;
                fastcgi_buffer_size 128k;
                fastcgi_buffers 2 256k;#8 128
                fastcgi_busy_buffers_size 256k;
                fastcgi_temp_file_write_size 256k;
                fastcgi_intercept_errors on;

                gzip on;
                gzip_min_length  1k;
                gzip_buffers     1 64k; #4 16
                gzip_http_version 1.0;
                gzip_comp_level 2;
                gzip_types       text/plain application/x-javascript text/css application/xml;
                gzip_vary on;

另外,php-fpm的默认静态处理方式会使得php-cgi的进程长期占用内存而无法释放,这也是导

致nginx出错的原因之一,因此可以将php-fpm的处理方式改成apache模式。
        <value name=”style”>apache-like</value>

从更改完毕到现在的测试表明上述方式的效果还是很明显的,并没有发现一次Nginx502 bad 

gateway或504 Gateway Time-out错误。当然,如果你的VPS或者服务器的性能足够好可以根

据具体情况不必做无谓的改动。

一般看来, 这种情况可能是由于nginx默认的fastcgi进程响应的缓冲区太小造成的, 这将导致

fastcgi进程被挂起, 如果你的fastcgi服务对这个挂起处理的不好, 那么最后就极有可能导致504 

Gateway Time-out
现在的网站, 尤其某些论坛有大量的回复和很多内容的, 一个页面甚至有几百K
默认的fastcgi进程响应的缓冲区是8K, 我们可以设置大点
在nginx.conf里, 加入:


fastcgi_buffers 8 128k 

这表示设置fastcgi缓冲区为8×128k
当然如果您在进行某一项即时的操作, 可能需要nginx的超时参数调大点, 例如设置成60秒:

send_timeout 60;

我只是调整了这两个参数, 结果就是没有再显示那个超时, 可以说效果不错, 但是也可能是由于

其他的原因, 目前关于nginx的资料不是很多, 很多事情都需要长期的经验累计才有结果, 期待您

的发现哈!

 

 

 

Nginx 502 Bad Gateway的含义是请求的PHP-CGI已经执行,但是由于某种原因(一般是读取

资源的问题)没有执行完毕而导致PHP-CGI进程终止。
Nginx 504 Gateway Time-out的含义是所请求的网关没有请求到,简单来说就是没有请求到可

以执行的PHP-CGI。

解决这两个问题其实是需要综合思考的,一般来说Nginx 502 Bad Gateway和php-fpm.conf的

设置有关,而Nginx 504 Gateway Time-out则是与nginx.conf的设置有关。
而正确的设置需要考虑服务器自身的性能和访客的数量等多重因素。
以我目前的服务器为例子CPU是奔四1.5G的,内存1GB,CENTOS的系统,访客大概是50人左

右同时在线。
但是在线的人大都需要请求PHP-CGI进行大量的信息处理,因此我将nginx.conf设置为:
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 128k;#8 128
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
这里最主要的设置是前三条,即
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
这里规定了PHP-CGI的连接、发送和读取的时间,300秒足够用了,因此我的服务器很少出现

504 Gateway Time-out这个错误。最关键的是php-fpm.conf的设置,这个会直接导致502 Bad 

Gateway和504 Gateway Time-out。
下面我们来仔细分析一下php-fpm.conf几个重要的参数:
php-fpm.conf有两个至关重要的参数,一个是”max_children”,另一个是”

request_terminate_timeout”
我的两个设置的值一个是”40″,一个是”900″,但是这个值不是通用的,而是需要自己计算的


计算的方式如下:
如果你的服务器性能足够好,且宽带资源足够充足,PHP脚本没有系循环或BUG的话你可以直

接将”request_terminate_timeout”设置成0s。0s的含义是让PHP-CGI一直执行下去而没有时间

限制。而如果你做不到这一点,也就是说你的PHP-CGI可能出现某个BUG,或者你的宽带不够

充足或者其他的原因导致你的PHP-CGI能够假死那么就建议你给”request_terminate_timeout”

赋一个值,这个值可以根据你服务器的性能进行设定。一般来说性能越好你可以设置越高,20

分钟-30分钟都可以。由于我的服务器PHP脚本需要长时间运行,有的可能会超过10分钟因此

我设置了900秒,这样不会导致PHP-CGI死掉而出现502 Bad gateway这个错误。

而”max_children”这个值又是怎么计算出来的呢?这个值原则上是越大越好,php-cgi的进程多

了就会处理的很快,排队的请求就会很少。设置”max_children”也需要根据服务器的性能进行

设定,一般来说一台服务器正常情况下每一个php-cgi所耗费的内存在20M左右,因此我的”

max_children”我设置成40个,20M*40=800M也就是说在峰值的时候所有PHP-CGI所耗内存在

800M以内,低于我的有效内存1Gb。而如果我的”max_children”设置的较小,比如5-10个,那

么php-cgi就会“很累”,处理速度也很慢,等待的时间也较长。如果长时间没有得到处理的请求

就会出现504 Gateway Time-out这个错误,而正在处理的很累的那几个php-cgi如果遇到了问

题就会出现502 Bad gateway这个错误。



nginx + keepalive 实现高可用负载均衡 

一、拓扑环境:

主nginx负载均衡器 192.168.166.203 
辅nginx负载均衡器 192.168.166.177
VIP地址 192.168.166.178
http://www.keepalived.org/download.html
tar -zxvf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1
./configure --prefix=/usr/local/keepalived --sysconf=/etc
make && make install

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /bin/
chkconfig --add keepalived
chmod 755 /etc/init.d/keepalived
chkconfig keepalived on
service keepalived start
二.修改配置文件为以下内容: [master slave]
MASTER
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
xxx@xxx.com
}
notification_email_from xxx@xxx.com
smtp_server xxx.smtp.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script Monitor_Nginx {
script "/root/monitor_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER #(主机为MASTER,备用机为BACKUP)
interface eth0 #(HA监测网络接口)

virtual_router_id 61 #(主、备机的virtual_router_id必须相同)
#mcast_src_ip 192.168.166.203 #(多播的源IP,设置为本机外网IP,与VIP同一网卡)此项可

不设置
priority 90 #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)
advert_int 1 #(VRRP Multicast广播周期秒数)
authentication {
auth_type PASS #(VRRP认证方式)
auth_pass 1234 #(密码)
}

track_script {
Monitor_Nginx #(调用nginx进程检测脚本)
}
virtual_ipaddress {
192.168.166.178 #(VRRP HA虚拟地址)
}
}

###BACKUP方面只需要修改state为BACKUP , priority比MASTER稍低就OK了
三:注:monitor_nginx.sh为监控nginx进程的脚本,内容如下
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/usr/local/nginx/sbin/nginx
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall keepalived
fi
fi
#chmod +x monitor_nginx.sh
四.启动keepalived
service keepalived start

/usr/local/keepalived/sbin/keepalived -D -f /etc/keepalived/keepalived.conf
五.测试步骤
1. 访问VIP看是否能够正常访问后端的web
2. 停止其中一个web看是否能将访问转到另一台上
3. 停止两台nginx上任何一个nginx进程看监控进程脚本是否会自动启动nginx
4. 停止任何一台nginx上的keepalived进程看另一台是否接管vip

track_interface的意思是将Linux中你想监控的网络接口卡监控起来,当其中的一块出现故障是

keepalived都将视为路由器出现故障。
这里请注意: virtual_router_id 61,同一组master/backup中,也就是如果你设置了多个VRRP

,同一组这个ID必须相同,不然启动的时候主从都会把VIP给启动了
keepalived 通过发送和接收组播包中的同一个virtual_router_id 的中的成员的存活,来确定对

方的不可用,一旦检测到对方的不可用,即会切换它的备份角色为主。
即:当真实机192.168.166.177上的keepalived 检测到 真实机192.168.166.203上的keepalived 

不可用时,177上将使用vip:192.168.166.178对外服务并由backup角色转变为master

keepalived+nginx实现 HA与负载均衡中keepalived负责实现HA功能,控制VIP(虚拟网络地址)

转移功能,nginx实现对realserver七层负载均衡功能,nginx提供代理服务和cache功能,DNS

服务可以负责前段VIP的负载均衡。

 


keepalived的配置
192.168.200.1 keepalived配置
! Configuration File for keepalived

global_defs {
notification_email {
xxx@xxx.com
}
notification_email_from xxx@xxx.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_TEST_YDQ
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 61
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.101/24 dev eth0 label eth0:0
}
}

vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 62
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.200.102/24 dev eth0 label eth0:1
}
}


vrrp_instance VI_3 {
state BACKUP
interface eth0
virtual_router_id 63
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 3333
}
virtual_ipaddress {
192.168.200.103/24 dev eth0 label eth0:1
}
}

nginx配置 :
user nobody nobody;                                                          
worker_processes 1;                                                           
                                                                              
#error_log logs/error.log;                                                    
#error_log logs/error.log notice;                                             
error_log logs/error.log debug;                                               
                                                                              
pid logs/nginx.pid;                                                           
                                                                              
                                                                              
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"';                                 
                                                                              
access_log logs/access.log main;                                              
                                                                              
sendfile on;                                                                  
#tcp_nopush on;                                                               
                                                                              
#keepalive_timeout 0;                                                         
keepalive_timeout 65;                                                         
                                                                              
gzip on;                                                                      
gzip_min_length 1k;                                                           
gzip_buffers 6 16k;                                                           
gzip_http_version 1.1;                                                        
gzip_comp_level 2;                                                            
gzip_types text/plain application/x-javascript text/css application/xml;      
gzip_vary on;                                                                 
# location / {                                                                
# root html;                                                                  
# index index.html index.htm;                                                 
# }                                                                           
error_page 404 /404.html;                                                     
                                                                              
upstream www.test.com {                                                      
server 192.168.11.37:80;                                                      
server 192.168.11.38:80;                                                      
}                                                                             
                                                                              
server                                                                        
{                                                                             
listen 80;                                                                    
server_name www.test.com 192.168.166.178;                                    
index index.htm index.html;                                                   
#root /web/wwwroot;                                                           
                                                                              
location / {                                                                  
proxy_pass http://www.test.com;                                              
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;  
proxy_set_header Host $host;                                                  
proxy_set_header X-Real-IP $remote_addr;                                      
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                  
include proxy.conf;                                                           
}                                                                             
log_format blog_test_com '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '                                 
'"$http_user_agent" "$http_x_forwarded_for"';                                 
access_log logs/blog_test_com;                                                
}                                                                             
}                                            

0
0
   

Docker知识库

相关内容

    暂无相关文章