Nginx入门使用-Linux

一、官网下载安装文件

官网下载地址:http://nginx.org/en/download.html

在这里插入图片描述

二、nginx解压安装

下载后上传到Linux服务器上解压

# 解压文件
[root@localhost applications]# tar -zxvf nginx-1.20.0.tar.gz

安装指令步骤

<font color="red">注意:</font>mark指令需要gcc环境

如果没有gcc环境,需要安装gcc:yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
# 执行配置文件,完成自动配置
[root@localhost nginx-1.20.0]# ./configure 
# 编译安装源文件(需要gcc环境)
[root@localhost nginx-1.20.0]# make
# 查询是否安装完成(查看是否存在,显示具体路径,则安装成功)
[root@localhost nginx-1.20.0]# whereis nginx
nginx: /usr/local/nginx

三、nginx启动

# 进入安装完成后的目录
[root@localhost nginx-1.20.0]# cd /usr/local/nginx
# 启动nginx(没有消息返回,则代表启动成功)
[root@localhost nginx]# sbin/nginx
# 开放80端口号
[root@localhost nginx]# firewall-cmd --permanent --zone=public --add-port=80/tcp
success
# 重载防火墙配置
[root@localhost nginx]# firewall-cmd --reload
success

访问IP地址,查看启动状态
在这里插入图片描述

四、nginx常用命令

# 启动
[root@localhost nginx]# sbin/nginx
# -s quit是正常停止。
[root@localhost nginx]# sbin/nginx -s quit
# -s stop是强制停止
[root@localhost nginx]# sbin/nginx -s stop
# 启动状态下重新加载配置文件
[root@localhost nginx]# sbin/nginx -s reload
# 查看nginx进程
[root@localhost nginx]# ps aux|grep nginx

五、nginx实战演示配置管理

5.1. 打包生产springboot的jar包

  1. 打包生产hello1.jar
    创建springboot项目,编写HelloController类
package com.ndemo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    
    
    @RequestMapping("hello")
    public String hello(){
    
    
        return "hello1";
    }
}
  1. 修改打包的项目名称
    在这里插入图片描述
    4.打包生产hello2.jar
    修改项目HelloController类的返回值和项目名称
    在这里插入图片描述
    在这里插入图片描述

5.2、部署项目:

把得到的两个jar包上传到Linux上并启动
在这里插入图片描述
启动hello1.jar(默认8080端口)
在这里插入图片描述
再开启一个远程连接,启动hello2.jar(指定8081端口)
在这里插入图片描述

5.3、配置nginx的反向代理和负载均衡

修改nginx的配置文件

[root@localhost nginx]# vim conf/nginx.conf

在这里插入图片描述
重载nginx的配置文件

[root@localhost nginx]# sbin/nginx -s reload

5.4、访问查看结果

在这里插入图片描述
刷新页面
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42273775/article/details/118035250