- # 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可執(zhí)行二進制文件存放目錄
- --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工作進程指定非特權(quán)用戶
- --group=nginx \ //為nginx工作進程指定非特權(quán)用戶組
- --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,內(nèi)容如下:
- #!/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
- 內(nèi)容如下:
- <?php
- phpinfo()
- ?>
- 重啟httpd服務(wù)并訪問試試
- # service httpd restart
- #root html;
- #index index.html index.htm;
添加如下行,制定代理的web服務(wù)器地址
- 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) //每秒鐘響應(yīng)64個請求//
- Time per request: 1547.388 [ms] (mean) //每個請求用時1547毫秒//
- Time per request: 15.474 [ms] (mean, across all concurrent requests) //并發(fā)請求100個用時15秒//
- Transfer rate: 2423.44 [Kbytes/sec] received //平均傳輸速率2423kb/s//
- 在其他另外的主機上執(zhí)行如下命令通過直接訪問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) //每秒鐘響應(yīng)143個請求//- Time per request: 698.862 [ms] (mean) //每個請求用時698毫秒//
- Time per request: 6.989 [ms] (mean, across all concurrent requests) //并發(fā)請求100個用時6秒//
- Transfer rate: 5340.14 [Kbytes/sec] received //平均傳輸速率5340kb/s//
通過以上壓力測試對比發(fā)現(xiàn),盡管采用nginx實現(xiàn)反向代理但nginx沒有緩存功能,頁面響應(yīng)速度不升反而會大大降低;
為nginx反代服務(wù)器提供緩存;
- 編輯nginx的主配置文件,找到http選項,添加如下內(nèi)容:
- proxy_cache_path /var/www/cache levels=1:2 keys_zone=mycache:20m; //定義緩存路徑,緩存級別和內(nèi)存鍵區(qū)域名稱及空間大小//
- max_size=2048m inactive=60m; //最大緩存空間大小和非活動時長(超過該時長緩存過期)//
- proxy_temp_path /var/www/cache/tmp; //代理臨時目錄,需要事先創(chuàng)建//
- 找到Location選項,添加如下內(nèi)容:
- proxy_pass http://172.16.11.21; //制定反代服務(wù)器//
- proxy_cache mycache; //定義緩存名稱,要與上述內(nèi)存鍵區(qū)域名稱保持一致//
- proxy_cache_valid 200 302 60m; //定義請求頁面響應(yīng)碼為200和302的頁面緩存時長為60分鐘//
- proxy_cache_valid 404 1m; //定義請求頁面響應(yīng)碼為404的頁面緩存時長為1分鐘//
- 創(chuàng)建緩存目錄
- # mkdir /var/www/cache/tmp -pv
- 檢查nginx配置文件語法并重啟nginx服務(wù)
- # service nginx configtest
- # service nginx restart
- 首先在其他另外的主機上執(zhí)行幾次如下命令通過nginx代理進行壓力測試使緩存中充滿訪問數(shù)據(jù);
- # 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) //每秒鐘響應(yīng)126個請求//
- Time per request: 792.483 [ms] (mean) //每個請求用時792毫秒//
- Time per request: 7.925 [ms] (mean, across all concurrent requests) //并發(fā)請求100個用時7秒//
- Transfer rate: 4737.85 [Kbytes/sec] received //平均傳輸速率4737kb/s//
通過上述對nginx反向代理進行壓力測試前后對比發(fā)現(xiàn),自從引入了nginx的緩存功能大大提升了頁面響應(yīng)速度,同時也很好的提升的用戶的感知度;
以上就是nginx反向代理引入緩存功能的介紹;
- location / { //定義訪問路徑為”/”時直接在nginx服務(wù)器響應(yīng)//
- root html;
- index index.html index.htm;
- }
- location /images/ { //定義訪問路徑為”/images/”時代理到Apache服務(wù)器響應(yīng)//
- 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
- 添加如下內(nèi)容:
- <h1>apache</h1>
- 重啟nginx和后端apache服務(wù)器
- # service nginx restart
- # service httpd restart
- upstream cluster { //定義后端負載均衡服務(wù)器名稱//
- server 172.16.11.21 weight=1; //指定后端服務(wù)器和權(quán)重//
- server 172.16.11.22 weight=2;
- }
- 在server選項下添加如下內(nèi)容
- location / {
- proxy_pass http://cluster; //反代指向定義的負載均衡服務(wù)器//
- }
- server {
- listen 80;
- server_name jump.magedu.com;
- index index.html index.php;
- root /www/htdocs;
- rewrite ^/ http://www.magedu.com/;
- } //如果用戶訪問的是jump.magedu.com則跳轉(zhuǎn)到http://www.magedu.com/
- 例如:在nginx主配置文件server中添加如下內(nèi)容:
- server {
- listen 80;
- server_name test.magedu.com;
- index index.html index.php;
- rewrite ^/ http://tomcat.magedu.com/;
- }
- 在本地主機hosts文件中添加如下內(nèi)容:
- 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,在執(zhí)行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選項內(nèi)容:
- location / {
- proxy_pass http://cluster;
- }
- location /stat {
- healthcheck_status;
- }