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