1. 程式人生 > >nginx+tomcat 負載均衡

nginx+tomcat 負載均衡

connect port nbsp another https ror filename ups types

第一篇nginx for windows 安裝:https://www.cnblogs.com/blogxiao/p/8761734.html

第二篇: tomcat 的部署:

1.nginx可以作為反向代理服務器,作為客戶端發送請求的門戶,這個門戶將接受的請求分均衡發給多個應用服務器,達到高負載的效果,典型的就比如tomcat應用服務器。

2.解釋一下反向代理:正向和反向是針對服務器來說的,服務器發送給客戶端的是正向,客戶端向服務器發起的是反向。

3.先下載一個tomcat,然後拷貝成兩份

技術分享圖片

4.在eclipse新建一個簡單的web項目,將生成的放到tomcat 的webapps 的目錄下

5.開始配置tomcat ,tomcat 最主要的就是端口號 和 項目根路徑,配置tomcat 的server 文件,文件的路徑tomcat1->conf->server.xml

tomcat1端口號配置

<Server port="8006" shutdown="SHUTDOWN">

<Connector connectionTimeout="20000" port="8090" protocol="HTTP/1.1" redirectPort="8443"/>

<Connector port="8001" protocol="AJP/1.3" redirectPort="8443"/>

項目根路徑配置

<Host appBase="D:\nginx\tomcat\tomcat1\webapps" autoDeploy="true" name="localhost" unpackWARs="true">

  <Context docBase="D:\nginx\tomcat\tomcat1\webapps\springmvcmaven" path="/" reloadable="true" />

</Host>

appBase 項目的根目錄 docBase 是項目路徑 ,這裏的項目名稱是springmvcmaven

springmvcmaven 文件

技術分享圖片

測試訪問http://localhost:8090/index.jsp

技術分享圖片

這裏的tomcat1 是為了區分nginx 反向代理了哪個應用服務器

tomcat2 端口配置

<Server port="8007" shutdown="SHUTDOWN">

<Connector connectionTimeout="20000" port="8070" protocol="HTTP/1.1" redirectPort="8553"/>

<Connector port="8002" protocol="AJP/1.3" redirectPort="8553"/>

項目訪問路徑的配置

<Host appBase="D:\nginx\tomcat\tomcat2\webapps" autoDeploy="true" name="localhost" unpackWARs="true">

<Context docBase="D:\nginx\tomcat\tomcat2\webapps\springmvcmaven" path="/" reloadable="true" />

</Host>

訪問地址:http://localhost:8070/index.jsp

技術分享圖片

6.nginx 的配置,找到nginx->conf->nginx.conf 文件進行修改

#user  nobody;
#設置工作進程
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    #設置工作進程的最大連接數
    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  localhost {  #服務器集群名字  
    #集群進行負載均衡有多重策略,這裏使用權重的方式進行測試 
        server    127.0.0.1:8070  weight=1;#服務器配置   weight是權重的意思,權重越大,分配的概率越大。  
        server    127.0.0.1:8090  weight=2;  
    } 

    server {
        #nginx 監聽的端口,這裏的端口設置要註意不要有端口沖突,不然nginx啟動會失敗
        listen       8060;
        #訪問的是服務名稱 可以是域名,localhost,ip
        server_name  localhost;
     
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #設置nginx作為反向代理服務器
        location / {
            proxy_pass http://localhost;  
            proxy_redirect default;  
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache‘s document root
        # concurs with nginx‘s one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

在dos命令中,找到nginx 的路徑,啟動nginx

技術分享圖片

7.測試訪問:輸入地址:http://localhost:8060

第一次返回的是

技術分享圖片

第二次返回的是:

技術分享圖片

多訪問幾次就可以看到效果。

總結:此篇文章的nginx+tomcat 負載均衡只是很簡單的配置,僅供入門。

nginx+tomcat 負載均衡