nginx負載平衡初探

來源:互聯網
上載者:User

實驗環境

win7+ubuntu(vmware)

win7 tomcat(ip and por:192.168.0.108:8080)

linux tomcat(ip and por:192.168.110.129:8080)

在ubuntu中安裝nginx(sudo apt-get install nginx),預設安裝在etc/nginx

在兩台tomcat的webapps下的ROOT中,為了表示出訪問的是那一台tomcat,替換index.html以及index.jsp,其中分別寫入linux和windows。

nginx的啟動關閉和測試命令如下:

關閉nginxzhengx@zhengx-virtual-machine:~$ sudo pkill -9 nginx測試nginx是否成功配置zhengx@zhengx-virtual-machine:~$ sudo nginx -tnginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignorednginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful開啟nginxzhengx@zhengx-virtual-machine:~$ sudo nginx nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignoredzhengx@zhengx-virtual-machine:~$ 


其中nginx.conf的設定檔如下所示

user www-data;worker_processes 4;pid /var/run/nginx.pid;events {    worker_connections 768;    # multi_accept on;}http {    sendfile on;    tcp_nopush on;    tcp_nodelay on;    keepalive_timeout 65;    types_hash_max_size 1024;        include /etc/nginx/mime.types;    default_type application/octet-stream;    access_log /var/log/nginx/access.log;    error_log /var/log/nginx/error.log;        gzip on;    upstream   localhost {                              #win7的tomcat訪問url              server   192.168.0.108:8080;                #linux的tomcat訪問url              server   192.168.110.129:8080;          }          server {          listen 80;          server_name  localhost;          charset utf-8;          location / {              root   html;              index  index.html index.htm;              #跳轉到win7上的tomcat            proxy_pass         http://192.168.0.108:8080;            proxy_set_header  X-Real-IP  $remote_addr;              client_max_body_size  100m;          }                      location ~ ^/(WEB-INF)/ {               deny all;           }                 error_page   500 502 503 504  /50x.html;          location = /50x.html {              root   html;          }                }      include /etc/nginx/server.conf;    include /etc/nginx/conf.d/*.conf;    include /etc/nginx/sites-enabled/*;}

為了便於維護把nginx.conf拆分一部分代碼出來分別是upstream.conf和location.conf

nginx.conf的設定檔如下:

user www-data;worker_processes 4;pid /var/run/nginx.pid;events {worker_connections 768;# multi_accept on;}http {sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 1024;include /etc/nginx/mime.types;default_type application/octet-stream;access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;gzip on;include /etc/nginx/upstream.conf;server {  listen 80;  server_name  localhost;  charset utf-8;  include /etc/nginx/location.conf;    }  include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;}

upstream.conf的設定檔如下:

upstream   localhost {                server   192.168.0.108:8080;                server   192.168.110.129:8080;  }  

location.conf的設定檔如下:

location / {              root   html;              index  index.html index.htm;              #去找網域名稱為localhost的upstream            proxy_pass         http://localhost;            proxy_set_header  X-Real-IP  $remote_addr;              client_max_body_size  100m;          }                      location ~ ^/(WEB-INF)/ {               deny all;           }                 error_page   500 502 503 504  /50x.html;          location = /50x.html {              root   html;          }          


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.