本地测试代理失败,不能返回正确结果,直接浏览器访问是正常的,是哪个配置项有问题?

ken30

环境描述

我在(windows)本地有个nginx服务器,应用的访问地址是http://localhost:88

我在本地启动了一个webman的服务器,使用http://127.0.0.1:8787,可以正常访问

我想要将8787代理到:88这个应用下,通过http://localhost:88/s 直接访问 8787下的所有应用

例如 http://localhost:88/s/dir/create 转发到 http:127.0.0.1:8787/dir/create 上

我参考了 官网上的【nginx代理】 https://www.workerman.net/doc/webman/others/nginx-proxy.html

配置如下

upstream webman {
    server 127.0.0.1:8787;
    keepalive 10240;
}

server 
{
    listen 88;
    server_name a.com; 
    index index.php index.html index.htm default.php default.htm default.html;
    root d:/dev/proj;

    #PHP-INFO-START 
    include php/74.conf;
    #PHP-INFO-END

    location /api2 {
            alias  d:/dev/proj/api2/;
            index  index.php index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^ /api2/index.php last;
            }

            location ~ \.php$ {
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $request_filename;
                fastcgi_pass 127.0.0.1:20074;
            }
        }

    # 代理服务
    location /s {
          client_max_body_size 20M;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header Host $host;
          proxy_http_version 1.1;
          proxy_set_header Connection "";
          proxy_pass http://webman;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }

    access_log  D:/BtSoft/wwwlogs/a.com.log;
    error_log  D:/BtSoft/wwwlogs/a.com.error.log;
}

访问结果

重启服务后,访问 http://localhost:88/s/dir/create 直接显示 404的页面

访问不成功

在浏览器上 直接访问 http://127.0.0.1:87/dir/create 可以正常访问

是哪个配置出错了呢?

PHP 代码

<?php

namespace app\controller;

use support\Request;

class Dir
{
    public function create(Request $request)
    {
        $dir = $request->post('dir');
        // mkdir($dir);
        return json([ 'code'=> 0, 'dir' => $dir ]);
    }

}
745 3 0
3个回答

latin

访问 http://localhost:88/s/dir/create ,转发给webman请求变成 http://localhost:8787/s/dir/create ,当然404了。
感觉你应该用nginx重写下url变成http://localhost:8787/dir/create,再转发到webman

  • 暂无评论
server {
    listen 80 ;
    server_name web.net;
    location / {
        proxy_pass http://127.0.0.1:8787/;
    }
    error_log /home/wwwlogs/web.error.log;

}
这样配置代理即可
  • 暂无评论
ontheway

你配置的server_name a.com;你用localhost肯定不能访问了

  • 暂无评论
年代过于久远,无法发表回答
🔝