dongza5150 2017-02-09 13:11
浏览 51
已采纳

NGINX,将少数本地主机转发到php-fpm

I've stuck on simple thing, please help. I have 2 directories with PHP projects: /var/www/api/ and /var/www/api-beta/. I want to forwarding each of them to PHP-FPM. Nginx config:

server {
    listen 80;
    set $doc_root /var/www/api;
    root $doc_root;
    index  index.php index.html;


  location /beta {
            alias /var/www/api-beta;
    }


    location ~ \.php$ {
        set $php_root /var/www/api;
        if ($request_uri ~* /beta) {
             set $php_root /var/www/api-beta;
              }

            fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;


    }
}

I've tried do this with if ($request_uri ~* /beta) but it didn't work. I think problem this, because project from /var/www/api works fine, but from /var/www/api-beta I have "File not found." error.

  • 写回答

1条回答 默认 最新

  • doucong1992 2017-02-09 13:46
    关注

    It may be simpler to create a location block for each PHP root:

    server {
        listen 80;
        root /var/www/api;
        index  index.php index.html;
    
        location ~ \.php$ {
            try_files $uri =404;
    
            include /etc/nginx/fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $request_filename;
            fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
        }
    
        location ^~ /beta {
            alias /var/www/api-beta;
    
            location ~ \.php$ {
                if (!-f $request_filename) { return 404; }
    
                include /etc/nginx/fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME  $request_filename;
                fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
            }
        }
    }
    

    Notes:

    • avoid using alias and try_files together. See this long standing issue.
    • the ^~ modifier cause the prefix location to take precedence over the regular expression location above. See this document for more.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?