How can I serve nested projects in Nginx

*爱你&永不变心* 提交于 2020-05-30 06:55:07

问题


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
            ...
         ...

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

http://BRANCH.domain.tld/VERSION/ eg. http://master.domain.tld/lastest/

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


回答1:


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

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.

For example:

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;
            ...
        }
    }
}


来源:https://stackoverflow.com/questions/45592289/how-can-i-serve-nested-projects-in-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!