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.

图片转代码服务由CSDN问答提供 功能建议

我坚持简单的事情,请帮助。 我有两个目录与​​PHP项目:/ var / www / api /和/var/www/api-beta/. 我想将每个转发到PHP-FPM。 Nginx config:

  server {
 listen  80; 
设置$ doc_root / var / www / api; 
 root $ doc_root; 
 index index.php index.html; 
 
 
 location / beta {
别名/ 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; 
 
 
} 
} 
   
 
 

我尝试用 if($ request_uri~ *)做到这一点 / beta)但它不起作用。 我觉得这个问题,因为来自/ var / www / api的项目工作正常,但是从/ var / www / api-beta我有“找不到文件”。 错误。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.
    点赞 评论

相关推荐 更多相似问题