Skip to content

Commit

Permalink
v1.1.1:可对location块增加自定义配置参数
Browse files Browse the repository at this point in the history
  • Loading branch information
codingmiao committed Mar 22, 2018
1 parent 1f7232c commit 292cd02
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
21 changes: 21 additions & 0 deletions linuxcfg.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ reload.sh文件如下:
修改第一行,使得sh命令能够切换到您的nginx根目录
通过chmod命令,赋予reload.sh文件执行权限

## 可选操作1、为location块添加参数(自版本1.1.1)
有时候,我们需要对location块添加一下参数,以满足性能,安全性等需求,比如:
```
location ^~ /xx/ {
proxy_pass http://upstream_xx;
deny 192.168.1.2;
deny 192.168.1.3;
}
```
此时,我们可以配置根目录下的文件locationParam.txt(没有则新建一个)
```
>xx
deny 192.168.1.2;
deny 192.168.1.3;
>xx1
proxy_set_header X-Real-IP $remote_addr;
```
">"开头的行表示应用名称,根据前面的约定,会被转为全小写
下面跟着的行就是该应用对应的location块下要加的参数了

## 4、启动
./startup.sh启动服务
您可以通过查看{confPath}/ngineureka_upstream.conf文件,检查配置信息是否正确地添加到nginx
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.wowtools.springcloudext</groupId>
<artifactId>ngineureka</artifactId>
<version>1.1-SNAPSHOT</version>
<version>1.1.1-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.wowtools.springcloudext.ngineureka.service;

import org.jdom.Element;
import org.springframework.stereotype.Service;
import org.wowtools.common.utils.ResourcesReader;
import org.wowtools.springcloudext.ngineureka.pojo.Record;
import org.wowtools.springcloudext.ngineureka.pojo.ServiceRecord;
import org.wowtools.springcloudext.ngineureka.util.Constant;
Expand All @@ -10,7 +10,8 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

/**
* 操作nginx的服务
Expand All @@ -20,6 +21,38 @@
*/
@Service
public class NginxService {
/**
* location块中扩展的参数
*/
private final HashMap<String, String> locationParams;

{
String path = Constant.rootPath + "locationParam.txt";
if (!new File(path).exists()) {
locationParams = new HashMap<>(0);
} else {
try {
String[] strs = ResourcesReader.readStr(Constant.rootPath + "locationParam.txt").split("\n");
HashMap<String, StringBuilder> kv = new HashMap<>();
StringBuilder param = null;
for (String str : strs) {
if (str.length() > 0 && str.charAt(0) == '>') {
param = new StringBuilder();
String key = str.substring(1).trim().toLowerCase();
kv.put(key, param);
} else {
param.append('\t').append(str).append('\n');
}
}
locationParams = new HashMap<>(kv.size());
kv.forEach((k, sb) -> locationParams.put(k, sb.toString()));
} catch (Exception e) {
throw new RuntimeException("解析配置文件locationParam.txt异常", e);
}
}


}

/**
* 将服务信息写入nginx配置并reload
Expand Down Expand Up @@ -51,6 +84,10 @@ private void writeCfg(Record record) {

sbServer.append("location ^~ /").append(appName).append("/ {\n");
sbServer.append("\tproxy_pass http://upstream_").append(appName).append(";\n");
String param = locationParams.get(appName);
if (null != param) {
sbServer.append(param);
}
sbServer.append("}\n\n");
for (String url : urls) {
sbUpstream.append("\tserver ").append(url).append(";\n");
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/locationParam.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
>xx
deny 192.168.1.2;
deny 192.168.1.3;
21 changes: 21 additions & 0 deletions wincfg.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ nginx -s reload
```
修改前两行,使得cmd命令能够切换到您的nginx根目录

## 可选操作1、为location块添加参数(自版本1.1.1)
有时候,我们需要对location块添加一下参数,以满足性能,安全性等需求,比如:
```
location ^~ /xx/ {
proxy_pass http://upstream_xx;
deny 192.168.1.2;
deny 192.168.1.3;
}
```
此时,我们可以配置根目录下的文件locationParam.txt(没有则新建一个)
```
>xx
deny 192.168.1.2;
deny 192.168.1.3;
>xx1
proxy_set_header X-Real-IP $remote_addr;
```
">"开头的行表示应用名称,根据前面的约定,会被转为全小写
下面跟着的行就是该应用对应的location块下要加的参数了

## 4、启动
双击startup.bat启动服务
您可以通过查看{confPath}/ngineureka_upstream.conf文件,检查配置信息是否正确地添加到nginx
Expand Down

0 comments on commit 292cd02

Please sign in to comment.