Linux下Nginx+Tomcat負載均衡和動靜分離配置要點

本文使用的Linux發行版:CentOS6.7 下載地址:https://wiki.centos.org/Downloadjavascript

1、安裝Nginxphp

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

安裝源:yum install nginx-release-centos-6-0.el6.ngx.noarch.rpmhtml

安裝Nginx:yum install nginxjava

啓動Nginx服務:service nginx startnginx

中止Nginx服務:service nginx stopweb

查看Nginx運行狀態:service nginx statusjson

檢查Nginx配置文件:nginx -tcentos

服務運行中從新加載配置:nginx -s reloadtomcat

添加Nginx服務自啓動:chkconfig nginx on

2、修改防火牆規則

修改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

3、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

4、靜態資源分離配置

修改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 }

5、修改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 執行)

相關文章
相關標籤/搜索