在Linux上配置Nginx

如何在Linux上去配置Nginx的代理。

1.首先,要在application-pro.yml中配置springBoot项目的项目路径

server:
  port: 8082
  servlet:
    context-path: "/aisocket"

定义:context-path应用的上下文路径,也可以称为项目路径,是构成url地址的一部分。

@RequestMapping("/openAi")
@Controller
public class WebSocketController {
    
    
    @Autowired
    private WebSocketService webSocketService;

    @GetMapping("/index")
    @ResponseBody
    public ModelAndView test(){
    
    

        return new ModelAndView("index");
    }

作用:
如果server.context-path没有配,请求的url地址就是 localhost : openAi/index/
如果server.context-path = “/market/task”, 请求的url地址就是 localhost : /market/task/openAi/index
在 task这个模块下的所有web层的url地址都需要添加server.context-path。

2.然后将springBoot项目打成jar包。放到Linux服务器中。
3.配置Linux服务器的Nginx服务代理。我的Nginx配置文件目录为
cd /etc/nginx 这个下面的nginx.conf是主配置文件
在这里插入图片描述
然后再进入conf.d文件夹,这里面是我们的子配置文件,主配置文件中会自动读取子配置文件中的内容,我们只需要在子配置文件配置即可。

在这里插入图片描述
vi api.openai.com.conf 进入到配置文件。

在这里插入图片描述
序号1:这个名称可以随意,使我们访问Nginx的路径。
序号2:这个名称是我们在springBoot项目的yml配置文件中配置的context-path 的项目路径。

3.重启Nginx服务
启动nginx命令:
systemctl restart nginx

不中断重启
nginx -s reload
检测配置文件是否正确
nginx -t

4.然后就可以进行访问nginx的地址了,记得加上刚才配置的后缀。

猜你喜欢

转载自blog.csdn.net/m0_44980168/article/details/130260081