我正在尝试在/下提供我的前端应用程序,但是请求将/ oauth2传递给php后端。这是我最新的nginx配置尝试:
upstream dockerphp {
server backendphp:9000;
}
server {
listen 80;
server_name localhost;
index index.html;
root /application/frontend/build;
location /oauth2 {
root /application/public;
index index.php;
try_files $uri $uri/ /index.php$is_args$args;
#try_files /index.php$is_args$args =404;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass dockerphp;
fastcgi_index index.php;
}
}
location / {
try_files $uri $uri/ /index.html;
}
}
我已经尝试了几乎我能想到的每个配置组合,但是无法让它工作。大部分时间我最终都是404s。
我的nginx和php docker容器都安装了相同的/应用程序目录。
使用上面的配置,对/ oauth2 / blah的任何请求都被底部的位置块拾取,因此返回到我的前端。这可能是我最大的问题 - 我认为/ oauth2位置块更“具体”,为什么它不“赢”?
我尝试了注释掉的try_files行(看看index.php是否是“回退”值对特异性有影响),并且nginx刚开始下载index.php文件而不是传递请求。救命?
这是我使用的方法:
upstream dockerphp {
server backendphp:9000;
}
server {
listen 80;
server_name localhost;
index index.html;
root /application/frontend/build;
location / {
try_files $uri $uri/ @php;
}
location @php {
root /application/public;
index index.php;
try_files $uri $document_root/index.php?$query_string;
# $document_root/index.php is the important part due to how root and alias directives work
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass dockerphp;
fastcgi_index index.php;
}
}
当你尝试的网址正好是location /oauth2
时,website.com/oauth2
才会获胜。添加^~
,该路线将赢得所有以/oauth2
开头的网址,如下所示:
location ^~ /oauth2 {
作为参考,我最终找到了一个简单的工作解决方案(下)。
upstream dockerphp {
server backendphp:9000;
}
server {
listen 80;
server_name localhost;
index index.html;
root /application/frontend/build;
location / {
try_files $uri $uri/ /index.html;
}
location /oauth2 {
try_files $uri $uri/ @php;
}
location @php {
include /etc/nginx/fastcgi_params;
fastcgi_pass dockerphp;
fastcgi_param SCRIPT_FILENAME /application/public/index.php;
}
}