主要是nginx的安装使用, 至于原理
1. 安装nginx(以及两个tomcat)
2. 使用nginx(测试负载均衡) 想要搭建的测试环境,
1.两个tomcat, 端口分别是80和8090(因为之前安装过一次端口为80的tomcat)
2.nginx端口为8088
  1. nginx安装(linux为例)

    安装目录都在/usr/local下.

    • 安装包获取, 选取Stable version下载

      • 直接下载, (传入虚拟机或服务器) nginx: download

      • 参考上面地址中的版本通过指令 wget http://nginx.org/download/nginx-1.22.1.tar.gz下载

    • 解压安装

      • 解压: tar zxvf nginx-1.22.1.tar.gz

      • 重命名: mv nginx-1.22.1 nginx

      • 进入目录: cd ./ngingx

      • 编译: make

      • 安装: make install

      • 修改配置文件: vim ./conf/nginx.conf

        #user  nobody;
        worker_processes 1; #error_log logs/error.log;
        #error_log logs/error.log notice;
        #error_log logs/error.log info; #pid logs/nginx.pid; events {
        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; sendfile on;
        #tcp_nopush on; #keepalive_timeout 0;
        keepalive_timeout 65; #gzip on;
        # 改动1
        # 新增一个指令块(请求转发都会转发到这里面), 指向两个tomcat的端口
        # weight是权重
        upstream myservers {
        server 127.0.0.1:80 weight=1;
        server 127.0.0.1:8090 weight=1;
        } server {
        # 改动2
        # 端口修改为nginx的端口
        # server_name修改为当前机器的ip或域名
        listen 8088;
        server_name 127.0.0.1; #charset koi8-r; #access_log logs/host.access.log main; # 改动3 将指令块添加到转发规则中
        location / {
        proxy_pass http://myservers;
        proxy_redirect default;
        # root html;
        # index index.html index.htm;
        } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        root html;
        } # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        # proxy_pass http://127.0.0.1;
        #} # ... 省略

        改动1: 添加指令块(名字和改动3中的一致即可)

        改动2: 修改间挺端口, 默认80

        改动3: 添加请求转发规则(http://后面的名字和改动1中一致)

  2. tomcat安装

    • 下载tomcat安装包, 建议安装8版本的 Apache Tomcat 8 Software Downloads

    • 解压重命名指令同nginx

    • 第一个端口为80的tomcat目录为tomcat

    • 修改端口: vim ./tomcat/conf/server.conf

      <!-- 将8080改成80 -->
      <Connector port="80" protocol="HTTP/1.1"
      connectionTimeout="20000"
      redirectPort="8443" />
    • 启动80的tomcat, ./tomcat/bin/start.sh

    • 第二个端口为8090的tomcat目录为tomcat8090(再次解压一个新的)

    • 修改端口: vim ./tomcat8090/conf/server.conf

      主要修改 Server中的port 和 Connector中的port (如果启动失败可百度启动两个不同端口tomcat自行查找问题或者更改更多参数令其完全不同)

      <!-- 指定关机时候的端口 8005改成8006 -->
      <Server port="8005" shutdown="SHUTDOWN"> <!-- AJP协议端口: 将8080改成8089 -->
      <Connector port="8089" protocol="HTTP/1.1"
      connectionTimeout="20000"
      redirectPort="8443" />
    • 启动8090的tomcat: ./tomcat8090/bin/start.sh

  3. 创建一个测试项目

    创建一个只有一个接口的demo项目(maven构件为例)

    • pom: 因为使用tomcat运行, 我们打包方式改成war, 添加上打包插件

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
      <artifactId>demo</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging> <properties>
      <maven.compiler.source>8</maven.compiler.source>
      <maven.compiler.target>8</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties> <dependencies> <!-- web必要依赖 -->
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.7.5</version>
      </dependency> <!-- hutool工具类库 -->
      <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>5.8.9</version>
      </dependency> </dependencies> <build> <!-- 指定打包出来的文件名 -->
      <finalName>test-api</finalName> <plugins> <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>2.5.0</version>
      </plugin> <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <version>3.3.1</version>
      <configuration>
      <!--如果想在没有web.xml文件的情况下构建WAR,请设置为false。-->
      <failOnMissingWebXml>false</failOnMissingWebXml>
      </configuration>
      </plugin> </plugins>
      </build>
      </project>
    • yml: 建两个配置文件testprod, 配置不同的参数, 测试负载均衡

      # 其实一个配置文件打包时候改参数内容就行, 意思都一样,
      # 只是模仿, 分文件还可以通过日志查看哪个配置文件生效了 # application.yml
      spring:
      profiles:
      active: test # application-test.yml
      name: test # application-prod.yml
      name: prod
    • Application: 启动类, 配置自动启动

      package org.example;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.boot.builder.SpringApplicationBuilder;
      import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; /**
      * @author codor
      * @date 2023/03/13 14:22
      */
      @SpringBootApplication
      public class TestApplication extends SpringBootServletInitializer { public static void main(String[] args) {
      SpringApplication.run(TestApplication.class, args);
      } @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
      return builder.sources(TestApplication.class);
      }
      }
    • controller: 写一个接口读配置文件信息

      package org.example.controller;
      
      import cn.hutool.core.date.DatePattern;
      import cn.hutool.core.date.DateUtil;
      import cn.hutool.extra.spring.SpringUtil;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController; import java.util.Date; /**
      * @author codor
      * @date 2023/03/13 14:24
      */
      @RestController
      @RequestMapping("/test")
      public class TestController { @RequestMapping("/time")
      public String time() {
      return SpringUtil.getProperty("name") +
      ": " +
      DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MS_PATTERN);
      }
      }
    • 修改application.yml中激活文件打testprod的两个包修改两个tomcat中的webapps下, 会自动解压部署.

    • 访问各自端口下返回的内容中分别对应着testprod即为正常

  4. 测试nginx

    • 访问http://127.0.0.1:8088/test-api/test/time

    • 反复刷新得到结果中有testprod即为正常, 默认的负载均衡机制应该是轮播.

    • 关闭8090的tomcat: ./tomcat8090/bin/shutdown.sh

    • 再次刷新, 结果只有test的结果为正常.

    • 再次启动8090的tomcat

    • 刷新结果中则恢复之前的情况

      如果tomcat8090中的包关闭后, 刷新会导致一次test一次404


此次记录只是nginx入门简单使用, 作为笔记记录.

原理, 安装, 整合

ngix安装与使用的更多相关文章

  1. linux ngix安装

    因为我完全按照第一篇参考文章从上到下一步步安装导致有些安装失败最后重装的,过程有点乱,就没自己总结please read the follow articles Linux 安装Nginx详细图解教程 ...

  2. windows ngix 安装 配置 使用

    参考版本nginx-1.10.3 一.常用命令 start nginx.exe                      //开启服务 nginx.exe -s stop                ...

  3. Ngix

    Ngix安装 官网地址,下载为源码,需要编译安装 http://nginx.org/ 环境 1.需要安装gcc的环境. yum install gcc-c++ 2.第三方的开发包. PCRE PCRE ...

  4. #在windows上使用ngix重定向目录访问远程服务器文件详细实例

    为了在开发环境保持于生产环境相同的访问远程服务器文件资源的目录配置,需要在开发环境(windows)在远程文件服务器使用nignx重定向文件目录,因为网上的资料大都是copy的,解释比较笼统,也没有具 ...

  5. CentOS 6.5安装和配置ngix

    一.安装配置ngix 这里用wget直接拉取并安装资源文件 首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库). ...

  6. 安装ngix

    第一步:解压源码包 第二步:./configure -->这个时候会提示缺少PCRE 这个时候要安装yum -y install pcre-devel 第三步:./configure --> ...

  7. centos下 yum安装ngix

    1.CentOS 6,先执行:rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6. ...

  8. linux 下安装 python ngix 项目发布流程

    1.安装python #1.安装python3.7所需要的依赖包yum -y groupinstall "Development tools"yum -y install zlib ...

  9. Linux下查看Nginx安装目录、版本号信息?

    Linux环境下,怎么确定Nginx是以那个config文件启动的? 输入命令行: ps  -ef | grep nginx 摁回车,将出现如下图片: master process 后面的就是 ngi ...

  10. nginx 安装及代理配置。

    新建etc/yum.repos.d/nginx.repo文件,添加以下内容:[nginx] name=nginx repo baseurl=http://nginx.org/packages/cent ...

随机推荐

  1. Linux基础第八章:操作系统引导过程、运行级别及开机问题处理

    一.操作系统引导过程 1.开机自检(bios) 2.MBR引导 3.grub菜单 4.加载内核(kernel) 5.init进程初始化 二.操作系统运行级别(init0-init6) 1.init 0 ...

  2. 【Java】时间类型

    Date 转 timeStamp long time = System.currentTimeMillis(); // 秒级 long time = System.currentTimeMillis( ...

  3. Jmeter-接口测试(二)

    鉴权码获取: 1.通过接口获取 appid secret  (第三方用户唯一凭证, 第三方用户唯一凭证秘钥) 2.登录之后自动生成 username,password 一.jmeter 接口关联 1. ...

  4. suse 关闭 图形界面

    图形界面 /etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target ln -s /usr/li ...

  5. c++官方网站汇集

    c++官方网站汇集 gcc官网: https://gcc.gnu.org/ c++参考手册: https://en.cppreference.com/w/cpp c++教程网站: https://ww ...

  6. 图论之最小生成树问题(kruskal)

    最近有几位同学催我更新,于是来摸摸鱼,来讲一下最小生成树问题. 所谓最小生成树(MST),就是在一张无向带权图中的一棵经过所有节点,边权和最小的一棵树.在实际生活中,可以运用于城镇之间的修路上. 对于 ...

  7. 关于IllegalMonitorStateException异常的解释之一

    注意 在同步控制方法或同步控制块里调用wait(),notify()和notifyAll().如果在非同步控制方法里调用这些方法,程序能通过编译,但运行的时候,将得到IllegalMonitorSta ...

  8. Verilog 预编译

    Verilog 预编译 Verilog 语言支持宏定义(`define),参数 parameter,局域参数(localparam)以及`include等内容.这些数据常量的支持极大方便数字系统设计. ...

  9. ubuntu14搭建内网gitlab服务器

    平台 宿主机系统版本:Ubuntu 14.04.5 LTS gitlab软件版本:11.10.8-ee 验证web浏览器版本:Mozilla Firefox 68.3.0esr 安装firefox浏览 ...

  10. P2504 聪明的猴子

    题目描述 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个雨林的地表还是被大水淹没着,部分植物的树冠露在水面上.猴子不会游泳,但跳跃能力比较强,它们仍然可以 ...