服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - Nginx部署SpringBoot项目的实现

Nginx部署SpringBoot项目的实现

2023-03-05 15:38默默前行的蜗牛 Nginx

本文主要介绍了Nginx部署SpringBoot项目的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

笔记记录一下用Nginx部署SpringBoot项目

1、新建一个yml文件 application.yml

?
1
2
3
# 端口号
server:
  port: 2001

2、编写一个Controler测试类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.demo1.controller;
 
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@Component
@RequestMapping("/v1")
public class HelloController {
    final static Logger log = LogManager.getLogger(HelloController.class);
    @Value("${server.port}")
    private int port ;
 
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String test() {
        return "invoke url /,port="+port;
    }
 
    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    public String test1() {
        return "invoke url /test1,port="+port;
    }
 
    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public String test2() {
        return "invoke url /test2,port="+port;
    }
}

3、编写一个启动类

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.example.demo1;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class Demo1Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }
}

4、我用到的pom文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo1</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <log4j.version>2.19.0</log4j.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--            <exclusions>-->
            <!--                <exclusion>-->
            <!--                    <groupId>ch.qos.logback</groupId>-->
            <!--                    <artifactId>logback-classic</artifactId>-->
            <!--                </exclusion>-->
 
            <!--            </exclusions>-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!-- <scope>test</scope> -->
        </dependency>
        <!--日志框架-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!--日志框架-->
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
 
                <groupId>org.apache.maven.plugins</groupId>
 
                <artifactId>maven-assembly-plugin</artifactId>
 
                <version>2.5.5</version>
 
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.example.demo1.Demo1Application</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

5、先在本地测试,启动项目,看到这个就说明启动成功了

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

6、测试,在浏览器中依次输入

http://127.0.0.1:3001/v1
http://127.0.0.1:3001/v1/test1
http://127.0.0.1:3001/v1/test2

在浏览器中能看到端口号的打印信息就说明成功了

7、maven编译打成jar包

8、修改nginx.conf文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
worker_processes  1;
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    sendfile        on;
    
    keepalive_timeout  65;
 
    server {
        listen       89;
        server_name  nginx_server;
 
        location / {
            proxy_pass http://server_ip:3001/v1;
        }
        location /edu {
            proxy_pass http://server_ip:3001/v1/test1;
        }
        location /ymd {
            proxy_pass http://server_ip:3002/v1/test2;
        }
    }
 
}

nginx_server:nginx所在的服务器的地址

server_ip:反向代理的服务器的地址

这里我都是10.161.20.10

7、测试,根据访问的路径跳转到不同的服务中

浏览器中输入:

http://10.161.20.10:90/

?
1
invoke url /,port=3001

http://10.161.20.10:90/test1

?
1
invoke url /test1,port=3001

http://10.161.20.10:90/test2

?
1
invoke url /test2,port=3002

到此这篇关于Nginx部署SpringBoot项目的实现的文章就介绍到这了,更多相关Nginx部署SpringBoot内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大阿家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/ywf008/article/details/128330487

延伸 · 阅读

精彩推荐
  • Nginx小白也可以完成的0基础部署Nginx服务

    小白也可以完成的0基础部署Nginx服务

    这篇文章主要为大家介绍了0基础部署Nginx服务的实现方式,非常简单详细零基础小白跟着做也可以完成,有需要的朋友可以借鉴参考下,希望能够有所帮助...

    、重明7732022-07-31
  • NginxNginx防盗链的3种方法

    Nginx防盗链的3种方法

    Nginx防盗链的3种方法,需要的朋友可以参考下。 ...

    Nginx教程网9162019-10-10
  • Nginxngnix的简单转发请求之server和location配置详解

    ngnix的简单转发请求之server和location配置详解

    这篇文章主要介绍了ngnix的简单转发请求之server和location配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    刘振宁的博客23952020-01-04
  • NginxNginx正反向代理及负载均衡等功能实现配置代码实例

    Nginx正反向代理及负载均衡等功能实现配置代码实例

    这篇文章主要介绍了Nginx正反向代理及负载均衡等功能实现配置代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价...

    绿色的草5722020-03-12
  • Nginx聊聊Django+uwsgi+nginx服务器部署问题

    聊聊Django+uwsgi+nginx服务器部署问题

    这篇文章主要介绍了Django+uwsgi+nginx服务器部署的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...

    Jason 203752022-03-02
  • Nginx配置Nginx服务器防止Flood攻击的方法

    配置Nginx服务器防止Flood攻击的方法

    这篇文章主要介绍了配置Nginx服务器防止Flood攻击的方法,包括PHP的应用请求限制等降低负载的措施,需要的朋友可以参考下 ...

    goldensun5152019-10-29
  • NginxNginx 403 forbidden错误的原因以及解决方法

    Nginx 403 forbidden错误的原因以及解决方法

    yum安装nginx,安装一切正常,但是访问时报403 forbidden,下面这篇文章主要给大家介绍了关于Nginx 403 forbidden错误的原因以及解决方法,需要的朋友可以参考下...

    看那年十七4472022-08-17
  • Nginxnginx location语法使用介绍

    nginx location语法使用介绍

    Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令。Location 指令,是用来为匹配的 URI 进行配置,URI 即语法中的”/uri/”,可以是字符串或正则表达式。但...

    mdxy-dxy3732019-10-25