资源路径: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. Linux - Shell - 免密码登录

    概述 简述 linux ssh 无密码登录 无能狂怒 最近真是不知道写啥了 环境 os centos7 1. 场景 场景 主机A 需要经常访问 主机B 每次访问, 都要输入一次 密码 问题 每次都输密 ...

  2. Redis 数据结构的底层实现 (一) RealObject,embstr,sds,ziplist,quicklist

    一.realObject Redis使用 string list zset hash set 五大数据类型来存储键和值.在每次生成一个键值对时,都会生成两个对象,一个储存键一个储存值.redis定义了 ...

  3. LitElement(五)事件

    1.概述 1.1 在何处添加事件监听器 您需要以一种可以在事件发生之前触发的方法添加事件监听器.但是,为了获得最佳的加载性能,应尽可能晚添加事件监听器. 你可以在以下位置添加事件监听器: 1.1.1  ...

  4. 新建表需要原表的数据,mysql 如何把查询到的结果插入到新表中

    项目运用情景:新建表需要原表的数据 1. 如果两张张表(导出表和目标表)的字段一致,并且希望插入全部数据,可以用这种方法: INSERT INTO  目标表  SELECT  * FROM  来源表 ...

  5. C++-POJ2503-Babelfish[hash]

    哈个希加挂个链表 一个要背的字符串hash函数ELFhash() mod数取数据最大容量的1.5倍最佳?! #include <set> #include <map> #inc ...

  6. crontab调用python脚本新思路

    crontab调用python脚本文件,有可能失败,定时执行并没有执行,怎么办? 答:写一个shell脚本调用python,然后用crontab调用shell脚本.具体细节,有时间补充. ————— ...

  7. Flink架构(一)- 系统架构

    1. 系统架构 Flink是一个分布式系统,用于有状态的并行数据流处理.也就是说,Flink会分布式地运行在多个机器上.在分布式系统中,常见的挑战有:如何对集群中的资源进行分配与管理.协调进程.数据存 ...

  8. 题解 【洛谷P1035】[NOIP2002普及组]级数求和

    [NOIP2002普及组]级数求和 这个题……用循环也是可以的,不过我写了两种循环的题解,供各位dalao参考!O(∩_∩)O谢谢! for循环版本: #include<bits/stdc++. ...

  9. winform学习(10)设置控件透明背景色

    如何将控件的背景色设置为透明 ①将属性BackColor设置为Web--Transparent ②将属性FlatStyle设置为Flat 如果想将边框去掉: 将属性FlatAppearance下的Bo ...

  10. Http接口安全设计

    1.  完全开放 2. 基本验证 appid(企业唯一标识)+args(请求参数)->sign(摘要). 3. 时效控制 appid+args+timestamp(时间戳)->sign. ...