#user nobody;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
# 啟用內核復制模式,應該保持開啟到達最快IO效率
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
# HTTP1.1支持耐久毗連alive
# 低落每個毗連的alive時間可在必然水平上提高可響應毗連數量,所以一般可適當低落此值
keepalive_timeout 65;
# 啟動內容壓縮,,有效低就逮絡流量
gzip on;
# 過短的內容壓縮結果不佳,壓縮進程還會揮霍系統資源
gzip_min_length 1000;
# 可選值1~9,壓縮級別越高壓縮率越高,但對系統機能要求越高
gzip_comp_level 4;
# 壓縮的內容種別
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# 靜態文件緩存
# 最大緩存數量,文件未利用存活期
open_file_cache max=655350 inactive=20s;
# 驗證緩存有效期時距離斷
open_file_cache_valid 30s;
# 有效期內文件最少利用次數
open_file_cache_min_uses 2;
upstream web_app {
server 192.168.0.4:8080 weight=1;
server 192.168.0.4:8081 weight=2;
}
upstream web_test {
server 192.168.0.4:8081 ;
}
server {
listen 8888;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm index.jsp index.do;;
# proxy_pass http://web_app;
#}
#設置Nginx消息疏散,界說的靜態頁面直接從Nginx宣布目次讀取。
location ~ .*.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root html;
#expires界說用戶欣賞器緩存的時間為7天,假如靜態頁面不常更新,可以配置更長,這樣可以節減帶寬緩和解處事器的壓力
expires 7d;
}
location /test {
deny all;
}
location /web {
proxy_pass http://web_test;
}
#所有jsp、do的動態請求都交給后頭的tomcat處理懲罰 location ~ (.jsp)|(.do)$
location /
{
#tomcat地點
proxy_pass http://web_app;
# 請求頭中Host信息
proxy_set_header HOST $host;
# 真實的客戶端IP
proxy_set_header X-Real-IP $remote_addr;
# 署理路由信息,此處取IP有安詳隱患
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 真實的用戶會見協議
proxy_set_header X-Forwarded-Proto $scheme;
# 默認值default,
# 后端response 302時 tomcat header中location的host是http://192.168.1.62:8080
# 因為tomcat收到的請求是nginx發已往的, nginx提倡的請求url host是http://192.168.1.62:8080
# 配置為default后,nginx自動把響應頭中location host部門替換成當前用戶請求的host部門
# 網上許多教程將此值配置成 off,禁用了替換,
# 這樣用戶欣賞器收到302后跳到http://192.168.1.62:8080,直接將后端處事器袒露給欣賞器
# 所以除非非凡需要,不要配置這種多此一舉的設置
proxy_redirect default;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
nginx+tomcat前后端疏散的要領
