V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
dbas
V2EX  ›  NGINX

nginx 反向代理问题

  •  
  •   dbas · 2015-11-30 12:02:21 +08:00 · 3506 次点击
    这是一个创建于 3069 天前的主题,其中的信息可能已经有所发展或是发生改变。
    情况是,只有一个公网 IP: 222.222.222.222 只开了一个端口 80;(不能多开)
    内网有很多 web 服务器,192.168.1.N 每个的端口各不同
    问题来了,如何通过 80 端口去访问到不同的内网 web 服务.

    server {
    listen 80;
    server_name2 22.222.222.222;
      location / {
      proxy_pass http://192.168.1.N; ###这种只能代理到一个,多个的话如何搞
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
    }
    8 条回复    2015-11-30 16:35:09 +08:00
    zjqzxc
        1
    zjqzxc  
       2015-11-30 12:15:07 +08:00   ❤️ 1
    location /web1/{
    }
    location /web2/{
    }
    gowithwind
        2
    gowithwind  
       2015-11-30 12:18:24 +08:00
    upstream frontends {

    server 127.0.0.1:8801;

    server 127.0.0.1:8802;

    server 127.0.0.1:8803;

    server 127.0.0.1:8804;

    server 127.0.0.1:8805;

    server 127.0.0.1:8806;

    server 127.0.0.1:8807;

    server 127.0.0.1:8808;

    }

    server {

    listen 8888; #监听端口

    location / {

    proxy_read_timeout 1800;

    proxy_pass_header Server;

    proxy_set_header Host $http_host;

    proxy_redirect off;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Scheme $scheme;

    proxy_pass http://frontends;

    }

    }
    dbas
        3
    dbas  
    OP
       2015-11-30 12:46:43 +08:00
    @zjqzxc
    你的意思是
    location /web1 {
    proxy_pass http://192.168.1.1:9000; ### <-----------------------------
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /web2 {
    proxy_pass http://192.168.1.2:9001; ###<--------------------------------------------
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    你的意思是这样吗?
    访问 http://222.222.222.222/web1 就反代到 1:9000 http://222.222.222.222/web2 就 http://222.222.222.222/web2
    我就是想要这种效果
    xujif
        4
    xujif  
       2015-11-30 12:49:18 +08:00
    绑定不同的域名,绑定不同的目录。只有这两个方案。
    icegreen
        5
    icegreen  
       2015-11-30 15:03:25 +08:00
    分目录或者域名
    wizardoz
        6
    wizardoz  
       2015-11-30 15:15:01 +08:00
    绑定好多域名
    zzlyzq
        7
    zzlyzq  
       2015-11-30 16:34:25 +08:00 via Android
    分目录有个不好的地方,如果你的 index 里面引入了./img/qwe.img 还能读出文件不?
    zzlyzq
        8
    zzlyzq  
       2015-11-30 16:35:09 +08:00 via Android
    分端口 或者 域名 都可以,还是端口来的实在,有条件可以上域名。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3764 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:28 · PVG 12:28 · LAX 21:28 · JFK 00:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.