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

Nginx+Tomcat+Https 服务器负载均衡配置

125次阅读
没有评论

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

由于需要,得搭建个 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 负载均衡配置 相关教程见以下内容

Nginx 负载均衡配置说明 http://www.linuxidc.com/Linux/2016-03/129424.htm

Linux 下 Nginx+Tomcat 负载均衡和动静分离配置要点  http://www.linuxidc.com/Linux/2016-01/127255.htm

Docker+Nginx+Tomcat7 配置简单的负载均衡  http://www.linuxidc.com/Linux/2015-12/125907.htm

Nginx 负载均衡(主备)+Keepalived  http://www.linuxidc.com/Linux/2015-12/126865.htm

使用 Nginx 作为负载均衡器 http://www.linuxidc.com/Linux/2015-12/125789.htm

使用 Nginx 简单实现负载均衡  http://www.linuxidc.com/Linux/2016-08/134443.htm

Nginx 负载均衡与高可用的实现 http://www.linuxidc.com/Linux/2016-04/130350.htm

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

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

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