天天看点

Nginx+Tomcat配置集群负载均衡(windows)

这几天,仔细研究了一下Nginx+Tomcat配置,严格按照网上配置来的,最后终于成功了!这其中的过程很有意思,有自己不小心的错误,也有文章讲的比较含糊的地方,下面讲讲遇到的几个坑,以备查询;

一、启动nginx时候,最好要到安装目录下

Microsoft Windows [版本 6.1.7601]

版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\Administrator>start nginx

C:\Users\Administrator>tasklist /fi "imagename eq nginx.exe"

映像名称 PID 会话名 会话# 内存使用

========================= ======== ================ =========== ============

nginx.exe 8956 RDP-Tcp#0 2 6,792 K

nginx.exe 8332 RDP-Tcp#0 2 7,036 K

C:\Users\Administrator>nginx -t

nginx: [alert] could not open error log file: CreateFile() "logs/error.log" fail

ed (3: The system cannot find the path specified)

2017/02/14 16:32:17 [emerg] 9044#10132: CreateFile() "C:\Users\Administrator/con

f/nginx.conf" failed (3: The system cannot find the path specified)

nginx: configuration file C:\Users\Administrator/conf/nginx.conf test failed

C:\Users\Administrator>

虽然我配置了环境变量,但是在使用命令nginx -t报配置文件错误!

到安装目录下就没问题了。

E:\zsf\nginx-1.11.9>nginx -s stop

E:\zsf\nginx-1.11.9>start nginx

E:\zsf\nginx-1.11.9>tasklist /fi "imagename eq nginx.exe"

映像名称 PID 会话名 会话# 内存使用

========================= ======== ================ =========== ============

nginx.exe 8956 RDP-Tcp#0 2 6,788 K

nginx.exe 8332 RDP-Tcp#0 2 7,064 K

E:\zsf\nginx-1.11.9>nginx -t

nginx: the configuration file E:\zsf\nginx-1.11.9/conf/nginx.conf syntax is ok

nginx: configuration file E:\zsf\nginx-1.11.9/conf/nginx.conf test is successful

E:\zsf\nginx-1.11.9>

二、Nginx在win7,win2008下启动报错:bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions) 。

原因是Win7下nginx默认80端口被System占用,造成nginx启动报错的。

netstat-aon| findstr "80"

通过命令可以看到80端口果真被占用。发现占用的pid是4,名字是System。怎么禁用呢?

解决方案一、修改conf目录下的nginx.conf文件,修改端口,只要是没有占用的端口就行,将listen 80;改成listen 8889;

主要修改这几个地方:

upstream local_tomcat {

#这里指定多个源服务器,ip:端口,80端口的话可写可不写

server localhost:8080;

}

server {

listen 8889;

server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root html;

index index.html index.htm index.jsp login.jsp; #定义首页索引文件的名称

proxy_pass http://local_tomcat;#请求转向suroot定义的服务器列表

#以下是一些反向代理的配置可删除.

proxy_redirect off;

~~~~~~

}

~~~~~~

}

解决方案二、(没有试过,抄的别人的)

  1. 打开注册表:regedit
  2. 找到:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\HTTP
  3. 找到一个REG_DWORD类型的项Start,将其改为0
  4. 重启系统,System进程不会占用80端口
  5. 重启之后,start nginx.exe 。

  一、启动nginx,start nginx.exe 二、启动tomcat 在浏览器中,输入http://localhost:8889,或者http://localhost:8080/就看到tomcat的首页了,这表示配置成功!