Nginx+Tomcat配置负载均衡-动静分离(二)

配置动静分离的时候遇到了一些问题,一个是配置nginx配置文件有问题导致访问不到服务器,另一个问题是配置静态资源的路径和实际的资源目录不匹配导致404,502等错误

结合上一篇的基础,在此将动静分离的配置文件主要部分做一说明,为日后参考。

  1 #user  nobody;
  2 worker_processes  1;
  3
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7
  8 #pid        logs/nginx.pid;
  9
 10
 11 events {
 12     worker_connections  1024;
 13 }
 14
 15
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19
 20     #log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
 21     #                  ‘$status $body_bytes_sent "$http_referer" ‘
 22     #                  ‘"$http_user_agent" "$http_x_forwarded_for"‘;
 23
 24     #access_log  logs/access.log  main;
 25
 26     #sendfile        on;
 27     #tcp_nopush     on;
 28
 29     keepalive_timeout  0;
 30     #keepalive_timeout  65;
 31
 32     # ******************* step one ****************
 33     upstream tomcats {
 34         server 192.168.248.130:8080 weight=3;
 35         server 192.168.248.132:8080 weight=1;
 36     }
 37
 38     #gzip  on;
 39
 40     server {
 41         listen       80;
 42         server_name  localhost;
 43
 44         #charset koi8-r;
 45
 46         #access_log  logs/host.access.log  main;
 47
 48
 49         location ~.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
 50             root /usr/local/nginx/html;
 51         }
 52
 53         location ~ .*\.(html|jsp|do|action)$ {  #动态
 54            proxy_pass http://tomcats;
 55            root   html;
 56            index  index.html index.htm;
 57         }
 58
 59         #error_page  404              /404.html;
 60
 61         # redirect server error pages to the static page /50x.html
 62         #
 63         error_page   500 502 503 504  /50x.html;
 64         location = /50x.html {
 65             root   html;
 66         }
 67
 68         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 69         #
 70         #location ~ \.php$ {
 71         #    proxy_pass   http://127.0.0.1;
 72         #}
 73
 74         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 75         #
 76         #location ~ \.php$ {
 77         #    root           html;
 78         #    fastcgi_pass   127.0.0.1:9000;
 79         #    fastcgi_index  index.php;
 80         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 81         #    include        fastcgi_params;
 82         #}
 83
 84         # deny access to .htaccess files, if Apache‘s document root
 85         # concurs with nginx‘s one
 86         #
 87         #location ~ /\.ht {
 88         #    deny  all;
 89         #}
 90     }
 91
 92
 93     # another virtual host using mix of IP-, name-, and port-based configuration
 94     #
 95     #server {
 96     #    listen       8000;
 97     #    listen       somename:8080;
 98     #    server_name  somename  alias  another.alias;
 99
100     #    location / {
101     #        root   html;
102     #        index  index.html index.htm;
103     #    }
104     #}
105
106
107     # HTTPS server
108     #
109     #server {
110     #    listen       443 ssl;
111     #    server_name  localhost;
112
113     #    ssl_certificate      cert.pem;
114     #    ssl_certificate_key  cert.key;
115
116     #    ssl_session_cache    shared:SSL:1m;
117     #    ssl_session_timeout  5m;
118
119     #    ssl_ciphers  HIGH:!aNULL:!MD5;
120     #    ssl_prefer_server_ciphers  on;
121
122     #    location / {
123     #        root   html;
124     #        index  index.html index.htm;
125     #    }
126     #}
127
128 }

点击查看 nginx配置文件

静态资源目录说明

如果工程名字叫做hello,则访问hello项目的静态资源,需要在nginx的静态资源配置目录下面放置相应的工程名文件夹

原文地址:https://www.cnblogs.com/alan0521/p/10262346.html

时间: 2024-08-24 11:29:42

Nginx+Tomcat配置负载均衡-动静分离(二)的相关文章

nginx+apache实现负载均衡+动静分离配置(编译安装)

一.编译安装nginx cd /usr/local/src wget http://nginx.org/download/nginx-1.6.3.tar.gz tar -zxvf nginx-1.6.3.tar.gz cd nginx-1.6.3 ./configure --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_image_filter_module --with-http_sub_m

Nginx+Tomcat——配置负载均衡和动静分离(实战!)

Nginx应用 Nginx是一款非常优秀的HTTP服务器软件 支持高达50000个并发连接数的响应 拥有强大的静态资源处理能力 运行稳定 内存.CPU等系统资源消耗非常低 目前很多大型网站都应用Nginx服务器作为后端网站程序的反向代理及负载均衡器,提升整个站点的负载并发能力 Nginx负载均衡实现原理 Nginx配置反向代理的主要参数 upstream 服务池名{} 配置后端服务器池,以提供响应数据 proxy_ _pass http://服务池名 配置将访问请求转发给后端服务器池的服务器处理

Nginx + Tomcat 配置负载均衡集群

一.Hello world 1.前期环境准备 准备两个解压版tomcat,如何同时启动两个tomcat,请看我的另一篇文章<一台机器同时启动多个tomcat>. nginx官网下载解压版nginx. 创建一个简单的web项目.为了直观的区分访问的哪个tomcat,在页面写上标记8081.8082. 分别部署到对应的tomcat下.如图:???? ? 2.配置nginx 进入nginx-1.10.1\conf路径,修改配置文件nginx.conf. 1.配置服务器组,在http{}节点之间添加u

Nginx + Tomcat 配置负载均衡集群简单实例

一.Hello world 1.前期环境准备 准备两个解压版tomcat,如何同时启动两个tomcat,请看我的另一篇文章<一台机器同时启动多个tomcat>. nginx官网下载解压版nginx. 创建一个简单的web项目.为了直观的区分访问的哪个tomcat,在页面写上标记8081.8082. 分别部署到对应的tomcat下.如图:???? ? 2.配置nginx 进入nginx-1.10.1\conf路径,修改配置文件nginx.conf. 1.配置服务器组,在http{}节点之间添加u

nginx + tomcat配置负载均衡

目标:Nginx做为HttpServer,连接多个tomcat应用实例,进行负载均衡. 注:本例程以一台机器为例子,即同一台机器上装一个nginx和2个Tomcat且安装了JDK1.7. 1.安装Nginx安装Nginx教程 2.配置两个Tomcat在本机上配置两个Tomcat,分别为tomcat7-8081.tomcat7-8082.tomcat7-8081访问地址:http://localhost:8081,浏览显示内容:this is 8081 porttomcat7-8082访问地址:h

Nginx--Windows环境下Nginx+tomcat配置(包括动静分离)

前提条件: (1)已安装好tomcat,且能成功启动 (2)已安装好Nginx,且能成功启动 接下来进行配置: (1)在Nginx的conf文件夹中新增两个文件,分别如下:(新建文件后,直接复制代码即可) 文件1:Proxy.conf proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_ad

nginx+tomcat配置负载均衡集群

一.Hello world 1.前期环境准备 准备两个解压版tomcat,如何同时启动两个tomcat,方法如下:首先去apache tomcat官网下载一个tomcat解压版.解压该压缩包,生成n份tomcat 分别命名为 tomcat1,tomcat2,然后修改server.xml配置文件,分别进入tomcat/conf/目录,修改server.xml,一共三处.第一处:第二处.tomcat访问端口号:第三处:之后修改bin下的启动文件分别进入tomcat/bin目录,修改 startup.

nginx+tomcat 配置负载均衡

nginx 从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最新版本(我用的是nginx-1.8.1版本) 安装就直接把压缩包解压到一个路径下(路径最好是没有中文非空的) 在cmd里切换到部署目录路径   启动Nginx:start nginx   停止Nginx:nginx -s stop 查看进程,如果看到    这两个进程就说明你nginx已经成功启动 成功启动nginx之后,在浏览器里访问http://localhost,可以看

Nginx+Tomcat+Keepalived+Memcache 负载均衡动静分离技术

一.概述 Nginx 作负载均衡器的优点许多,简单概括为: ①实现了可弹性化的架构,在压力增大的时候可以临时添加Tomcat服务器添加到这个架构里面去; ②upstream具有负载均衡能力,可以自动判断下面的机器,并且自动踢出不能正常提供服务的机器: Keepalived 可实现 Nginx负载均衡器双机互备,任意一台机器发生故障,对方都能够将虚拟IP接管过去. Memcache可以实现Tomcat服务器的Sission共享整个拓补如下: 注意: 1.由于服务器有限,IP相同的为同一台机.只是端