nginx反向代理实验(仅ip)

环境配置

安装nginx,先配置yum源

1
2
cd /etc/yum.repos.d/
vim nginx.conf

nginx.conf文件的内容如下:

1
2
3
4
5
6
7
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

配置完成后,直接yum安装nginx就好了,yum会自动更新并配置上源

1
yum install nginx -y

安装完成后,cd进入目录,将原先的nginx.conf备份

1
2
cd /etc/nginx
mv nginx.conf nginx.conf.bak

然后使用工具cs2modrewrite生成对应你的cs中profile文件的nginx.conf

1
2
3
git clone https://github.com/threatexpress/cs2modrewrite
cd cs2modrewrite
python3 ./cs2nginx.py -i <profile> -c http://<cs监听的地址和端口> -r https://www.baidu.com -H <域名或ip> >/etc/nginx/nginx.conf

举个例子,我的环境如下

  • nginx: 192.168.1.100
  • cs server: 192.168.1.101
  • profile filename: test.profile
1
python3 ./cs2nginx.py -i test.profile -c http://192.168.1.101 -r https://www.baidu.com -H 192.168.1.101 >/etc/nginx/nginx.conf

于是你就会得到这样一份文件

image-20230414094022987

排错

这里面有很多东西都是用不上的,例如xff是为了获取到ip,more_set_headers是为了隐藏标识

所以我们改一下配置文件

  • 首先第12行的内容

image-20230414094110517

我们是root启动,不需要这个,注销

  • 36行的内容

这个需要额外装一个模块来设置,这个说实话没啥必要在,不配置了

image-20230414094132839

  • 132行的规则

image-20230414094150932

这里有个问题,正常来说按照profile生成的内容不会有错,但是在实际的请求中我们可以看到GET请求的地址不只是jquery-3.1.1.min.js,而是/cs/jquery-3.1.1.min.js

image-20230414094209872

这是因为我们对路径进行了修改,所以在配置文件中体现不了的话,生成的规则是有误的(如果没修改的话,无视这条),当你上了重定向器,会发现规则匹配不上而导致无法生效重定向器

image-20230414094316976

所以我们稍微修改一下规则,这样不管前面是什么,只要以这些结尾,都能匹配的上,然后触发反向代理

image-20230414094334952

1
2
3
location ~* (^/.*)(jquery-3.3.1.min.js|vue.min.js|jquery-3.3.2.min.js|bootstrap-2.min.js) {
xxx
}
  • 关闭selinux

配置完成并开启后,发现返回的是504的内容了

image-20230414094401468

这个时候去/var/log/nginx/error.log中查看,会发现如下报错

image-20230414094416042

关闭selinux即可

1
2
3
4
5
6
# 临时关闭,重启后失效
setenforce 0

# 永久关闭,但需要重启机器
vim /etc/selinux/config
SELINUX=enforcing --> SELINUX=disabled

尝试上线

image-20230414094705540

image-20230414094455249