据说 Nginx 做负载均衡不错,恩拿来学习配置下。
先安装:
wget http://sysoev.ru/nginx/nginx-0.6.35.tar.gztar zxvf nginx-0.6.35.tar.gz
cd nginx-0.6.35
./configure
make
make install
安装时出现下面的错误:
Configuration summary
+ PCRE library is not found
+ OpenSSL library is not used
+ md5 library is not used
+ sha1 library is not used
+ using system zlib library
./configure: error: the HTTP rewrite module requires the PCRE
library.
You can either disable the module by using
--without-http_rewrite_module
option, or install the PCRE library into the system, or build the
PCRE library
statically from the source with nginx by using --with-pcre=
option.
说没有 PCRE 库,那安装它:
yum install pcre-devel
安装完后,修改配置vi conf/nginx.conf,修改后看起来像:
user chenlb;worker_processes 10;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
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;
upstream demo {
server 172.0.0.1:8080 weight=1;
server 192.168.1.100:8080 weight=1;
}
server {
listen 80;
server_name nobody.chenlb.com;
#charset koi8-r;
log_format nobody_chenlb_com '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" "$http_x_forwarded_for"';
access_log logs/access.log nobody_chenlb_com;
location / {
proxy_pass http://demo;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
修改浏览器所在机子hosts,nobody.chenlb.com 指向到nginx所在机子。
jsp的测试页面,nginx-test.jsp:
<%@ page language="java" pageEncoding="UTF-8"%><%@ page import="java.util.LinkedHashMap" %>
<%@ page import="java.util.Enumeration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>nginx demo</title>
</head>
<body>
<%
LinkedHashMap map = new LinkedHashMap();
map.put("getServerName", request.getServerName());
map.put("getServerPort", request.getServerPort());
map.put("getRemoteAddr", request.getRemoteAddr());
map.put("getRemoteHost", request.getRemoteHost());
map.put("getRemotePort", request.getRemotePort());
map.put("getLocalName", request.getLocalName());
map.put("getLocalAddr", request.getLocalAddr());
map.put("getLocalPort", request.getLocalPort());
//HttpServletRequest req = (HttpServletRequest)request;
for (Enumeration e = request.getHeaderNames() ; e.hasMoreElements() ;) {
Object obj = e.nextElement();
map.put(obj.toString(), request.getHeader(obj.toString()));
}
%>
<table border=1>
<tr><th>name</th><th>value</th></tr>
<%
for(Object key : map.keySet()) {
out.println("<tr><th>");
out.println(key+"</th><th>"+map.get(key)+"</th></tr>");
}
String str = request.getContextPath()+request.getServletPath()+(request.getPathInfo()==null?"":request.getPathInfo())+(request.getQueryString() == null ? "" : "?"+request.getQueryString());
System.out.println(str);
%>
</table>
</body>
</html>
分别在172.0.0.1:8080、192.168.1.100:8080的web实例(webapps/demo)下放入nginx-test.jsp。
启动nginx:
sbin/nginx
在浏览器中访问:http://nobody.chenlb.com/demo/nginx-test.jsp,出现了一个问题,一时快,一时很慢,看到nginx在域名查找,后来把upstream的demo在nginx那台机子的hosts配上IP就好。
声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。