当前位置:   article > 正文

nginx代理请求到内网不同服务器_nginx 根据ip访问不同服务器

nginx 根据ip访问不同服务器

需求:之前用的是frp做的内网穿透,但是每次电脑断电重启,路由或者端口会冲突,现在使用汉土云盒替换frp。
需要把公网ip映射到任意一台内网服务器上,然后在这台内网服务器上用Nginx做代理即可访问内网其它服务器,如果内网仅一台服务器则不需要代理

需要准备三台云服务器,一台带有公网ip,另外两台不需要公网ip,有内网ip即可,只要这三台的内网ip在同一个网段中。

要在 Ubuntu 上安装 Nginx,可以按照以下步骤进行操作:

1.更新系统软件包列表:

sudo apt update
  • 1

2.安装 Nginx:

sudo apt install nginx
  • 1

3.安装完成后,Nginx 服务将会自动启动。可以使用以下命令来检查 Nginx 服务状态

sudo systemctl status nginx

  • 1
  • 2

测试:这里使用了两个相同的项目在两台服务器上运行
切换到/etc/nginx/conf.d 创建nginx.conf文件
若使用80端口是需要备案的

server {
	
    listen 81;  // 服务器1        
    server_name test21.zzb97.com;  //有域名的话可以写在这个位置 上面改成80端口

    location / {
        proxy_pass http://192.168.1.12:8092;  // 这里代理的是前端项目 使用该服务器的ip
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
    }

location /ps_vin/ {
       #  反向代理
       proxy_pass http://192.168.1.12:9872/ps_vin/;  // 这里代理的是后端项目 使用该服务器的ip
    }
}

server {
    listen 82; // 服务器2


    location / {
        proxy_pass http://192.168.1.11:8092;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
    }

location /ps_vin/ {
       #  反向代理
       proxy_pass http://192.168.1.11:9872/ps_vin/;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

服务器2的前端项目的nginx配置文件

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
  worker_connections  1024;
}
http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  /var/log/nginx/access.log  main;
  sendfile        on;
  keepalive_timeout  65;

  server {
    listen       8080;
    server_name  localhost;
    location / {
      alias   /app/;
      index  index.html;
#      try_files $uri $uri/ /index.html;
    }
    error_page  404 /40x.html;
    location = /40x.html {
      root   /usr/share/nginx/html;
    }
    # 后端接口,反向代理
    location /ps_vin/ {
       proxy_pass http://36.150.110.203:82/ps_vin/;
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

项目的请求url

import axios from "axios";
// import NProgress  from 'nprogress'
// import 'nprogress/nprogress.css'
let baseURL = 'http://36.150.110.203:82/ps_vin/'
if(process.env.NODE_ENV == "development") {
    baseURL = 'http://172.16.10.8:8008/ps/'
}

const request = axios.create({
    baseURL: baseURL,
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

后端可以直接使用下面url访问

http://36.150.110.203:82/ps_vin/
  • 1

效果
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/690065
推荐阅读
相关标签
  

闽ICP备14008679号