资源路径: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配置的更多相关文章

  1. 企业级监控软件Zabbix搭建部署之zabbix在WEB页面中的配置

    企业级监控软件zabbix搭建部署之zabbix在WEB页面中的配置 企业级监控软件zabbix搭建部署之zabbix在WEB页面中的配置 关于安装请看 http://www.linuxidc.com ...

  2. tomcat web页面管理应用配置

    大部分时候,我们的tomcat服务器都不是部署在本机,那么怎么样不通过ftp/sftp方式来将war包部署到tomcat容器呢? tomcat有提供web页面管理应用的功能. 我们来看看怎么配置实现该 ...

  3. 新手学分布式-动态修改Nginx配置的一些想法

    本人是分布式的新手,在实际工作中遇到了需要动态修改nginx的需求,因此写下实现过程中的想法.Nginx功能强大且灵活,所以这些权当抛砖引玉,希望可以得到大家的讨论和指点.(具体代码在 https:/ ...

  4. 修改Nginx配置参数解决http状态码:413上传文件大小限制问题

    修改Nginx上传文件大小限制 我们使用ngnix做web server的时候,nginx对上传文件的大小有限制,默认是1M. 当超过大小的时候会报413(too large)错误.这个时候我们要修改 ...

  5. Linux服务器上迁移项目路径,修改nginx配置,迁移及备份MongoDB数据库流程 (超详细)!!!

    缘由:客户服务器项目路径不是很合理,导致Jenkins自动部署时还需要添加路径后再更新部署,所以需要把项目路径统一和规范化. 迁移项目路径,保证路径合规,同时做好备份和迁移.迁移后先安装好依赖. 项目 ...

  6. docker下安装nginx,启动ngixn,修改nginx配置等--超详细

    1.获取nginx版本 docker中nginx版本信息:https://hub.docker.com/_/nginx?tab=tags&page=1&ordering=last_up ...

  7. nginx配置集群

    1.准备两个Tomcat 首先在Linux机器上部署两个Tomcat,端口分别为80和8080 2.分别部署测试应用 在两个tomcat下分别部署同一个应用testapp,很简单,就是在页面显示当前系 ...

  8. Web页面解析过程(浅)

    web页面流程 域名解析DNS 域名解析:把域名指向网络空间IP,让人们通过简单的域名访问Web网站的服务. DNS:域名系统 DNS服务器:记录着域名及其对应的IP地址 解析域名: ​ 浏览器中输入 ...

  9. Nginx配置域名转发实例

    域名:cps.45wan.com   所在阿里云主机:123.35.9.12 45wan没有在阿里云备案 67wan已经在阿里云备案 阿里云主机(假如123.35.9.12)上原来的nginx配置: ...

随机推荐

  1. NEKO's Maze Game

    NEKO#ΦωΦ has just got a new maze game on her PC! The game's main puzzle is a maze, in the forms of a ...

  2. js清空子节点

    删除全部子节点 function removeAllChild(){ var div = document.getElementById("div1"); while(div.ha ...

  3. jQuery 扩展 全屏切换

    (function ($) { //全屏切换方法 $.fn.toggleFullScreen = function (qp1, qp2) { var fullFlag = false; var _qp ...

  4. 滑动窗口-洛谷T1866(单调队列)

    咕咕咕 单调队列板子题 一.基本 1.单调队列: 特殊的双端队列,内部元素.分为最大队列(单调递增)和最小队列(单调递减)两种 二.应用 本题中:大部分单调队列优化的动态规划问题都和定长连续子区间的最 ...

  5. C# MVC扩展方法

    控制方法 : 在不修改类原来代码的情况下,给类扩展方法 https://www.cnblogs.com/jxsimon/articles/5043654.html https://www.cnblog ...

  6. APP 安全测试

    http://www.cnblogs.com/wetest/p/6694529.html

  7. Custom LED Keychain, Small And Surefire Gifts

    The    LED Keychain    makes it easy for people to carry their keys with them and carry them with th ...

  8. OSS链接出现 connection pool shutdown错误修改

    在类中创建了OSSClient对象 ,方法共用此实例对象,在前端很短的时间内连续提交,造成异常错误. 解决方法时将OSSClient对象在方法中创建

  9. codeforces div2 603 D. Secret Passwords(并查集)

    题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...

  10. c++ google glog模块安装和基本使用(ubuntu)环境

    1,如何安装 1 Git clone https://github.com/google/glog.git 2 cd glog 3 ./autogen.sh 4 ./configure --prefi ...