實驗環境
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; }