天天看點

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的首頁了,這表示配置成功!