本文介绍了NGINX,将几个本地主机转发到php-fpm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我坚持简单的事情,请帮忙. 我有2个包含PHP项目的目录:/var/www/api/和/var/www/api-beta/. 我想将它们中的每一个转发到PHP-FPM. Nginx配置:
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; } }我尝试使用if ($request_uri ~* /beta)来执行此操作,但是它不起作用.我认为这是有问题的,因为来自/var/www/api的项目工作正常,但是来自/var/www/api-beta的项目却显示找不到文件".错误.
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.
推荐答案为每个PHP根目录创建location块可能更简单:
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; } } }注意:
- 避免同时使用别名和try_files.请参见这个长期存在的问题.
- ^~修饰符使前缀位置优先于上述正则表达式位置.有关更多信息,请参见本文档.
- 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.
更多推荐
NGINX,将几个本地主机转发到php
发布评论