环境需求

使用 nginx 配置反向代理,将 web 根目录与 web 二级目录映射到不同的 ip 和端口 例如: 10.11.10.131 -> 10.11.10.131:8090; 10.11.10.131/vul/->10.11.10.131:8080

配置方法:

# 安装nginx
sudo apt install nginx
# 配置文件
sudo vi /etc/nginx/conf.d/nginx.conf

配置文件内容: 在配置文件中的 http 部分加入下面的配置

    server {
            listen    80; #nginx监听的端口
            server_name    10.11.10.131; #nginx监听的地址
            charset    utf-8;

            location / {
                proxy_pass http://10.11.10.131:8090;
            } 

            location ^~/vul/ {
                rewrite ^/vul/(.*)$ /$1 break;
                proxy_pass http://10.11.10.131:8080;
          }
}

重新加载配置文件,生效设置

sudo nginx -t
sudo nginx -s reload

关键代码(坑):

location / {
    sub_filter '<a href="http://127.0.0.1:8090/'  '<a href="https://$host/';
    sub_filter '<img src="http://127.0.0.1:8090/' '<img src="https://$host/';
    sub_filter_once on;
}

这一部分很关键,如果不对这一部分进行设置,nginx 将不会解析文件中的相对路径,从而导致 web 服务中很多设置无法正常运行