一、搭建项目
我这用的是spring boot,搭建了一个只有一个页面的网站,用来练习nginx。
pom文件中加入了web和thymeleaf。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
controller用来控制跳转页面
@Controller
public class NginxController {
@GetMapping("lx")
public String nginx(){
return "lx"; //返回lx.html
}
}
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Nginx</title>
</head>
<body>
<h1>欢迎━(*`∀´*)来到Nginx</h1>
<img src="../images/614278f64af1d85e4ddc8effab9175d1.jpg">
</body>
</html>
application.properties将端口改成2000
server.port= 2000
然后 maven 打包,将jar包丢到服务器上。
二、发布项目并配置nginx.conf
在服务器创建目录 /opt/app/backend/lx,将jar放入其中。
cd /opt/app/backend/lx/
java -jar nginx-0.0.1-SNAPSHOT.jar 启动命令,注意,这里是前台启动,不能关闭窗口哦
项目启动成功之后我们修改 nginx.conf
vim /usr/local/nginx/conf/nginx.conf
server {
listen 1992; #监听端口
server_name 47.105.198.54; 自己服务器的ip,也可以写localhost
location / {
proxy_pass http://47.105.198.54:2000; #转发后台地址
}
}
我们需要检测1992端口和2000端口有没有打开
#开启防火墙
systemctl start firewalld
# 检测端口
firewall-cmd --query-port=1992/tcp
#开启端口
firewall-cmd --add-port=1992/tcp --permanent
#重启防火墙
systemctl restart firewalld
#记得关闭防火墙
systemctl stop firewalld
-
--permanent:永久生效,没有此参数重启后失效。
然后打开浏览器访问http://47.105.198.54:1992/lx