I’m trying to create a Nginx config which uses PHP-FPM for all routes starting with /craft
and another proxy for all other routes (which should serve an Next.js app):
# Next.js upstream config
upstream nextjs {
server host.docker.internal:3000;
}
# Proxy cache
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:10m inactive=7d use_temp_path=off;
server {
listen 80;
listen [::]:80;
server_name localhost;
# Document root
root /var/www/html/web;
index index.php;
# Logs
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
# Craft CMS
location /craft {
include "/etc/nginx/sites-shared/static-files.conf";
try_files $uri $uri/ /index.php?$query_string;
}
location ~ [^/].php(/|$) {
try_files $uri $uri/ /index.php?$query_string;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
# Next.js's built assets
# Browser cache - max cache headers from Next.js as build id in url
# Server cache - valid forever (cleared after cache "inactive" period)
location /_next/static {
proxy_cache STATIC;
proxy_pass http://nextjs;
}
# Next.js's static assets
# Browser cache - "no-cache" headers from Next.js as no build id in url
# Server cache - refresh regularly in case of changes
location /static {
proxy_cache STATIC;
proxy_ignore_headers Cache-Control;
proxy_cache_valid 60m;
proxy_pass http://nextjs;
}
# Next.js app
location / {
proxy_pass http://nextjs;
}
}
The installation works (running on https://foobar.dev:8843/craft/index.php?p=admin/install), but once I get redirected to https://foobar.dev:8843/craft/admin/dashboard, I get the following error:
2021/02/11 11:39:03 [error] 32#32: *1 rewrite or internal redirection cycle while internally redirecting to "/index.php", client: 172.20.0.1, server: localhost, request: "GET /craft/admin/dashboard HTTP/2.0", host: "foobar.dev:8843", referrer: "https://foobar.dev:8843/craft/index.php?p=admin/install"
That’s my directory structure of /var/www/html
:
.
└── /
├── config
├── modules
├── storage
├── templates
├── vendor
├── web/
│ ├── app/
│ │ └── index.php
│ └── craft/
│ ├── cpresources
│ └── index.php
├── .env
├── composer.json
├── composer.lock
└── craft
https://foobar.dev:8843/app/
works fine also, but it seems that rewrite of URLs does not work and I don’t understand the mentionend redirection cycle in the error message.
2
Answers
It's working fine with this configuration:
I couldn’t get the answer on this page to work. So, after some troubleshooting with clues from this answer, here’s my working nginx config. Hope it helps others.
You will need to modify your alias subfolder and PHP socket or TCP listener.