php 后端和 JS 前端的 NGINX 配置
NGINX config for php backend and JS frontend
我正在尝试在 / 下提供我的前端应用程序,但对 /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;
}
}
我已经尝试了几乎所有我能想到的配置组合,但都无法正常工作。大多数时候我以 404 结束。
我的 nginx 和 php docker 容器都安装了相同的 /application 目录。
使用上面的配置,任何对 /oauth2/blah 的请求都被底部的位置块接收,因此返回到我的前端。这可能是我最大的问题 - 在我看来 /oauth2 位置块更多 "specific" 为什么不是 "winning"?
我尝试了注释掉的 try_files 行(看看 index.php 作为 "fallback" 值是否对特异性有影响),nginx 刚刚开始下载 index.php 文件而不是传递请求。帮忙?
只有当您尝试的 URL 恰好是 website.com/oauth2
时,location /oauth2
才会获胜。添加 ^~
,路由将赢得所有以 /oauth2
开头的 URL,如下所示:
location ^~ /oauth2 {
这是我使用的方法:
- 首先尝试提供 js/静态页面
- 如果 1.) 失败,传递给 PHP 后端
- 定义处理位置。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;
}
}
作为参考,我最终找到了一个简单的工作解决方案(如下)。
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;
}
}
我正在尝试在 / 下提供我的前端应用程序,但对 /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;
}
}
我已经尝试了几乎所有我能想到的配置组合,但都无法正常工作。大多数时候我以 404 结束。
我的 nginx 和 php docker 容器都安装了相同的 /application 目录。
使用上面的配置,任何对 /oauth2/blah 的请求都被底部的位置块接收,因此返回到我的前端。这可能是我最大的问题 - 在我看来 /oauth2 位置块更多 "specific" 为什么不是 "winning"?
我尝试了注释掉的 try_files 行(看看 index.php 作为 "fallback" 值是否对特异性有影响),nginx 刚刚开始下载 index.php 文件而不是传递请求。帮忙?
只有当您尝试的 URL 恰好是 website.com/oauth2
时,location /oauth2
才会获胜。添加 ^~
,路由将赢得所有以 /oauth2
开头的 URL,如下所示:
location ^~ /oauth2 {
这是我使用的方法:
- 首先尝试提供 js/静态页面
- 如果 1.) 失败,传递给 PHP 后端
- 定义处理位置。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;
}
}
作为参考,我最终找到了一个简单的工作解决方案(如下)。
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;
}
}