Ngnix做路由转发实现二级域名跳转不同端口的问题

Respberry Pi
2021-06-28 / 0 评论 / 1 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年06月28日,已超过827天没有更新,若内容或图片失效,请留言反馈。

去

前言

之前做的支付功能,直接把ip地址暴漏出去感觉太过危险了,所以想做个反向代理把,路由转发功能,伪装ip

配置

先要找到ngnix的安装地址
ps aux|grep nginx
其实,宝塔里面的配置文件可以直接找到
将域名转发到本地端口,打开配置文件 ngnix.conf文件,下入如下代码

server{
  listen 80;
  server_name  tomcat.shaochenfeng.com;
  index  index.php index.html index.htm;

  location / {
    proxy_pass  http://127.0.0.1:8080; # 转发规则
    proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

这样访问 http://tomcat.shaochenfeng.com 时就会转发到本地的 8080 端口
将域名转发到另一个域名

server{
  listen 80;
  server_name  baidu.shaochenfeng.com;
  index  index.php index.html index.htm;

  location / {
    proxy_pass  http://www.baidu.com;
    proxy_set_header Host $proxy_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

这样访问 http://baidu.shaochenfeng.com 时就会转发到 http://www.baidu.com
本地一个端口转发到另一个端口或另一个域名

server{
  listen 80;
  server_name 127.0.0.1; # 公网ip
  index  index.php index.html index.htm;

  location / {
    proxy_pass  http://127.0.0.1:8080; # 或 http://www.baidu.com
    proxy_set_header Host $proxy_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

这样访问 http://127.0.0.1 时就会转发到本地的 8080 端口或 http://www.baidu.com
在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径
加 /

server_name shaochenfeng.com
location /data/ {
    proxy_pass http://127.0.0.1/;
}

访问 http://shaochenfeng.com/data/index.html 会转发到 http://127.0.0.1/index.html
不加 /

server_name shaochenfeng.com
location /data/ {
    proxy_pass http://127.0.0.1;
}

访问 http://shaochenfeng.com/data/index.html 会转发到 http://127.0.0.1/data/index.ht

结论是:
不加

取消
扫码打赏
支付金额随意哦!
0

评论 (0)

取消
0:00