阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux下Nginx+Tomcat负载均衡和动静分离配置要点

200次阅读
没有评论

共计 4874 个字符,预计需要花费 13 分钟才能阅读完成。

本文使用的 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,内容如下:

1 upstream tomcats {2     ip_hash;
3     server 192.168.0.251:8080;
4     server 192.168.0.251:8081;
5     server 192.168.0.251:8082;
6 }

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

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

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

四、静态资源分离配置

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

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

五、修改 SELinux 安全规则

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

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

 1 user  nginx;
 2 worker_processes  auto;
 3 
 4 error_log  /var/log/nginx/error.log warn;
 5 pid        /var/run/nginx.pid;
 6 worker_rlimit_nofile    100000;
 7 
 8 
 9 events {10     use epoll;
11     multi_accept on; 
12     worker_connections  1024;
13 }
14 
15 
16 http {17     include       /etc/nginx/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  /var/log/nginx/access.log  main;
25 
26     sendfile        on;
27     server_tokens off;
28     #tcp_nopush     on;
29 
30     keepalive_timeout  65;
31 
32     gzip on;
33     gzip_disable "msie6";
34     gzip_static on;
35     gzip_proxied any;
36     gzip_min_length 1000;
37     gzip_comp_level 4;
38     gzip_types text/plain text/css application/json application/x-Javascript text/xml application/xml application/xml+rss text/javascript;
39 
40     include /etc/nginx/conf.d/*.conf;
41 }

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

 1 server { 2     listen       80;
 3     server_name  localhost;
 4 
 5     #charset koi8-r;
 6     #access_log  /var/log/nginx/log/host.access.log  main;
 7 
 8     #location / { 9     #    root   /usr/share/nginx/html;
10     #    index  index.html index.htm;
11     #}
12 
13     location / {14         proxy_set_header Host $host;
15         proxy_set_header X-Real-IP $remote_addr;
16         proxy_set_header REMOTE-HOST $remote_addr;
17         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
18         proxy_pass http://web_servers;
19     }
20 
21     location ~.*\.(js|css)$ {22         root    /opt/static-resources;
23         expires     12h;
24     }
25 
26     location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {27         root    /opt/static-resources;
28         expires     7d;
29     }
30 
31     #error_page  404              /404.html;
32 
33     # redirect server error pages to the static page /50x.html
34     #
35     error_page   500 502 503 504  /50x.html;
36     location = /50x.html {37         root   /usr/share/nginx/html;
38     }
39 
40     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
41     #
42     #location ~ \.php$ {43     #    proxy_pass   http://127.0.0.1;
44     #}
45 
46     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
47     #
48     #location ~ \.php$ {49     #    root           html;
50     #    fastcgi_pass   127.0.0.1:9000;
51     #    fastcgi_index  index.php;
52     #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
53     #    include        fastcgi_params;
54     #}
55 
56     # deny access to .htaccess files, if Apache's document root
57     # concurs with nginx's one
58     #
59     #location ~ /\.ht {60     #    deny  all;
61     #}
62 }

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

更多 Nginx 相关教程见以下内容

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-01/127255.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计4874字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中