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

公司有台代理服务器,可以访问谷歌和 chatgpt 的那种,但是想利用 nginx 进行分发

  •  
  •   mrzx · 2023-04-11 17:21:44 +08:00 · 2097 次点击
    这是一个创建于 382 天前的主题,其中的信息可能已经有所发展或是发生改变。

    因为安全问题,不想让外面的人直接访问我们公司内部的代理服务器。

    于是想创建一台服务器,安装 nginx ,并且这台 nginx 对外公布出去。

    并且这里面有个需求,nginx 只能帮忙转发特定 url 给代理服务器。

    自己 google 了很多资料,配置文件改了很多次,没搞定,特此来求助 V2EX 这里的技术大大们。

    要求是,nginx 只转发*.openai.com 的 url 到内网代理服务器 10.114.26.34 8118 上,其他 url 请求不予通过。

    这个配置文件该怎么写?谢谢大家了。

    10 条回复    2023-04-12 20:13:22 +08:00
    Hilong
        1
    Hilong  
       2023-04-11 17:38:49 +08:00   ❤️ 1
    问下 chatgpt 不就可以了,我帮你问了
    可以使用 Nginx 的 location 指令来实现这个需求。具体的配置如下:

    server {
    listen 80;
    server_name example.com;

    location / {
    return 404;
    }

    location /proxy/ {
    proxy_pass http://10.114.26.34:8118;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    }

    location ~* \.openai\.com {
    proxy_pass http://10.114.26.34:8118;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    }
    }
    在上面的配置中,我们首先禁止了所有非 /proxy/ 和 *.openai.com 的请求,返回了 404 错误。然后,我们使用了两个 location 指令来分别处理 /proxy/ 和 *.openai.com 的请求。

    对于 /proxy/ 的请求,我们直接将请求转发到内网代理服务器 10.114.26.34:8118 上,并设置了一些必要的代理头信息。

    对于 *.openai.com 的请求,我们同样将请求转发到内网代理服务器 10.114.26.34:8118 上,并设置了一些必要的代理头信息。

    需要注意的是,上面的配置只是一个示例,实际的配置可能需要根据具体的需求进行调整。另外,为了保证安全性,建议在配置完成后进行测试和审查,确保没有漏洞和安全隐患。
    l4ever
        2
    l4ever  
       2023-04-11 19:20:50 +08:00
    到那个机器开个 nginx
    ```
    worker_processes 1;

    events {
    worker_connections 1024;
    }

    http {
    server {
    listen 80;
    server_name _;
    gzip on;
    gzip_min_length 1k;
    gzip_types text/event-stream;

    auth_basic "Please input password";
    auth_basic_user_file /etc/nginx/passwd;

    location ^~ /v1 {
    proxy_pass https://api.openai.com/v1;
    proxy_set_header Host api.openai.com;
    proxy_ssl_name api.openai.com;
    proxy_ssl_server_name on;
    proxy_set_header Authorization "Bearer sk-嘻嘻嘻";
    proxy_pass_header Authorization;
    proxy_buffering off;
    }

    location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    }
    }
    }

    ```

    然后 http://YOUR_HOST/v1/ 就可以访问到 openai 了.
    mohumohu
        3
    mohumohu  
       2023-04-12 02:17:54 +08:00
    你这个需求用 sniproxy 可能比较方便。或者直接开个 clash 写规则
    defunct9
        4
    defunct9  
       2023-04-12 05:58:35 +08:00 via iPhone
    开 ssh ,让我上去试试
    neighbads
        5
    neighbads  
       2023-04-12 06:42:31 +08:00
    server name 开一个 default 的。直接返回 403
    server name 开一个 openai 的。代理
    mrzx
        6
    mrzx  
    OP
       2023-04-12 11:26:00 +08:00
    @defunct9 这个 nginx 不放在公网上,而是放在我们公司与另外一家公司的一条专线上,方便给别的公司的一台服务器调用我们公司内部的代理服务器。我们公司的代理服务器不能直接放开给其他公司,因为安全考虑,所以只能前面顶个 nginx.
    abonan
        7
    abonan  
       2023-04-12 13:57:36 +08:00
    代理服务器弄一个 nginx server ,然后对外服务器弄个端口转发或者反代

    server {
    listen 80;
    server_name *.test.com;
    # 定义域名前缀
    if ($host ~ "^(.*)\.test\.com$") {
    set $subdomain $1;
    }
    location / {
    # 配置后端服务器
    proxy_pass https://$subdomain.openai.com;
    proxy_ssl_server_name on;
    proxy_redirect off;
    proxy_hide_header Cache-Control ;
    }

    }
    mrzx
        8
    mrzx  
    OP
       2023-04-12 17:22:39 +08:00
    @mohumohu 目标访问过来,不希望加代理,而是通过修改他们公司内部的 DNS ,将 IP 解析成我们对外的这台 nginx 服务器。。可以理解成对方公司访问过来,误以为这台 nginx 就是 openai.com
    mrzx
        9
    mrzx  
    OP
       2023-04-12 17:29:47 +08:00
    大概访问流方向就是
    对方公司服务器----->对方公司 DNS 服务器修改解析记录将*.openai.com 对应的 IP 改成我们 Nginx 的服务器----->通过 ipsec vpn----->访问我们 Nginx------->我们 nginx 代为转发给内部代理服务器-------->公司内部代理服务器通过 fan qiang 专线访问真正的*.openai.com
    mohumohu
        10
    mohumohu  
       2023-04-12 20:13:22 +08:00
    @mrzx 你说的这个就是 sniproxy 的功能。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1097 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:13 · PVG 07:13 · LAX 16:13 · JFK 19:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.