1. 程式人生 > >Windows下nginx + tomcat 配置

Windows下nginx + tomcat 配置

寫了一個 webservice 服務,給其他系統測試使用,如果直接在eclipse裡啟動,自己編寫程式碼時,就會影響別人使用,於是搭了一個nginx供其他開發人員使用。

網上大部分教程人都有這個說明:

Tomcat伺服器作為一個Web伺服器,其併發數在300-500之間,如果有超過500的併發數便會出現Tomcat不能響應新的請求的情況,嚴重影響網站的執行。另外,在訪問量大的情況下,Tomcat的執行緒數會不斷增加。由於Tomcat自身對記憶體的佔用有控制,當對記憶體的佔用達到最大值時便會出現記憶體溢位,對網站的訪問嚴重超時等現象,這時便需要重新啟動Tomcat以釋放佔用的記憶體,這樣做便會阻斷網站執行。
所以對Tomcat做負載均衡便很有必要。目前可以和Tomcat做負載均衡的主流伺服器是Apache,但是Nginx由於功能多、配置簡單等優點逐漸成為很多負載均衡伺服器的首選。Nginx的併發數可達到50000,所以理論上可以和Tomcat以1:100的比例來配置,這樣便可以很好的解決網站併發瓶頸問題。而且Nginx、apache是基於http反向代理方式,位於ISO模型的第七層應用層。直白些就是TCP
UDP 和http協議的區別,Nginx不能為基於TCP協議的應用提供負載均衡。

maxThreads 的官網說明
The maximum number of request processing threads to be
created by this Connector, which therefore determines the maximum
number of simultaneous requests that can be handled. If not specified,
this attribute is set to 200. If an executor is associated with this
connector, this attribute is ignored as the connector will execute
tasks using the executor rather than an internal thread pool.

下載下來是zip,直接解壓 nginx.exe即可使用

三、解壓tomcat
解壓tomcat後,複製一份,這裡稱為tomcat1,tomcat2
修改一下tomcat/conf/server.xml 裡埠,不與現有埠衝突即可
這裡假設tomcat1 為8081,tomcat2為8082
測試localhost:8081 localhost:8082 可正常訪問即可

四、配置nginx
Nginx根目錄下的/conf目錄下 nginx.conf

  1. 在#gzip on;後面加入下面配置:
upstream localhost {
    server localhost:8081
weight=5; server localhost:8082 weight=5; ip_hash; }
  1. 修改location配置
    location / {
    root html;
    index index.html index.htm;
    }
    改為:

location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
proxy_redirect off;
proxy_set_header Host host;proxysetheaderXRealIPremote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k; }

啟動nginx

常用nginx 命令:
停止:nginx -s stop
修改配置使配置生效:nginx -s reload
檢查配置:nginx -t