本機配置好兩個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服務器