首先是window环境结构如下图:
Window xp环境:Nginx1.0.15+Tomcat7.0.8
1、下载地址
http://nginx.org/en/download.html
2、目录结构
Nginx-
|_ conf 配置目录
|_ contrib
|_ docs 文档目录
|_ logs 日志目录
|_ temp 临时文件目录
|_ html 静态页面目录
|_ nginx.exe 主程序
解压缩到一个无空格的英文目录即可
双击nginx.exe即可启动Nginx服务器
DOS环境启动
运行CMD 到DOS界面
下面是一些常用的使用命令:
Nginx -s stop 快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
Nginx -s quit 平稳关闭Nginx,保存相关信息,有安排的结束web服务。
Nginx -s reload 因改变了Nginx相关配置,需要重新加载配置而重载。
Nginx -s reopen 重新打开日志文件。
Nginx -v 查看版本
Nginx -V 查看nginx的版本,编译器版本和配置参数
3、nginx.conf配置
Nginx配置文件默认在conf目录,主要配置文件为nginx.conf,我们安装在D:\nginx-1.0.15、默认主配置文件为D:\nginx-1.0.15\nginx.conf。下面是nginx作为前端反向代理服务器的配置。
Nginx.conf代码
- #Nginx所用用户和组,window下不指定
- #user Administrator;
- #工作的子进程数量(通常等于CPU数量或者2倍于CPU)
- worker_processes 2;
- #错误日志存放路径
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #指定pid存放文件
- #pid logs/nginx.pid;
- events {
- #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
- #use epoll;
- worker_connections 2048;
- }
- 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;
- include gzip.conf;
- upstream localhost {
- #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。
- #同一机器在多网情况下,路由切换,ip可能不同
- #ip_hash;
- server localhost:18081;
- server localhost:8080;
- }
- 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://localhost;
- }
- #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;
- # server_name localhost;
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
代理设置如下:
- 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 300;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
zip压缩相关配置如下:
- gzip on;
- gzip_min_length 1000;
- gzip_types text/plain text/css application/x-javascript;
4、Tomcat配置
第一处端口修改:
- <!-- 修改port端口:8006 俩个tomcat不能重复,端口随意,别太小-->
- <Server port="8006" shutdown="SHUTDOWN">
- <!-- port="18081" tomcat监听端口,随意设置,别太小 -->
- <Connector port="18081" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
启动Nginx和俩个Tomcat验证置负载均衡设置,http://localhost/ 或http://localhost/index.jsp 。
至此window下nginx+tomcat负载均衡配置结束,关于tomcat Session的问题通常是采用memcached,或者采用nginx_upstream_jvm_route ,他是一个 Nginx 的扩展模块,用来实现基于 Cookie 的 Session Sticky 的功能。如果tomcat过多不建议session同步,server间相互同步session很耗资源,高并发环境容易引起Session风暴。请根据自己应用情况合理采纳session解决方案。
参考资料:
http://www.oschina.net/bbs/thread/9301
oschina.net 生产配置,此网站采用java语言,nginx,tomcat服务器。
张宴:<<实战Nginx>>