通过web页面修改nginx配置
资源路径:https://download.csdn.net/download/song_yan_/12002460
nginx动态配置
一、页面展示

二、前端代码
(1)jsp页面(nginxConfig.jsp)
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/plugins/zTree/metroStyle/metroStyle.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/public/nginx.css">
</head>
<body>
<div class="btn_group">
<button type="button" id="btn_add" class="btn btn-primary" onclick="save()">保存</button>
<button type="button" id="btn_add" class="btn btn-primary" onclick="start()">启动</button>
<button type="button" id="btn_add" class="btn btn-primary" onclick="stop()">停止</button>
<button type="button" id="btn_add" class="btn btn-primary" onclick="restart()">重启</button>
</div>
<pre id="editor">${content}</pre>
<script src="${pageContext.request.contextPath}/static/plugins/layui/2.4.3/layui.all.js"></script>
<script src="${pageContext.request.contextPath}/static/plugins/ace/ace.js"></script>
<script src="${pageContext.request.contextPath}/static/plugins/ace/ext-language_tools.js"></script>
<script >
var contextPath = "${pageContext.request.contextPath}"
var $ = layui.jquery //初始化配置文件区域的高度
var height = document.body.clientHeight - 100;
$("#editor").height(height + "px") //初始化文件编辑组件
ace.require("ace/ext/language_tools");
var editor = ace.edit("editor");
editor.setOptions({
enableBasicAutocompletion : true,
enableSnippets : true,
enableLiveAutocompletion : true,
});
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/text");
editor.setFontSize(16); //保存配置文件
function save() {
var fileContent = editor.getValue()
var index = layer.load();
$.ajax({
url : contextPath + "/nginx/save",
data : {
fileContent : fileContent
},
type : "POST",
dataType : 'json',
success : function(data) {
layer.close(index);
if (data.flag) {
layer.alert(data.msg, {
icon : 1
});
} else {
alert()
layer.alert(data.msg, {
icon : 5
});
}
}
})
} //启动nginx
function start() {
var index = layer.load();
$.ajax({
url : contextPath + "/nginx/start",
data : {},
type : "POST",
dataType : 'json',
success : function(data) {
layer.close(index);
if (data.success) {
layer.alert(data.msg, {
icon : 1
});
} else {
layer.alert(data.msg, {
icon : 5
});
}
}
})
} //停止nginx
function stop() {
var index = layer.load();
$.ajax({
url : contextPath + "/nginx/stop",
data : {},
type : "POST",
dataType : 'json',
success : function(data) {
layer.close(index);
if (data.success) {
layer.alert(data.msg, {
icon : 1
});
} else {
layer.alert(data.msg, {
icon : 5
});
}
}
})
} //重启nginx
function restart() {
var index = layer.load();
$.ajax({
url : contextPath + "/nginx/reStart",
type : "POST",
dataType : 'json',
success : function(data) {
layer.close(index);
if (data.success) {
layer.alert(data.msg, {
icon : 1
});
} else {
layer.alert(data.msg, {
icon : 5
});
}
} })
}
</script>
</body>
</html>
(2)Js(见附件)
(3)Css(见附件)
三、配置信息
(1)配置:在global.properties文件中配置

(1)使用(在NginxService.java中会使用到):

四、Controller代码(NginxController .java)
package com.googosoft.controller.fzgn.rz; import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.googosoft.controller.base.BaseController;
import com.googosoft.service.nginx.NginxService; /**
* @author sonyan
* @version 2019年11月27日 下午3:22:59
* @desc nginx
*/ @Controller
@RequestMapping(value="/nginx")
public class NginxController extends BaseController{
@Autowired
private NginxService nginxService; /**
* 跳转到nginx配置页面
* @return
* @throws IOException
*/
@RequestMapping(value="/nginx")
public ModelAndView nginx() throws IOException{
ModelAndView mv = this.getModelAndView();
mv.addObject("content", nginxService.getConfContent());
mv.setViewName("/nginx/nginxConfig");
return mv;
} /**
* nginx重启
* @return
*/
@RequestMapping("/reStart")
@ResponseBody
public Object reStart() {
return nginxService.reStart();
} /**
* 保存配置文件
*/
@RequestMapping("/save")
@ResponseBody
public Object save(String fileContent){
return nginxService.save(fileContent);
} /**
* 代理开启
* @return
*/
@RequestMapping("/start")
@ResponseBody
public Object start() {
return nginxService.start();
} /**
* 代理关闭
* @return
*/
@RequestMapping("/stop")
@ResponseBody
public Object stop() {
return nginxService.stop();
} }
五、Service代码(NginxService .java)
package com.googosoft.service.nginx; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import org.springframework.stereotype.Service; import com.googosoft.controller.fzgn.rz.NginxUtil;
import com.googosoft.service.base.BaseService;
import com.googosoft.util.FileUtil;
import com.googosoft.util.Validate; /**
* @author sonyan
* @version 2019年11月27日 下午4:22:08
* @desc
*/
@Service("nginxService")
public class NginxService extends BaseService{
String nginxPath = ResourceBundle.getBundle("global").getString("nginxPath");
String nginxConfPath = ResourceBundle.getBundle("global").getString("nginxConfPath");
String nginxConfDir = ResourceBundle.getBundle("global").getString("nginxConfDir"); /**
* 获取配置文件的内容
* @return
*/
public String getConfContent() {
try {
return readToString(nginxConfPath,"UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return "";
} /**
* nginx重启
* @return
*/
public Map<String, Object> reStart() {
Map<String, Object> relSet =new HashMap<String, Object>();
boolean success = false;
String msg = "";
if (NginxUtil.getNginxProcessStatus()) {
try {
NginxUtil.killProc();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (NginxUtil.getNginxProcessStatus()) {
msg = "代理关闭时出错";
}
}
try {
NginxUtil.startProc(nginxPath);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (NginxUtil.getNginxProcessStatus()) {
msg = "代理重启成功";
success = true;
} else {
msg = "代理启动时出错";
}
}
}else{
msg = "代理未启动";
}
relSet.put("msg", msg);
relSet.put("success", success);
return relSet;
} /**
* 保存配置文件内容
* @param fileContent
* @return
*/
public Map<String, Object> save(String fileContent) {
Map<String, Object> relSet = new HashMap<>();
try{
FileUtil.writeStringToFile(nginxConfPath, fileContent);
relSet.put("flag", true);
relSet.put("msg", "配置文件保存成功.");
}catch(Exception e){
e.printStackTrace();
relSet.put("flag", false);
relSet.put("msg", "配置文件保存失败.");
}
return relSet;
} /**
* nginx启动
* @return
*/
public Map<String, Object> start() {
Map<String, Object> relSet = new HashMap<>();
boolean success = false;
String msg = ""; if (!NginxUtil.getNginxProcessStatus()) {
try {
NginxUtil.startProc(nginxPath);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (NginxUtil.getNginxProcessStatus()) {
msg = "代理开启成功";
success = true;
} else {
msg = "代理开启失败";
}
}
} else {
msg = "代理已开启";
} relSet.put("msg", msg);
relSet.put("success", success);
return relSet;
} /**
* nginx停止
* @return
*/
public Map<String, Object> stop() {
Map<String, Object> relSet = new HashMap<>();
boolean success = false;
String msg = ""; if (NginxUtil.getNginxProcessStatus()) {
try {
NginxUtil.killProc();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (!NginxUtil.getNginxProcessStatus()) {
msg = "代理关闭成功";
success = true;
} else {
msg = "代理关闭失败";
}
}
}else{
msg = "代理未启动";
} relSet.put("msg", msg);
relSet.put("success", success);
return relSet;
} public String readToString(String filePath,String enconding) throws IOException {
File file = new File(filePath);
Long filelength = file.length();
byte[] filecontent = new byte[filelength.intValue()];
FileInputStream in = new FileInputStream(file);
in.read(filecontent);
in.close();
return new String(filecontent, Validate.isNullToDefaultString(enconding, "UTF-8") );
} }
六、工具类
(1)NginxUtil.java
package com.googosoft.controller.fzgn.rz; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException; /**
* @Description: nginx工具类
* @author songyan
* @date 2019年08月01日 下午05:26:01
*/
public class NginxUtil {
private static InetAddress ip ;
private static int port=80;
private static String NginxWindowsPath = "D://nginx-1.16.1"; /**
* @desc 判断进程是否开启
*/
public static boolean getNginxProcessStatus() {
boolean result = false;
Socket connect = new Socket();
try {
connect.connect(new InetSocketAddress(ip,port), 1000); // 连接服务器,每隔1秒重试
result = connect.isConnected();
return result;
} catch (SocketTimeoutException e) {
result = false;
return result;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
connect.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
} /**
* 跨平台启动nginx
* @throws IOException
*/
public static void startProc(String nginxPath) throws IOException {
startWinProc(nginxPath);
} /**
* 跨平台关闭nginx
* @throws IOException
*/
public static void killProc() throws IOException {
KillWin();
} /**
* 开启windows系统的nginx
* @throws IOException
*/
private static void startWinProc(String nginxPath) throws IOException {
String command = "cmd /c start nginx";
CommandUtil.executeCmd(command, nginxPath);
} /**
* 关闭windows系统的nginx
* @throws IOException
*/
public static void KillWin() throws IOException {
CommandUtil.executeCmd("taskkill /F /IM nginx.exe");
} }
(2)CommandUtil.java
package com.googosoft.controller.fzgn.rz; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader; /**
* @author sonyan
* @version 2019年9月12日 下午5:27:02
* @desc
*/
public class CommandUtil { public static String executeCmd(String command) throws IOException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
String line = null;
StringBuilder build = new StringBuilder();
while ((line = br.readLine()) != null) {
build.append(line);
}
return build.toString();
} public static Process executeCmd(String command, String dirPath) throws IOException {
File dir = new File(dirPath);
String[] str = new String[] {};
return Runtime.getRuntime().exec(command, str, dir);
} public static Process getProcessByProcessBuilder(String command, String filePath) {
Process proc = null;
try {
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", command);
pb.directory(new File(filePath));
proc = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
return proc;
} public static Process getProcessByRuntime(String command, String filePath) {
Process process = null;
try {
process = Runtime.getRuntime().exec(command, null, new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
return process;
} }
通过web页面修改nginx配置的更多相关文章
- 企业级监控软件Zabbix搭建部署之zabbix在WEB页面中的配置
企业级监控软件zabbix搭建部署之zabbix在WEB页面中的配置 企业级监控软件zabbix搭建部署之zabbix在WEB页面中的配置 关于安装请看 http://www.linuxidc.com ...
- tomcat web页面管理应用配置
大部分时候,我们的tomcat服务器都不是部署在本机,那么怎么样不通过ftp/sftp方式来将war包部署到tomcat容器呢? tomcat有提供web页面管理应用的功能. 我们来看看怎么配置实现该 ...
- 新手学分布式-动态修改Nginx配置的一些想法
本人是分布式的新手,在实际工作中遇到了需要动态修改nginx的需求,因此写下实现过程中的想法.Nginx功能强大且灵活,所以这些权当抛砖引玉,希望可以得到大家的讨论和指点.(具体代码在 https:/ ...
- 修改Nginx配置参数解决http状态码:413上传文件大小限制问题
修改Nginx上传文件大小限制 我们使用ngnix做web server的时候,nginx对上传文件的大小有限制,默认是1M. 当超过大小的时候会报413(too large)错误.这个时候我们要修改 ...
- Linux服务器上迁移项目路径,修改nginx配置,迁移及备份MongoDB数据库流程 (超详细)!!!
缘由:客户服务器项目路径不是很合理,导致Jenkins自动部署时还需要添加路径后再更新部署,所以需要把项目路径统一和规范化. 迁移项目路径,保证路径合规,同时做好备份和迁移.迁移后先安装好依赖. 项目 ...
- docker下安装nginx,启动ngixn,修改nginx配置等--超详细
1.获取nginx版本 docker中nginx版本信息:https://hub.docker.com/_/nginx?tab=tags&page=1&ordering=last_up ...
- nginx配置集群
1.准备两个Tomcat 首先在Linux机器上部署两个Tomcat,端口分别为80和8080 2.分别部署测试应用 在两个tomcat下分别部署同一个应用testapp,很简单,就是在页面显示当前系 ...
- Web页面解析过程(浅)
web页面流程 域名解析DNS 域名解析:把域名指向网络空间IP,让人们通过简单的域名访问Web网站的服务. DNS:域名系统 DNS服务器:记录着域名及其对应的IP地址 解析域名: 浏览器中输入 ...
- Nginx配置域名转发实例
域名:cps.45wan.com 所在阿里云主机:123.35.9.12 45wan没有在阿里云备案 67wan已经在阿里云备案 阿里云主机(假如123.35.9.12)上原来的nginx配置: ...
随机推荐
- 实用 docker history
关闭安装认证, 开启tcp 端口 sudo vi /usr/lib/systemd/system/docker.service ExecStart=/usr/bin/dockerd --insecur ...
- Centos7 入门几个操作
http://www.wallcopper.com/linux/1650.html 创建文件软连接 ln -s 源路径 目标路径 查看软连接ls -il 服务操作:systemctl start fo ...
- codeforces 1288E. Messenger Simulator(树状数组)
链接:https://codeforces.com/contest/1288/problem/E 题意:序列p的长度为n,初始序列为1 2 3 4 ...n,然后有m次操作,每次指定序列中一个数移动到 ...
- c数据结构 -- 线性表之 复杂的链式存储结构
复杂的链式存储结构 循环链表 定义:是一种头尾相接的链表(即表中最后一个结点的指针域指向头结点,整个链表形成一个环) 优点:从表中任一节点出发均可找到表中其他结点 注意:涉及遍历操作时,终止条件是判断 ...
- html 未选择复选框不上传
问题 之前就遇到类似的问题,在一个列表中,如果有复选框,并且不选中 会导致这个复选框不上传,导致后台接收不到复选框数据 解决方法我想到的就是 <td> <input type=&qu ...
- php:对象(object)数据类型实例详解
什么是对象? 对象是存储数据和有关如何处理数据的信息的数据类型.是系统中用来描述客观事物的一个实体,它是构成系统的一个基本单位.一个对象由一组属性和对这组属性进行操作的一组服务组成. 语法 在 PHP ...
- Mongo查询list数组中一个字段大于特定条数
由于刚刚接触mongodb,很多语法还不是很了解,写的不好的地方请大佬指出 查询的demo数据 { "_id":Object("xxxxxxxx"), &quo ...
- Wordpress里提示警告信息creating default object from empty value in *** 的解决方法
PHP里提示 Creating default object from empty value 的问题,一般是由于PHP版升级的原因,PHP 5.4 以上的版本一般会报这个错误.PHP的解决方法:找到 ...
- 关于static 关键字的总结
转发自:https://www.cnblogs.com/xrq730/p/4820992.html 前言 之前讲到final关键字的作用是每次面试的时候我必问求职者的两个问题之一,另外一个问题就是文本 ...
- Bugku - 好多压缩包 - Writeup
bugku - 好多压缩包 - Writeup M4x原创,转载请注明出处 这道题前前后后做了好几天,这里记录一下 题目 文件下载 分析 解压下载后的文件,发现有68个压缩文件,并且每个压缩文件里都有 ...