我如何在Nginx中服务嵌套项目 [英] How can I serve nested projects in Nginx

查看:672
本文介绍了我如何在Nginx中服务嵌套项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Lumen api项目,其中包含多个用于api版本控制的git标签.因此,我必须部署该项目的多个签出.

I have a Lumen api project with multiple git tags for api versioning. So I have to deploy multiple checkouts of the project.

服务器上的文件夹结构如下:

The folder structure on the server looks like this:

var
   www
     api-staging
       master
         v1
           public
             index.php
           ...
         v2
           public
             index.php
           ...
         lastest
            public
              index.php
            ...
         ...

现在,我想通过nginx服务项目,以便url看起来像这样.

Now I'd like to serve the projects via nginx so that the url looks something like this.

http://BRANCH.domain.tld/VERSION/例如. http://master.domain.tld/lastest/

我用regexp尝试了很多,但没有任何效果.我希望你能帮助我.

I have tried a lot with regexp, but nothing really worked. I hope you can help me out.

推荐答案

您将需要使用正则表达式server_name语句捕获BRANCH.有关更多信息,请参见本文档.

You will need to capture the BRANCH using a regular expression server_name statement. See this document for more.

通过将/public附加到捕获的VERSION来构造根,这需要正则表达式locationalias语句.有关更多信息,请参见本文档.

The root is constructed by appending /public to the captured VERSION, which requires a regular expression location and an alias statement. See this document for more.

例如:

server {
    ...
    server_name  ~^(?<branch>.+)\.domain\.tld$;

    location ~ ^/(?<version>[^/]+)/(?<name>.*)$ {

        alias /var/www/api-staging/$branch$version/public/$name;

        if (!-e $request_filename) { rewrite ^ $version/index.php last; }

        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }
            include fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $request_filename;
            ...
        }
    }
}

这篇关于我如何在Nginx中服务嵌套项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆