欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2022-06-02 13:41:25
由于需要,得搭建个nginx+tomcat+https的服务器,搜了搜网上的发现总是有错,现在整理了些有用的,备忘。 环境:centos6.5、jdk1.8、tomc...

由于需要,得搭建个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

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

以上所述是小编给大家介绍的nginx+tomcat+https 服务器负载均衡配置实践方案详解,希望对大家有所帮助