Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。 其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好。
准备
nginx:nginx/Windows-1.10.1 http://nginx.org/en/download.html
tomcat:apache-tomcat-6.0.45
准备2个tomcat服务器分别命名为:
1 2 | apache-tomcat- 6.0 . 45 - 8090 apache-tomcat- 6.0 . 45 - 8091 |
同时修改server.xml文件中的端口号,有三个端口号需要修改,防止端口冲突:
1 2 3 | 1 .<Server port= "8005" shutdown= "SHUTDOWN" > 2 .<Connector port= "8090" protocol= "HTTP/1.1" ... 3 .<Connector port= "8009" protocol= "AJP/1.3" redirectPort= "8443" /> |
修改tomcat默认首页:webapps/ROOT/index.html,用于识别当前进入的是哪个tomcat:
1 2 | <td align= "left" valign= "top" ><b>Apache Tomcat------ 8090 </b></td> <td align= "left" valign= "top" ><b>Apache Tomcat------ 8091 </b></td> |
nginx.conf配置
修改conf/nginx.conf文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #user nobody; worker_processes 1 ; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024 ; } http { include mime.types; 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"' ; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0 ; keepalive_timeout 65 ; #gzip on; upstream suroot{ server 127.0 . 0.1 : 8090 max_fails= 2 fail_timeout=30s; server 127.0 . 0.1 : 8091 max_fails= 2 fail_timeout=30s; } server { listen 80 ; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http: //suroot/; #请求转向suroot定义的服务器列表 #以下是一些反向代理的配置可删除. 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; client_max_body_size 10m;#允许客户端请求的最大单文件字节数 client_body_buffer_size 128k;#缓冲区代理缓冲用户端请求的最大字节数, proxy_connect_timeout 600 ;#nginx跟后端服务器连接超时时间(代理连接超时) proxy_send_timeout 600 ; #后端服务器数据回传时间(代理发送超时) proxy_read_timeout 600 ; #连接成功后,后端服务器响应时间(代理接收超时) proxy_buffer_size 8k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 64k;#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_busy_buffers_size 128k;#高负荷下缓冲大小(proxy_buffers* 2 ) proxy_temp_file_write_size 128k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传 } #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; # } #} } |
测试
1.tomcat/bin/startup.bat分别启动2个tomcat
2.启动nginx
执行nginx.exe或者使用命令:start nginx
常用命令:
1 2 3 4 | start nginx 启动 nginx -s stop 停止服务 nginx -t 验证nginx.conf是否正确 nginx -s reload 改变了nginx配置信息并需要重新载入 |
因为nginx启动的端口是80,所以我们直接访问:http://localhost/
不停刷新页面,经测试Apache Tomcat——8090和Apache Tomcat——8091交替出现