Nginx+Tomcat+Https 服务器负载均衡配置实践方案详解

由于需要,得搭建个nginx+tomcat+https的服务器,搜了搜网上的发现总是有错,现在整理了些有用的,备忘。

环境:Centos6.5、JDK1.8、Tomcat8、Nginx1.10.1

准备材料:

1.JDK1.8安装包jdk-8u102-linux-x64.tar.gz

2.Tomcat8安装包apache-tomcat-8.0.37.tar.gz

3.Nginx1.10安装包nginx-1.10.1.tar.gz

1、JDK安装配置

解压并安装到/usr/local/jdk

[root@localhost ~]# tar zxvf jdk-8u102-linux-x64.tar.gz
[root@localhost ~]# mv jdk1.8.0_102 /usr/local/jdk

配置JDK环境变量

[root@localhost ~]# vi /etc/profile

在底部加入以下内容

JAVA_HOME=/usr/local/jdk
JRE_HOME=$JAVA_HOME/jre
CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
export JAVA_HOME JRE_HOME PATH CLASSPATH

应用环境变量

[root@localhost ~]# source /etc/profile

检测是否成功,显示版本说明成功

[root@localhost ~]# java -version

2、Tomcat安装配置

解压并安装到/usr/local/tomcat

[root@localhost ~]# tar zxvf apache-tomcat-8.0.37.tar.gz
[root@localhost ~]# mv apache-tomcat-8.0.37 /usr/local/tomcat

默认tomcat是root身份运行的,这样不安全,这里设置普通用户运行

[root@localhost ~]# groupadd tomcat
[root@localhost ~]# useradd -g tomcat tomcat
[root@localhost ~]# passwd tomcat
[root@localhost ~]# chown tomcat.tomcat -R /usr/local/tomcat

运行tomcat

[root@localhost ~]# su - tomcat /usr/local/tomcat/bin/startup.sh

设置开机启动

[root@localhost ~]# echo "su - tomcat /usr/local/tomcat/bin/startup.sh" >> /etc/rc.local

3、Nginx安装配置

配置Nginx用户

[root@localhost ~]# groupadd nginx
[root@localhost ~]# useradd -g nginx -s /sbin/nologin nginx

安装依赖包

[root@localhost ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++

解压并进入文件夹内

[root@localhost ~]# tar zxvf nginx-1.10.1.tar.gz
[root@localhost ~]# cd nginx-1.10.1

配置安装

[root@localhost nginx-1.10.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module
[root@localhost nginx-1.10.1]# make && make install

配置Nginx

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf

这一步需要手动将ssl证书放入/usr/local/nginx/conf/目录下,分别为cert.crt和cert.key文件

如果证书文件是其他格式,可以自行搜索转换方法

如果无须配置https,更改443端口即可

nginx主配置文件

user nginx;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
use epoll;
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;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 6 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascripttext/css application/xml;
gzip_vary on;
server {
listen 80;
server_name www.domain.com; #修改域名
return 301 https://$server_name$request_uri; #强制跳转443端口
}
server {
listen 443 ssl;
server_name www.domain.com; #修改域名
ssl_certificate cert.crt; #导入证书
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 /usr/local/tomcat/webapps/ROOT;
index index.html index.jsp index.htm;
}
location ~ .*.jsp$ {
index index.jsp;
proxy_pass http://127.0.0.1:8080;
}
location /nginxstatus {
stub_status on;
access_log on;
auth_basic "nginxstatus";
auth_basic_user_file /usr/local/nagois/etc/htpasswd.users;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

启动服务器

/usr/local/nginx/sbin/nginx

浏览器访问出现小猫即成功。

相关推荐