实验环境
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的启动关闭和测试命令如下:
关闭nginx[email protected]:~$ sudo pkill -9 nginx
测试nginx是否成功配置
[email protected]:~$ sudo nginx -t
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
开启nginx
[email protected]:~$ sudo nginx
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
[email protected]:~$
其中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;
}