nginx+tomcat简单负载均衡配置

来源:互联网 发布:v版4s支持什么网络 编辑:程序博客网 时间:2024/06/06 02:49
nginx window安装操作手册:
一、安装:
官网:http://nginx.org/

下载:http://nginx.org/en/download.html建议下载稳定版本Stable version

csdn免费下载:nginx1.10.1

安装:下载后解压文件即可


二、操作:
启动:
1.双击解压文件下的nginx.exe

2.cmd窗口下 到解压文件路径下后 命令start nginx或nginx.exe

启动后nginx窗口会闪现一下,查看是否成功启:

a.浏览器访问http://127.0.0.1 或 http://localhost 出现Welcome to nginx!界面表成功

a.启动window任务管理器查看是否有nginx.exe进程存在

b.cmd窗口下命令nginx.exe -t 显示有ok、successful结尾表示已成功启动

c.如果没有进程存在表示失败,失败原因可能是(防火墙阻挡、默认的80端口被占用),

解决方法关闭防火墙,修改端口号(window下查看已使用端口号命令 netstat -an)

3.查看pid:

a.cmd窗口下命令 tasklist /fi "imagename eq nginx.exe"

b.启动window任务管理器查看进程

关闭:

1.window任务管理器中干掉nginx.exe进程

2.nginx -s stop(强制关闭),nginx -s quit(安全关闭)

3.nginx -s reload(修改配置后重新加载配置)

检查配置是否正确:nginx.exe -t

载入指定配置文件:C:\nginx\nginx-1.10.1>start nginx.exe -c conf/default.conf


三:语法规则:

 location [=|~|~*|^~] /uri/ { … }

如:~*^.+(html|jsp|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$  注意:()括号里面代表哪些资源有访问权限,不然页面js/css之类加载不正常

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

~ 开头表示区分大小写的正则匹配

~* 开头表示不区分大小写的正则匹配

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则

/ 通用匹配,任何请求都会匹配到。


nginx配置文件:

#user  nobody;worker_processes  4;#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;    # 设定负载均衡后台服务器列表 ,请求模式为ABABAB.....    upstream mysvr {       server 127.0.0.1:8080;      server 127.0.0.1:8081;    }    # 设定负载均衡后台服务器列表 ,请求模式为ABCABC.....    #upstream mysvr {     #  server 127.0.0.1:8080 weight=1;    #  server 127.0.0.1:8081 weight=2;    #  server 127.0.0.1:8082 weight=3;    #}        # 设定负载均衡后台服务器列表 ,请求模式为AAAA.....,当A服务器挂掉后启用B    #upstream mysvr {     #  server 127.0.0.1:8080;     #  server 127.0.0.1:8081 backup;  #备份服务器B        #}    server {        listen       9090;        server_name  127.0.0.1;        charset utf-8;        #access_log  logs/host.access.log  main;#代理以下服务应用location / {    proxy_pass http://mysvr;  #请求转向mysvr 定义的服务器列表               }#()括号里面代表那些资源有访问权限,不然页面js/css之类加载不正常location ~ .*\.(html|jsp|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {    proxy_pass http://mysvr;  #请求转向mysvr 定义的服务器列表               }        #location / {         #   root   html;         #   index  index.html index.htm;        #}location = /div.jsp {            #root   WebRoot;            index  div2.jsp;        }        #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.根据上面配置,首先准备2个tomcat,分别设置端口号为8080、8081

2.启动nginx服务

3.把项目放到tomcat下,启动两个tomcat

4.浏览器地址访问nginx服务9090端口(127.0.0.1:9090/testWeb)




原创粉丝点击