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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器