duaijiao0648 2017-03-27 11:43
浏览 301

Nginx配置多个Yii2高级项目位置(部署开发)

I'm trying to setup ningx server file for one project with multiple versions on separated folders. I read a lot of different concepts and solutions but didn't get anywhere, I don't know anymore what is correct, so..

The purpose, is to build a good architecture while developing an API with different features but don't want to merge to our DEV branch yet, like:

  • api.example.com/v1/user/auth (dev)
  • api.example.com/features/my-new-branch/v1/user/auth (my-new-branch)

1. Structure

/var/www/html/project-api/
    /dev/ 
      api/ <- Advanced Yii2 Environment as frontend/backend
        modules/
          v1/
            controllers/
            models/
        web/
          index.php
      common/
      vendor/
      ...

    /my-new-branch/
      api/
        modules/
          v1/
            controllers/
            models/
        web/
          index.php
      common/
      vendor/
      ...
    /*/ <- Deploying more equal branches for in progress features

2. Nginx

server {
    listen 80;

    root /var/www/html/project-api/src/api/web;
    index index.php index.html index.htm;

    server_name api.example.com;

    error_log /var/log/nginx/project-api/dev-error.log notice;
    access_log off;

    location / {
      try_files $uri $uri/ /index.php$is_args$args;
    }

    ## Features (not working)
    location ~* ^/features/my-new-branch/(.+)$ {
      root /var/www/html/project-api/my-new-branch/api/web;
      try_files /api/web/$1 /api/web/$1/ /features/my-new-branch/index.php?$args;
      index /features/my-new-branch/$1/index.php;

      location ~ /\.(ht|git) {
        deny all;
      }

      location ~ \.php$ {
        try_files $uri =404;

        fastcgi_param SCRIPT_FILENAME $document_root/api/web/$1;
        fastcgi_pass unix:/var/run/php/php-fpm.sock;

        include fastcgi_params;
      }
    }
    ###########

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
      root /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on socket
    #
    location ~ \.php$ {

      try_files $uri =404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php-fpm.sock;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
      fastcgi_index index.php;

      include fastcgi_params;
    }

    location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
      access_log        off;
      log_not_found     off;
      expires           30d;
    }

    # deny access to . files, for security
    #
    location ~ /\. {
      log_not_found off;
      deny all;
     }
}

I have the current server configuration serving my development branch as normal php website, but I not finding a good solution for multiple php project nginx, with this structure on different roots and public folders (web).

Any solution? Alias, root, rewrite, sublocations .. ?

(Later) will be nice having our "my-new-branch" as variable to make this dynamic, as I deploying the new features on project folder.

Thanks

  • 写回答

1条回答 默认 最新

  • duanjin9035 2017-03-27 17:40
    关注

    You can use alias, root, sublocations or separated locations. I think rewrite isn't necessary.

    I would rather do this with Yii2 basic and modules. One enter point and Yii2-app-basic urlMenager do all work. I do not know, if this will help you but look at this Yii2 NGINX advanced config

    sub my-new-branch in Yii2-app-advanced need change config file

        return [
        'homeUrl' => '/features/my-new-branch/',
    
        'components' => [
    
            'request' => [
                'baseUrl' => '/features/my-new-branch',
            ],
        ]
    ]
    
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况