Nginx學習筆記

來源:互聯網
上載者:User

作者:fbysss
msn:jameslastchina@hotmail.com 
blog:blog.csdn.net/fbysss
聲明:本文由fbysss整理編寫,轉載請註明出處
關鍵字:Nginx

Nginx ,爽呆了。

首先編譯pcre:進入解壓目錄

./configure --prefix=/opt/pcre

make

makeinstall

需要注意的是:預設是有問題的。需要做以下操作

 

mkdir  /usr/local/include/pcre

mkdir /usr/local/include/pcre/.libs

cp /opt/pcre/lib/libpcre.a /usr/local/include/pcre/libpcre.a

cp /opt/pcre/lib/libpcre.a /usr/local/include/pcre/libpcre.la

cp /opt/pcre/include/pcre.h /usr/local/include/pcre/pcre.h

cp  /usr/local/include/pcre/*.* /usr/local/include/pcre/.libs

 

./configure --prefix=/opt/nginx -with-pcre=/usr/local/include/pcre

注意路徑後面不要加斜杠。

在make的時候,要刪掉objs/Makefile裡面的一句話

尋找./configure --disable-shared 關鍵字,刪除那一行,然後才make

 

配置:

nginx的配置,簡直太人性化了,除了叢集的配置,其他的我幾乎就沒有看協助文檔,完全是心想事成,一點bug都沒有,簡直太順利了。

虛擬機器主機:可以配多個,

listen xxx.xxx.xxx [port]

servername [是否需要在/etc/hosts中配置都不一定(不需要),這裡也可以是多個,用空格分隔,比如我既可以做圖片伺服器,也可以做負載平衡伺服器,只不過連接埠號碼不一樣,那麼就配置兩個虛擬機器主機,連接埠偵聽不一樣。]

比如圖片伺服器在本機192.168.0.2上,然後有兩個tomcat在192.168.0.3 和192.168.0.4上。

在http段中配置

upstream tomcats{

    192.168.0.3:8080

192.168.0.3:8080

}

要注意的一個問題:如果nginx的偵聽連接埠不是80,有時候有問題,轉寄的時候,沒有帶上連接埠號碼,這可能是一個bug。而配置為80之後就沒有任何問題了。

然後在location /中,配置

proxy_pass tomcats  即反向 Proxy,實現分發

 

 

虛擬目錄配置:

location /hello{

    alias /usr/local/sss/hello;

}

其他要注意的就是realip配置

 

proxy_set_header        Host $host;

proxy_set_header        X-Real-IP $remote_addr;

proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

nginx+(1…n)tomcat叢集部署時,後端tomcat需要取得使用者的IP,這時通過 request.getRemoteAddr()取得的始終是127.0.0.1.

 

由於使用者請求經過nginx,ngigx轉寄了請求到tomcat後,tomcat取得的始終是nginx的Ip,如果需要取得真實使用者IP,只需要在ngix上做些配置。

 

location / 下設定

  proxy_set_header X-Real-IP $remote_addr;

  proxy_set_header Host $host;

 

 

測試組態:./sbin/nginx -t -c conf/nginx.conf  正確後重啟nginx.

 

Java代碼修改如下

 

out.println("X-Real-IP : " + request.getHeader("X-Real-IP") + "<br>");

 

其中X-Real-IP即使用者真實IP

 

 

 

負載平衡策略

upstream  tomcats{

ip_hash;

server   192.168.0.3:8080;

server   192.168.0.4:8080;

}

 

server {

  location / {

    proxy_pass  http://backend;

  }

}根據用戶端IP進行負載平衡,針對同一個C類位址區段中的用戶端選擇到後端同一個原始伺服器,可避免session引起的問題。

 

還有就是nginx並不提供啟停指令碼,需要自己編寫。

#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
 . /etc/default/nginx
fi

set -e

case "$1" in
  start)
 echo -n "Starting $DESC: "
 start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid
  --exec $DAEMON -- $DAEMON_OPTS
 echo "$NAME."
 ;;
  stop)
 echo -n "Stopping $DESC: "
 start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid
  --exec $DAEMON
 echo "$NAME."
 ;;
  test)
 $DAEMON -t -c /etc/nginx/conf/nginx.conf
 ;;
  restart|force-reload)
 echo -n "Restarting $DESC: "
 start-stop-daemon --stop --quiet --pidfile
  /var/run/$NAME.pid --exec $DAEMON
 sleep 1
 start-stop-daemon --start --quiet --pidfile
  /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
 echo "$NAME."
 ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid
          --exec $DAEMON
      echo "$NAME."
      ;;
  upgrade)
      echo -n "Upgrade $DESC: "
      kill -USR2 `cat /var/run/$NAME.pid`
      sleep 1
      kill -WINCH `cat /var/run/$NAME.pid`
      sleep 1
      kill -9 `cat /var/run/$NAME.pid.oldbin`
      rm /var/run/$NAME.pid.oldbin
      echo "$NAME."
      ;;
  *)
 N=/etc/init.d/$NAME
 echo "Usage: $N {start|stop|test|restart|reload|force-reload|upgrade}" >&2
 exit 1
 ;;
esac

exit 0

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.