我需要创建一个无法测试的API 网关...但问题是关于管理 PHP 脚本。
我需要什么
- 所有文件 HTML 或 PHP
- 特殊名称作为
my1
和issn
重定向到端口 2018 上的本地主机 - 特殊名称
my2
和my3
重定向到ETC.php
解释
带有"我需要什么"详细信息的伪代码,
if ($uri exists) {
if extension is .php use it with php7.0-fpm.sock
else use it as static page;
} else
try the @proxy_rewrite_engine;
@proxy_rewrite_engine =
if (regex ^(my[23])$ use
ETC.php;
elseif (regex ^/(d+)/my1$ use
http://127.0.0.1:2018?type=int&val=$1
elseif ^/([0-9]+-d+[Xx]?)/issn$ use
http://127.0.0.1:2018?type=str&val=$1
else
say error;
我尝试什么
我有问题的解决方案,请展示一个真正的解决方案,将"解释"部分翻译成具体且正确的 NGINX 脚本代码。
。下面,我错误的 NGINX 脚本寻找线索和灵感,这不是解决方案......需要使用if
代替location
?fastcgi_param
有效吗?我可以对位置进行分组吗?
server {
server_name test.mytest.news;
root /var/www/test;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ @proxy;
}
location ~ .php$ {
try_files $uri =404;
include /etc/nginx/fastcgi.conf; # without SCRIPT_FILENAME
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location @proxy {
rewrite ^?(my[23])$ $document_root/ETC.php?cmd=$1
last;
rewrite ^/(d+)/my1$ ?type=int&val=$1
break;
rewrite ^/([0-9]+-d+[Xx]?)/issn$ ?type=str&val=$1
break;
proxy_pass http://127.0.0.1:2018;
}
include snippets/ssl-test.mytest.news.conf;
} #end server
@cnst注释:假设我在说
"所有文件 HTML 或 PHP"> =try_files
必须尝试其他文件 tham index.htm时需要的是文件夹、图像和其他文件。
如果希望">通过构造(而不是测试(使脚本 100% 可靠">,那么将多个独立问题放在一个问题中可能不是最好的方法。
-
在将请求传递到单独的后端之前必须检查文件是否存在的理由并不完全清楚,特别是如果请求与给定的正则表达式匹配,例如
issn$
. 最好的方法是对案件的每个不同处理使用单独的location
指令。 -
更具体地说,您的方法也存在以下错误:
rewrite ^?(my[23])$
中的正则表达式不太可能匹配任何请求,因为所有外部请求都以斜杠开头。 -
同样,根据 https://serverfault.com/a/864778/110020,
rewrite ^/(d+)/my1$ ?type=int&val=$1
中的替换字符串不太可能是正确的;事实上,当我按照上面尝试你的代码时,以下是我得到的500 Internal Server Error
,根据nginx/error.log
:2017/07/24 08:46:22 [error] 45776#0: *2 the rewritten URI has a zero length, client: 127.0.0.1, server: , request: "GET /44444/my1 HTTP/1.1", host: "localhost:
发生这种情况是因为您确实重写了 URI 以使其成为空 - 问号、
?
以及它之后的任何内容通常不是 nginx 中请求处理中的 URI 的一部分,因此,URI 确实是完全空的。 解决方案:至少在?
前面放一个斜杠(即/?id=
(,或者更好的是,必须激活脚本的全名(特别是因为这只是一个内部重定向(。
Nginx适用于大型配置。坏主意优化nginx.conf。 做简单,它会努力工作:( 我认为这样的配置可以正常工作:
# Try to load existing files
location / {
try_files $uri $uri/ @rewriteIt;
}
# Handle all requests except two other locations below
location @rewriteIt {
# Send it to index.php
rewrite ^(.*)$ /index.php?cmd=$1 last;
}
# This RegExp location will rewrite to ETC.php
# It is more priority, than "/" and @rewriteIt
location ~ ^(my2|my3) $ {
rewrite ^(.*)$ /ETC.php?cmd=$1 last;
include /etc/nginx/fastcgi.conf; # without SCRIPT_FILENAME
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# This RegExp location will proxy to 2018 port
# It is more priority, than "/" and @rewriteIt
location ~ ^(my1|issn) $ {
proxy_pass http://127.0.0.1:2018;
}
# This location handles all php files to php-fpm
location ~ .php$ {
try_files $uri =404;
include /etc/nginx/fastcgi.conf; # without SCRIPT_FILENAME
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}