天天看點

nginx

實驗内容:

伺服器ip 192.168.1.236 安裝nginx

伺服器ip 192.168.1.208 安裝tomcat

通路192.168.1.236:8080 跳轉到192.168.1.208:8080

1.安裝nginx伺服器,首先來解決nginx的依賴關系,

[root@nginx ~]# yum groupinstall -y "development tools" "server platform deveopment"

[root@nginx ~]# yum install -y openssl-devel pcre-devel

2.建立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)

3.接着我們來開始編譯和安裝,

[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@mail 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 -eq 0 ] && rm -f $lockfile

restart() {

  configtest || return $?

  stop

  sleep 1

  start

reload() {

  echo -n $"reloading $prog: "

  killproc $nginx -hup

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

  restart|configtest)

  reload)

    rh_status_q || exit 7

  force-reload)

    force_reload

  status)

    rh_status

  condrestart|try-restart)

      ;;

  *)

    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

ii

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.1.208/; #注釋預設兩行,新增一行。

    }

    #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:                      [确定]

2.nginx将圖檔緩存到本地

[root@mail 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;

    worker_connections  1024;

    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.1.208:8080 weight=1;

 }

    server {

        listen       8080;

        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.1.208:8080/;

 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;

        # proxy the php scripts to apache listening on 127.0.0.1:80

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

        # pass the php scripts to fastcgi server listening on 127.0.0.1:9000

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  script_filename  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        # deny access to .htaccess files, if apache's document root

        # concurs with nginx's one

        #location ~ /\.ht {

        #    deny  all;

    # another virtual host using mix of ip-, name-, and port-based configuration

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

    # https server

    #    listen       443 ssl;

    #    server_name  localhost;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:ssl:1m;

    #    ssl_session_timeout  5m;

    #    ssl_ciphers  high:!anull:!md5;

    #    ssl_prefer_server_ciphers  on;

下面我們來建立緩存目錄,

[root@nginx ~]# mkdir -pv /nginx/cache

mkdir: 已建立目錄 "/nginx"

mkdir: 已建立目錄 "/nginx/cache"

測試一下配置檔案是否有錯,

[root@nginx ~]# nginx -t

重新加載配置檔案,

nginx

大家可以看到我們通路的所有的靜态内容都是命中的,x-cache-status: hit,下面們來看一下緩存的目錄,

[[root@mail nginx]# cd /nginx/cache/

[root@mail cache]# ls

2  3  5  8  a  b  c  f

大家可以看到,緩存目錄當中有我們緩存的内容,好了到這裡我們的nginx緩存服務就配置完成了,下面我們看一下如何實作動靜分離。

繼續閱讀