1 背景:
之所以心血來潮要配置nginx,主要有如下N個原因:
1.因業務需要,服務器遷移通用區,爲保障域名切換時不中斷服務
2.SmartIDE作爲客戶端程序,因歷史原因後臺多個服務使用了同一個域名,導致發佈新服務時,往往一臺服務內存溢出後,造成所有服務全部Crash,而且此時定位問題的難度劇增,因爲你根本就知道是哪個服務造成的。所以需要通過虛擬主機,將應用服務拆分到不同的服務器上去。
3.改善服務性能,如服務集羣,動靜分離。
4.IP訪問控制,防止被人惡意調用。
5.重定向404,500等錯誤頁面。
6.統一記錄調用日誌(包括客戶端IP,瀏覽器,訪問地址,訪問時間)。
7.對不支持的瀏覽器統一路由到一個頁面。
2 Nginx安裝:
公司的環境是SUSE11,yum連接不到外源,於是採用的是源碼編譯的方式,好在並不複雜
模塊依賴性Nginx需要依賴下面3個包
(1) gzip模塊需要 zlib庫 ( 下載:http://www.zlib.net/ )
(2) rewrite 模塊需要 pcre庫 ( 下載:http://www.pcre.org/ )
(3) ssl 功能需要openssl庫 ( 下載:http://www.openssl.org/ )
Nginx包下載:http://nginx.org/en/download.html
我下載的版本是:
nginx-1.8.0.tar.gz
pcre-8.11.tar.gz
zlib-1.2.8.tar.gz
openssl-1.0.1p.tar.gz
第一步:安裝pcre
tar -zxvf pcre-8.11.tar.gz
cd pcre-8.11
./configure
make
make install
第二步:安裝zlib
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install
第三步:安裝openssl
tar -zxvf openssl-1.0.1p.tar.gz
cd openssl-1.0.1p
./config
make
make install
第四步:安裝nginx
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-8.11 --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-1.0.1p --with-http_stub_status_module --user=nginx --group=nginx
make
make install
新增服務文件vi/etc/init.d/nginx , 同樣也是要將前面安裝時設置的nginx的安裝路徑,配置文件,pid文件路徑設置正確注意上面configure的配置中,主要是將nginx的安裝路徑,配置文件,pid文件都設置在了/usr/local/nginx目錄裏,二是pcre,zlib,openssl的目錄設置爲前面解壓後的路徑(我這是設置的是相對路徑)
安裝完後就可以通過:
/usr/local/nginx/nginx -c /usr/local/nginx/nginx.conf 啓動nginx了
使用netstat-ntlp查看80端口,或者直接拿瀏覽器訪問看nginx是否已經啓動
3 Nginx設置爲服務,並設置啓動:
新增服務文件vi/etc/init.d/nginx , 同樣也是要將前面安裝時設置的nginx的安裝路徑,配置文件,pid文件路徑設置正確
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/nginx
nginx_config=/usr/local/nginx/nginx.conf
nginx_pid=/usr/local/nginx/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
#. /etc/rc.d/init.d/functions
# Source networking configuration.
#. /etc/sysconfig/network
# Check that networking is up.
#[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
$nginxd -c ${nginx_config} &
#daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
-保存後就可以通過以下簡單的命令啓動nginx了
啓動:service nginx start
停止:service nginx stop
重啓:servicenginx restart
設置開機自動啓動:
cd /etc/init.d/
chmod a+x nginx
chkconfig --add nginx
4 Nginx反向代理和集羣配置:
這個網上一搜一大把,主要是在/usr/local/nginx/nginx.conf 中配置了,暫時只配置了個反向代理,後續有必要時再集羣吧
server {
listen 80;
server_name localhost;
location /SmartIDEUCM/ {
#proxy_redirect off;
proxy_pass http://xxxxx:9080/SmartIDEUCM/;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#root html;
#index index.html index.htm;
}
}
5 遇到的問題:
5.1 1.Nginx代理websphere後,getServerPort()無法獲取到正確的端口
解決辦法:參考http://stackoverflow.com/questions/10635110/websphere-portal-behind-reverse-proxy-and-getserverport
裏面提到在websphere中getServerPort 方法依賴於通道的 getVirtualPort 方法,此方法會按以下順序返回端口號:
1.從請求 URL 返回端口號
2.從請求主機頭返回端口號
因此需要在web.xml中配置或在was中配置以下兩個參數,以先從請求主機頭返回端口號
trusthostheaderport= true
com.ibm.ws.webcontainer.extractHostHeaderPort= true
要指定 Web 容器定製屬性,請執行下列操作:
在管理控制檯中,單擊服務器 > 服務器類型 > WebSphere ApplicationServer > server_name > Web 容器設置 > Web 容器。
在其他屬性下,選擇定製屬性。
在“定製屬性”頁面上,單擊新建。
在設置頁面上的名稱字段中輸入要配置的定製屬性的名稱,並在值字段中輸入要爲此屬性設置的值。
單擊應用或確定。
單擊控制檯任務欄上的保存以保存配置更改。
重新啓動服務器。
5.2 2.Nginx代理非80端口的問題
參考:http://www.cnblogs.com/likehua/p/4056625.html