本文使用的Linux发行版:CentOS6.7 下载地址:https://wiki.centos.org/Download

一、安装Nginx

下载源:wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

安装源:yum install nginx-release-centos-6-0.el6.ngx.noarch.rpm

安装Nginx:yum install nginx

启动Nginx服务:service nginx start

停止Nginx服务:service nginx stop

查看Nginx运行状态:service nginx status

检查Nginx配置文件:nginx -t

服务运行中重新加载配置:nginx -s reload

添加Nginx服务自启动:chkconfig nginx on

二、修改防火墙规则

修改Nginx所在主机的防火墙配置:vi /etc/sysconfig/iptables,将nginx使用的端口添加到允许列表中。

例如:-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT (表示允许80端口通过)

修改Tomcat所在主机的防火墙配置:vi /etc/sysconfig/iptables,将tomcat使用的端口添加到允许列表中。

例如:-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT (表示允许8080端口通过)

如果主机上有多个tomcat的话,则按此规则添加多条,修改对应的端口号即可。

保存后重启防火墙:service iptables restart

三、Tomcat负载均衡配置

Nginx启动时默认加载配置文件/etc/nginx/nginx.conf,而nginx.conf里会引用/etc/nginx/conf.d目录里的所有.conf文件。

因此可以将自己定制的一些配置写到单独.conf文件里,只要文件放在/etc/nginx/conf.d这个目录里即可,方便维护。

创建tomcats.conf:vi /etc/nginx/conf.d/tomcats.conf,内容如下:

 upstream tomcats {
ip_hash;
server 192.168.0.251:8080;
server 192.168.0.251:8081;
server 192.168.0.251:8082;
}

修改default.conf:vi /etc/nginx/conf.d/default.conf,修改如下:

 #注释原有的配置
#location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
#} #新增配置默认将请求转发到tomcats.conf配置的upstream进行处理
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcats; #与tomcats.conf里配置的upstream同名
}

保存后重新加载配置:nginx -s reload

四、静态资源分离配置

修改default.conf:vi /etc/nginx/conf.d/default.conf,添加如下配置:

 #所有js,css相关的静态资源文件的请求由Nginx处理
location ~.*\.(js|css)$ {
root /opt/static-resources; #指定文件路径
expires 12h; #过期时间为12小时
}
#所有图片等多媒体相关静态资源文件的请求由Nginx处理
location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {
root /opt/static-resources; #指定文件路径
expires 7d; #过期时间为7天
}

五、修改SELinux安全规则

如果访问Nginx时出现502 Bad Gateway错误,则可能是Nginx主机上的SELinux限制了其使用http访问权限引起的,输入命令setsebool -P httpd_can_network_connect 1 开启权限即可。

文件/etc/nginx/nginx.conf完整配置如下:

 user  nginx;
worker_processes auto; error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 100000; events {
use epoll;
multi_accept on;
worker_connections 1024;
} http {
include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on;
server_tokens off;
#tcp_nopush on; keepalive_timeout 65; gzip on;
gzip_disable "msie6";
gzip_static on;
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; include /etc/nginx/conf.d/*.conf;
}

文件/etc/nginx/conf.d/default.conf完整配置如下:

 server {
listen 80;
server_name localhost; #charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main; #location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
#} location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://web_servers;
} location ~.*\.(js|css)$ {
root /opt/static-resources;
expires 12h;
} location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {
root /opt/static-resources;
expires 7d;
} #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 /usr/share/nginx/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;
#}
}

(温馨提示:如果执行命令时没有root权限,请在命令前面加上 sudo 执行)

Linux下Nginx+Tomcat负载均衡和动静分离配置要点的更多相关文章

  1. Nginx+Tomcat负载均衡、动静分离群集

    Nginx+Tomcat负载均衡.动静分离群集 目录 Nginx+Tomcat负载均衡.动静分离群集 一.Tomcat 1. Tomcat简介 2. Tomcat重要目录 二.Nginx负载均衡原理 ...

  2. 利用nginx实现负载均衡和动静分离

    1.Nginx介绍 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 . Nginx 是由 ...

  3. Nginx学习系列二Linux下Nginx实现负载均衡

    关于在本地虚拟机(VMware 14)下安装Linux同时安装Nginx,请参考Nginx学习系列之搭建环境 1.启动Nginx 在Nginx安装成功的前提下,启动Nginx 已root模式登陆(权限 ...

  4. linux 下 nginx的负载均衡

    nginx是如何实现负载均衡的,nginx的upstream目前支持以下几种方式的分配: 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除.   2 ...

  5. Linux记录-Nginx+Tomcat负载均衡配置

    Nginx负载均衡配置及策略: 轮询(默认) 优点:实现简单缺点:不考虑每台服务器的处理能力配置示例如下:upstream www.xxx.com {# 需要负载的server列表server www ...

  6. Docker的安装和镜像管理并利用Docker容器实现nginx的负载均衡、动静分离

    Docker的安装 一.Docker的概念 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化 ...

  7. 19.Tomcat多实例部署及负载均衡、动静分离

    Tomcat多实例部署及负载均衡.动静分离 目录 Tomcat多实例部署及负载均衡.动静分离 Tomcat多实例部署 安装jdk 设置jdk环境变量 安装tomcat 配置 tomcat 环境变量 修 ...

  8. Nginx加多个tomcat实现负载均衡,动静分离

    一:Nginx+Tomcat的动静分离 所谓动静分离就是通过nginx(或apache等)来处理用户端请求的图片.html等静态的文件,tomcat(或weblogic)处理jsp.do等动态文件,从 ...

  9. linux+nginx+tomcat负载均衡,实现session同步

    linux+nginx+tomcat负载均衡,实现session同步 花了一个上午的时间研究nginx+tomcat的负载均衡测试,集群环境搭建比较顺利,但是session同步的问题折腾了几个小时才搞 ...

随机推荐

  1. bootstrap无限级分类 jq拓展 之前的无限级分类的封装版~

    git地址:https://github.com/zhangjiahao93/jQ.select HTML部分 <!DOCTYPE html> <html> <head ...

  2. SharePoint 2013 Excel Services ECMAScript 示例之明日限行

    前言:最近遇到一个“明日限行”的功能,北京的交通啊,这个不在今天讨论范围内,暂不吐槽,想想代码开发,还要写WebPart部署,很麻烦,而且部署服务器,需要领导审批,想绕过这个麻烦事儿,就想到客户端了, ...

  3. iOS之自定义pickerview(行驶里程数)

    #pragma mark -- 里程数按钮的点击事件 - (void)mileageBtnClicked:(UIButton *)sender { UIAlertController *alert = ...

  4. 判断用户输入的银行卡号是否正确--基于Luhn算法的格式校验

    开发中,有时候,为了打造更好的用户体验,同时减轻服务器端的压力,需要对于一些如,手机号码,银行卡号,身份证号码进行格式校验 下面是判断银行卡号输入是否正确的代码(基于Luhn算法的格式校验): iOS ...

  5. 设置Hyper V

    1.打开服务器管理器 2.添加角色和功能 3.安装类型 -> 基于角色或基于功能的安装 4.服务器选择 -> 下一步 5.服务器角色 勾选"Hyper V"

  6. Starling中通过PivotX 和 PivotY 修改原点

    一个显示对象的默认原点在左上角.addChild 是将它的左上角放在了父容器的(0, 0)位置. 如果将该显示对象的PivotX 和 PivotY 修改为其宽高的一半,那么它的原点就变到了该对象的中心 ...

  7. 对象映射工具AutoMapper介绍

    AutoMapper是用来解决对象之间映射转换的类库.对于我们开发人员来说,写对象之间互相转换的代码是一件极其浪费生命的事情,AutoMapper能够帮助我们节省不少时间. 一. AutoMapper ...

  8. Create view failed with ORA-01031:insufficient privileges

    有时候在ORACLE数据库创建视图时会遇到:ORA-01031:insufficient privileges错误,我也多次碰到了各种创建视图出错的情况,很多时候也没有太在意,今天被一同事问起这个问题 ...

  9. Linux 格式化扩展分区(Extended)

    如果你在Linux系统中格式化磁盘时遇到如下错误,那么表示你正在格式化一个扩展分区. [root@GETTestLNX06 ~]# mkfs.ext4 /dev/sdb1   mke2fs 1.41. ...

  10. 服务器重启后SQL Server Agent由于"The EventLog service has not been started" 启动失败

    案例环境: 操作系统   : Microsoft Windows Server 2003 Standard Edtion SP2 数据库版本 : SQL Server 2005 Standard Ed ...