自学内容网 自学内容网

CentOS 7.9 64位安装nginx,部署tomcat实现负载均衡

在CentOS 7.9 中安装nginx做负载均衡步骤
用ubuntu试了一下没成功 centos更顺利,当初听到什么nginx,负载均衡,反向代理,redis这些就潜意识就觉得好复杂,今天花一下午时间尝试了一下,感觉也没那么难,至少简单上手还是可以的,毕竟网上资料也多,记录一下实现步骤
具体步骤:
1.先安装centos系统
2.先安装需要的环境:

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

3.下载pcre

 cd /usr/local/src/
 wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
 tar zxvf pcre-8.35.tar.gz

4.打开文件

cd pcre-8.35
./configure
make && make install

5.查看pcre是否安装成功 看到版本号说明成功了

pcre-config --version

6.下载nginx包

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
make
make install

7.查看nginx是否安装成功,看见版本好说明成功了

/usr/local/webserver/nginx/sbin/nginx -v

8.安装jdk11

查看可以下载的jdk yum -y list java*
yum -y install java-11-openjdk.x86_64

9.安装tomcat
下载路径tomcat,这里注意如果你下载最新的要注意jdk版本是否能对上,低版本的jdk在高版本的tomcat上用不了,下载完成上传到虚拟机中
10.修改tomcat里面的端口号,这里修改两个地方
(1) Server标签里的这个port端口,只要不重复就行
(2)Connector 标签里面的 port端口
这里我是部署了3个tomcat所以修改三次

 vi /tmp/apache-tomcat-18081/conf/server.xml 
 <Server port="8005" shutdown="SHUTDOWN"> 
   <Connector port="18081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />
  vi /tmp/apache-tomcat-18082/conf/server.xml 
 <Server port="8006" shutdown="SHUTDOWN"> 
   <Connector port="18082" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />
vi /tmp/apache-tomcat-18083/conf/server.xml 
 <Server port="8007" shutdown="SHUTDOWN"> 
   <Connector port="18081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />

11.启动tomcat,如果tomcat没有启动可以看看里面的日志信息,启动成功就可以访问,我是用的阿里云服务器所以相关的端口在控制的开启
,如果是自己本地安装的服务器需要自己手动开放端口

/tmp/apache-tomcat-18081/bin/startup.sh
/tmp/apache-tomcat-18082/bin/startup.sh
/tmp/apache-tomcat-18083/bin/startup.sh
cat /tmp/apache-tomcat-18081/logs/catalina.out 

12.配置nginx 反向代理实现负载均衡
创建 Nginx 运行使用的用户zqj

 /usr/sbin/groupadd zqj
 /usr/sbin/useradd -g zqj zqj
 cat /usr/local/webserver/nginx/conf/nginx.conf

13.这是我的配置文件里面的重点是配置了,对应找一下改成自己的 upstream 这个后面的tomcatserver别包含特殊符号,不然会报错,运行不起来
1.upstream tomcatserver
2. location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://tomcatserver;
}

user zqj zqj;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
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';
  
#charset gb2312;
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  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-javascript text/css application/xml;
  gzip_vary on;
 
  #limit_zone crawler $binary_remote_addr 10m;
upstream tomcatserver {
        server 127.0.0.1:18081;
        server 127.0.0.1:18082;
        server 127.0.0.1:18083;
}
 #下面是server虚拟主机的配置

 server
  {
    listen 80;#监听端口
    server_name localhost;#域名
    index index.html index.htm index.php;
    root /usr/local/webserver/nginx/html;#站点目录
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass http://tomcatserver;

    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    access_log off;
  }

}

14.启动nginx

/usr/local/webserver/nginx/sbin/nginx

浏览器端访问:ip:80
如我的:http://101.201.171.112:80
我这里是直接输入ip地址也可以
截图留恋,至于怎么把资源文件也加载出来目前还没研究过,还不会弄,想来多半也是在nginx.conf里面配置
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


原文地址:https://blog.csdn.net/qq_37797583/article/details/140501258

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!