windows环境下tomcat+nginx负载均衡集群配置,实现动静分离,rest接口分离配置
下载地址:
http://nginx.org/en/download.html
下载完成后解压
直接cmd到本路径下
然后用命令启动(也可以直接点击Nginx直接启动)
启动
直接点击Nginx目录下的nginx.exe 或者 cmd运行start nginx
关闭
nginx -s stop 或者 nginx -s quit
stop表示立即停止nginx,不保存相关信息
quit表示正常退出nginx,并保存相关信息
重启(因为改变了配置,需要重启)
nginx -s reload
启动完成后可以先试试输入localhost访问,如果出现nginx启动界面说明启动正常
接下来修改配置:
#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 localtomcat { #服务器集群名字,这里我用本地模拟
# least_conn;//负载策略
server 192.168.50.111:8080 fail_timeout=30s; #localhost时会自动切换,ip时不自动
server 192.168.50.111:8081 fail_timeout=30s;
server 192.168.50.111:8082 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://localtomcat;
#}
# 所有动态请求都转发给tomcat处理
location /so/{ #此处配置rest接口代理
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://localtomcat; #这里与上面配置的upstream 名字要相同
}
#静态文件交给nginx处理
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
root E:\2; #静态资源路径
}
#静态文件交给nginx处理
location ~ .*\.(js|css)?$
{
root E:\2;
}
#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;
# }
#}}