Nginx虚拟目录和Lumen框架路由配置

2020-05-22  本文已影响0人  流浪君子

定一个虚拟目录到lumen开发的api新项目

首先nginx添加一个配置文件

server {

        listen       8083;

        server_name 127.0.0.1;

        index index.html index.htm index.php;

        root  /home/website/wechat-api/public;

## log begin

        set $log_val '';

lua_need_request_body on;

        access_by_lua_file      /usr/local/openresty/nginx/lua/access.lua;

        body_filter_by_lua_file /usr/local/openresty/nginx/lua/body_filter.lua;

        log_by_lua_file         /usr/local/openresty/nginx/lua/log.lua;

        ## log end

add_header Access-Control-Allow-Origin *;

add_header Access-Control-Allow-Headers X-Requested-With;

add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;

         location / {

            if (!-e $request_filename) {

                rewrite . /index.php last;

                break;

           }

        }

        location ~ .*\.(php|php5)?$ {

           fastcgi_pass 127.0.0.1:9000;

           fastcgi_index index.php;

           fastcgi_param  SCRIPT_FILENAME /home/website/wechat-api/public$fastcgi_script_name;

           include fastcgi_params;

        }

}

然后再需要代理的域名配置文件中配置

这个代理会在访问项目时路由会带上虚拟目录,比如laravel5 和 lumen路由就会报错

location /WECHAT

    {

        proxy_pass http://127.0.0.1:8083;

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;

proxy_set_header X-Forwarded-Scheme  $scheme;

    }

所以最好方式就是nginx端就去掉路由的虚拟目录

location /WECHAT

    {

proxy_redirect     off;

proxy_set_header   Host             api.xiaozhangbang.org;

proxy_set_header   X-Real-IP        $remote_addr;

proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

        proxy_pass http://127.0.0.1:8083/;

    }

代理成功了,但是在lumen路由中识别不了,修改框架路由组件

/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php

/**

     * Dispatch the incoming request.

     *

     * @param  SymfonyRequest|null  $request

     * @return Response

     */

    public function dispatch($request = null)

    {

        list($method, $pathInfo) = $this->parseIncomingRequest($request);

*****        $pathInfo = str_replace(env('ROUTINGPATH','/WECHAT'), '', $pathInfo);

        try {

            return $this->sendThroughPipeline($this->middleware, function () use ($method, $pathInfo) {

                if (isset($this->routes[$method.$pathInfo])) {

                    return $this->handleFoundRoute([true, $this->routes[$method.$pathInfo]['action'], []]);

                }

                return $this->handleDispatcherResponse(

                    $this->createDispatcher()->dispatch($method, $pathInfo)

                );

            });

        } catch (Exception $e) {

            return $this->sendExceptionToHandler($e);

        } catch (Throwable $e) {

            return $this->sendExceptionToHandler($e);

        }

    }

上一篇 下一篇

猜你喜欢

热点阅读