- # groupadd -r nginx
- # useradd -r -g nginx –s /bin/false -M nginx
- # tar xf nginx-1.2.3.tar.gz
- # cd nginx-1.2.3
- # ./configure \
- --prefix=/usr \ //指定nginx安装目录
- --sbin-path=/usr/sbin/nginx \ //指定nginx可执行二进制文件存放目录
- --conf-path=/etc/nginx/nginx.conf \ //指定nginx配置文件所在目录
- --error-log-path=/var/log/nginx/error.log \ //指定nginx错误日志所在目录
- --http-log-path=/var/log/nginx/access.log \ //指定HTTP访问日志文件目录
- --pid-path=/var/run/nginx/nginx.pid \ //指定nginx的PID文件所在目录
- --lock-path=/var/lock/nginx.lock \ //指定nginx的lock文件所在目录
- --user=nginx \ //为nginx工作进程指定非特权用户
- --group=nginx \ //为nginx工作进程指定非特权用户组
- --with-http_ssl_module \ //加载ngx_http_ssl_module模块,使其支持ssl功能
- --with-http_flv_module \ //加载ngx_http_flv_module模块
- --with-http_stub_status_module \ //加载ngx_http_stub_status_module模块
- --with-http_gzip_static_module \ //加载ngx_http_gzip_static_module模块
- --http-client-body-temp-path=/var/tmp/nginx/client/ \ //指定HTTP客户端请求主体临时存放目录
- --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ //指定HTTP代理临时文件存放目录
- --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ //指定HTTP的Fastcgi临时文件存放目录
- --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ //指定HTTP的Uwsgi临时文件存放目录
- --http-scgi-temp-path=/var/tmp/nginx/scgi \ //指定HTTP的Scgi临时文件存放目录
- --with-pcre //强制支持pcre库
- # make && make install
- 新建文件/etc/rc.d/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
- # chmod +x /etc/rc.d/init.d/nginx
- # chkconfig --add nginx
- # chkconfig nginx on
- # service nginx start
- 1)安装apache:
- # yum -y install httpd
- 2)安装php
- # yum -y install php
- 添加php测试页面
- # vim /var/www/html/index.php
- 内容如下:
- <?php
- phpinfo()
- ?>
- 重启httpd服务并访问试试
- # service httpd restart
- #root html;
- #index index.html index.htm;
添加如下行,制定代理的web服务器地址
- proxy_pass http://172.16.11.21;
- # service nginx configtest
- # service nginx restart
- # ab -n 10000 -c 100 http://172.16.11.11/index.php
- Document Path: /index.php
- Document Length: 38239 bytes
- Concurrency Level: 100
- Time taken for tests: 154.738780 seconds
- Complete requests: 10000
- Failed requests: 0
- Write errors: 0
- Total transferred: 384000000 bytes
- HTML transferred: 382390000 bytes
- Requests per second: 64.63 [#/sec] (mean) //每秒钟响应64个请求//
- Time per request: 1547.388 [ms] (mean) //每个请求用时1547毫秒//
- Time per request: 15.474 [ms] (mean, across all concurrent requests) //并发请求100个用时15秒//
- Transfer rate: 2423.44 [Kbytes/sec] received //平均传输速率2423kb/s//
- 在其他另外的主机上执行如下命令通过直接访问web页面进行压力测试:
- # ab -n 10000 -c 100 http://172.16.11.21/index.php
- Document Path: /index.php
- Document Length: 38025 bytes
- Concurrency Level: 100
- Time taken for tests: 69.886173 seconds
- Complete requests: 10000
- Failed requests: 0
- Write errors: 0
- Total transferred: 382159368 bytes
- HTML transferred: 380437992 bytes
Requests per second: 143.09 [#/sec] (mean) //每秒钟响应143个请求//- Time per request: 698.862 [ms] (mean) //每个请求用时698毫秒//
- Time per request: 6.989 [ms] (mean, across all concurrent requests) //并发请求100个用时6秒//
- Transfer rate: 5340.14 [Kbytes/sec] received //平均传输速率5340kb/s//
通过以上压力测试对比发现,尽管采用nginx实现反向代理但nginx没有缓存功能,页面响应速度不升反而会大大降低;
为nginx反代服务器提供缓存;
- 编辑nginx的主配置文件,找到http选项,添加如下内容:
- proxy_cache_path /var/www/cache levels=1:2 keys_zone=mycache:20m; //定义缓存路径,缓存级别和内存键区域名称及空间大小//
- max_size=2048m inactive=60m; //最大缓存空间大小和非活动时长(超过该时长缓存过期)//
- proxy_temp_path /var/www/cache/tmp; //代理临时目录,需要事先创建//
- 找到Location选项,添加如下内容:
- proxy_pass http://172.16.11.21; //制定反代服务器//
- proxy_cache mycache; //定义缓存名称,要与上述内存键区域名称保持一致//
- proxy_cache_valid 200 302 60m; //定义请求页面响应码为200和302的页面缓存时长为60分钟//
- proxy_cache_valid 404 1m; //定义请求页面响应码为404的页面缓存时长为1分钟//
- 创建缓存目录
- # mkdir /var/www/cache/tmp -pv
- 检查nginx配置文件语法并重启nginx服务
- # service nginx configtest
- # service nginx restart
- 首先在其他另外的主机上执行几次如下命令通过nginx代理进行压力测试使缓存中充满访问数据;
- # ab -n 1000 -c 20 http://172.16.11.11/index.php
- 然后利用上述同样的测试方法通过nginx代理进行压力测试
- # ab -n 10000 -c 100 http://172.16.11.11/index.php
- Document Path: /index.php
- Document Length: 38239 bytes
- Concurrency Level: 100
- Time taken for tests: 79.248259 seconds
- Complete requests: 10000
- Failed requests: 0
- Write errors: 0
- Total transferred: 384478146 bytes
- HTML transferred: 382865409 bytes
- Requests per second: 126.19 [#/sec] (mean) //每秒钟响应126个请求//
- Time per request: 792.483 [ms] (mean) //每个请求用时792毫秒//
- Time per request: 7.925 [ms] (mean, across all concurrent requests) //并发请求100个用时7秒//
- Transfer rate: 4737.85 [Kbytes/sec] received //平均传输速率4737kb/s//
通过上述对nginx反向代理进行压力测试前后对比发现,自从引入了nginx的缓存功能大大提升了页面响应速度,同时也很好的提升的用户的感知度;
以上就是nginx反向代理引入缓存功能的介绍;
- location / { //定义访问路径为”/”时直接在nginx服务器响应//
- root html;
- index index.html index.htm;
- }
- location /images/ { //定义访问路径为”/images/”时代理到Apache服务器响应//
- proxy_pass http://172.16.11.21/images/;
- proxy_cache mycache;
- proxy_cache_valid 200 302 60m;
- proxy_cache_valid 404 1m;
- }
- # mkdir /var/www/html/images
- # vim /var/www/html/images/index.html
- 添加如下内容:
- <h1>apache</h1>
- 重启nginx和后端apache服务器
- # service nginx restart
- # service httpd restart
- upstream cluster { //定义后端负载均衡服务器名称//
- server 172.16.11.21 weight=1; //指定后端服务器和权重//
- server 172.16.11.22 weight=2;
- }
- 在server选项下添加如下内容
- location / {
- proxy_pass http://cluster; //反代指向定义的负载均衡服务器//
- }
- server {
- listen 80;
- server_name jump.magedu.com;
- index index.html index.php;
- root /www/htdocs;
- rewrite ^/ http://www.magedu.com/;
- } //如果用户访问的是jump.magedu.com则跳转到http://www.magedu.com/
- 例如:在nginx主配置文件server中添加如下内容:
- server {
- listen 80;
- server_name test.magedu.com;
- index index.html index.php;
- rewrite ^/ http://tomcat.magedu.com/;
- }
- 在本地主机hosts文件中添加如下内容:
- 172.16.11.11 test.magedu.com
- 172.16.11.21 tomcat.magedu.com
- server {
- listen 80;
- server_name mirror.magedu.com;
- index index.html index.php;
- root /www/htdocs;
- rewrite ^/(.*)$ http://www.magedu.com/$1 last;
- }
- location ~* \.(gif|jpg|png|swf|flv)$ {
- valid_referers none blocked www.magedu.com;
- if ($invalid_referer) {
- rewrite ^/ http://www.magedu.com/403.html;
- # return 404
- }
- }
- if (! -e $request_filename) {
- rewrite ^/user/([0-9]+)/?$ /view.php?go=user_$1 last;
- rewrite ^/component/id/([0-9]+)/?$ /page.php?pageid=$1 last;
- rewrite ^/component/([^/]+)/?$ /page.php?pagealias=$1 last;
- rewrite ^/category\_([0-9]+)\.htm$ http://$host/category/$1/ permanent;
- rewrite ^/showday\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/date/$1/$2/$3/ permanent;
- showday_1_2_3.htm $host/date/1/2/3/
- }
- $arg_PARAMETER This variable contains the value of the GET request variable PARAMETER if present in the query string.
- $args This variable contains the query string in the URL, for example foo=123&bar=blahblah if the URL is http://example1. com/? foo=123&bar=blahblah
- $binary_remote_addr The address of the client in binary form.
- $body_bytes_sent The bytes of the body sent.
- $content_length This variable is equal to line Content-Length in the header of request.
- $content_type This variable is equal to line Content-Type in the header of request.
- $document_root This variable is equal to the value of directive root for the current request.
- $document_uri The same as $uri.
- $host This variable contains the value of the 'Host' value in the request header, or the name of the server processing if the 'Host' value is not available.
- $http_HEADER The value of the HTTP header HEADER when converted to lowercase and with "dashes" converted to "underscores", for example, $http_user_agent, $http_referer.
- $is_args Evaluates to "?" if $args is set, returns "" otherwise.
- $request_uri This variable is equal to the *original* request URI as received from the client including the args. It cannot be modified. Look at $uri for the post-rewrite/altered URI. Does not include host name. Example: "/foo/bar.php?arg=baz".
- $scheme The HTTP scheme (that is http, https). Evaluated only on demand, for example: rewrite ^(.+)$ $scheme://example.com$1 redirect;
- $server_addr This variable contains the server address. It is advisable to indicate addresses correctly in the listen directive and use the bind parameter so that a system call is not made every time this variable is accessed.
- $server_name The name of the server.
- $server_port This variable is equal to the port of the server, to which the request arrived.
- $server_protocol This variable is equal to the protocol of request, usually this is HTTP/1.0 or HTTP/1.1.
- $uri This variable is equal to current URI in the request (without arguments, those are in $args.) It can differ from $request_uri which is what is sent by the browser. Examples of how it can be modified are internal redirects, or with the use of index. Does not include host name. Example: "/foo/bar.html"
- # mkdir /health/
- # unzip healthcheck_nginx_upstreams.zip -d /health/
- # mv /health/cep21-healthcheck_nginx_upstreams-16d6ae7 /health/healthcheck_nginx_upstreams
- 首先解压nginx,并进入nginx源码目录:
- # tar xf nginx-1.0.14.tar.gz //注:需要nginx版本匹配,不支持nginx-1.2.2及以上版本
- # cd nginx-1.0.14
- # patch -p1 < /health/healthcheck_nginx_upstreams/nginx.patch
- 而后编译nginx,在执行configure时添加类似下面的选项:
- --add-module=/health/healthcheck_nginx_upstreams
- 所以,这里就使用如下命令:
- # ./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/ \
- --with-pcre \
- --add-module=/health/healthcheck_nginx_upstreams
- # make && make install
- 编辑nginx的主配置文件:
- http选项中添加:
- upstream cluster {
- server 172.16.11.21 weight=1;
- server 172.16.11.22 weight=2;
- healthcheck_enabled;
- healthcheck_delay 1000;
- healthcheck_timeout 1000;
- healthcheck_failcount 2;
- healthcheck_expected 'OK';
- healthcheck_send "GET /.health HTTP/1.0";
- }
- Server中添加location选项内容:
- location / {
- proxy_pass http://cluster;
- }
- location /stat {
- healthcheck_status;
- }