NGINX,将几个本地主机转发到php

编程入门 行业动态 更新时间:2024-10-26 08:29:53
本文介绍了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

本文发布于:2023-10-07 12:43:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469414.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几个   主机   NGINX   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!