技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁(yè) > 教程 > 服務(wù)器類 >

Nginx如何配置url_hash轉(zhuǎn)發(fā)方式

來(lái)源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2018-06-29 12:22┆點(diǎn)擊:

  所需軟件名稱及版本:

  pcre-8.02.tar.gz

  nginx-0.7.58.tar.gz

  Nginx_upstream_hash-0.3.1.tar.gz

  1、安裝pcre

  tar -zxvf pcre-8.02.tar.gz

  cd pcre-8.02/

  ./configure

  make && make install

  2、給nginx安裝url_hash補(bǔ)丁

  tar -zxvf nginx-0.7.58.tar.gz

  tar -zxvf Nginx_upstream_hash-0.3.1.tar.gz

  cd nginx-0.7.58/

  patch -p0 < /var/tmp/loojoy/nginx_upstream_hash-0.3.1/nginx.patch

  3、安裝nginx

  groupadd www

  useradd www -g www

  ./configure --prefix=http://www.3lian.com/usr/local/webserver/nginx

  --user=www

  --group=www

  --add-module=http://www.3lian.com/var/tmp/loojoy/nginx_upstream_hash-0.3.1

  --with-http_ssl_module

  --with-http_stub_status_module

  4、檢查nginx生成的配置文件是否正確

  /usr/local/webserver/nginx/sbin/nginx -t

  5、啟動(dòng)nginx

  若nginx.conf檢查通過(guò),則啟動(dòng)nginx。

  /usr/local/webserver/nginx/sbin/nginx

  6、查看端口80

  netstat -ant

  7、通過(guò)瀏覽器訪問(wèn):

  

  若可以正常訪問(wèn),則nginx安裝成功。

  8、修改nginx的配置文件如下:

  -------------------url_hash轉(zhuǎn)發(fā)方式nginx.conf--------------------

  user www www

  worker_processes 10

  error_log logs/error.log

  #error_log logs/error.log notice

  #error_log logs/error.log info

  #pid logs/nginx.pid

  worker_rlimit_nofile 51200

  events

  {

  use epoll

  worker_connections 51200

  }

  http

  {

  include mime.types

  default_type application/octet-stream

  keepalive_timeout 120

  tcp_nodelay on

  upstream your.website.name{

  server your.tomcat.server.ip1:8080

  server your.tomcat.server.ip1:8080

  hash $request_uri

  }

  server

  {

  listen 80

  server_name your.website.name

  location / {

  proxy_pass

  proxy_set_header Host $host

  proxy_set_header X-Real-IP $remote_addr

  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

  }

  log_format www_gjw_com '$remote_addr - $remote_user [$time_local] $request '

  '"$status" $body_bytes_sent "$http_referer" '

  '"$http_user_agent" "$http_x_forwarded_for"'

  #access_log /data1/logs/www.log www_gjw_com

  location /nginx_status {

  stub_status on

  access_log off

  }

  }

  }

  -------------------[E]url_hash轉(zhuǎn)發(fā)方式nginx.conf[E]--------------------

  9、添加nginx啟動(dòng)、關(guān)閉、重啟腳本

  -------------------[S]url_hash轉(zhuǎn)發(fā)方式nginx啟動(dòng)、關(guān)閉腳本[S]--------------------

  #!/bin/sh

  CWD=`pwd`

  case $1 in

  start)

  /usr/local/webserver/nginx/sbin/nginx

  ;

  stop)

  kill -2 `ps -ef|grep "/usr/local/webserver/nginx/sbin/nginx"|grep -v "grep"|awk '{print $2}' `

  ;

  restart)

  cd "$CMD"

  $0 stop

  $0 start

  ;

  *)

  echo $"Usage: $0 {start|stop|restart}"

  exit 1

  esac

  exit 0

  -------------------[E]url_hash轉(zhuǎn)發(fā)方式nginx啟動(dòng)、關(guān)閉腳本[E]--------------------

  更改該文件權(quán)限

  chmod u+x nginx.sh

  啟動(dòng) nginx ./nginx.sh start

  重啟 nginx ./nginx.sh restart

  關(guān)閉 nginx ./nginx.sh stop