本机配置好两个tomcat:html
# curl http://127.0.0.1:8080 Tomcat 1 test jsp # curl http://127.0.0.1:8081 Tomcat 2 test jsp
配置本地hosts文件:
nginx
# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 127.0.0.1 tomcat1.com tomcat2.com
配置Nginx:后端
# cat nginx.conf #user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; include conf.d/vhost.conf; 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"'; #记录用户访问的真实IP #log_format main '$HTTP_X_REAL_IP - $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 65; gzip on; client_max_body_size 50m; #缓冲区代理缓冲用户端请求的最大字节数,能够理解为保存到本地再传给用户 client_body_buffer_size 256k; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; proxy_connect_timeout 300s; #nginx跟后端服务器链接超时时间(代理链接超时) proxy_read_timeout 300s; #链接成功后,后端服务器响应时间(代理接收超时) proxy_send_timeout 300s; proxy_buffer_size 64k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k如下的话,这样设置 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘 proxy_ignore_client_abort on; #不容许代理端主动关闭链接 server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
配置反向代理配置文件:缓存
# cat conf.d/vhost.conf server { listen 80; server_name tomcat1.com; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080; } access_log logs/tomcat1_access.log; } server { listen 80; server_name tomcat2.com; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8081; } access_log logs/tomcat2_access.log; }
从新加载配置文件:tomcat
# /opt/nginx/sbin/nginx -s reload
测试:bash
# curl http://tomcat1.com Tomcat 1 test jsp # curl http://tomcat2.com Tomcat 2 test jsp
更多内容,请关注博主我的博客网站:http://www.whysha.com服务器