如何使用Nginx在子文件夹中运行CraftCMS

gblwokeq  于 2023-03-29  发布在  Nginx
关注(0)|答案(2)|浏览(173)

我正在尝试创建一个Nginx配置,它使用PHP-FPM为所有以/craft开始的路由和其他所有路由的另一个代理(应该服务于Next.js应用程序):

# 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;
  }
}

安装工作正常(在https://foobar.dev:8843/craft/index.php?p=admin/install上运行),但当我被重定向到https://foobar.dev:8843/craft/admin/dashboard时,我得到以下错误:
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"
下面是/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/也工作得很好,但似乎重写URL不起作用,我不明白错误消息中提到的重定向周期。

ftf50wuq

ftf50wuq1#

它在此配置下工作正常:

# Craft CMS
  location /craft {
    try_files $uri $uri/ /craft/index.php?$query_string;
  }

  location ~ [^/]\.php(/|$) {
    # 404
    try_files                     $fastcgi_script_name =404;

    # default fastcgi_params
    include                       fastcgi_params;

    # fastcgi settings
    fastcgi_pass                  unix:/var/run/php-fpm/www.sock;
    fastcgi_index                 index.php;
    fastcgi_buffers               8 16k;
    fastcgi_buffer_size           32k;

    # fastcgi params
    fastcgi_param DOCUMENT_ROOT   $realpath_root;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
  }
vcirk6k6

vcirk6k62#

我无法在这个页面上找到答案。所以,在通过this answer的线索进行了一些故障排除后,这是我的工作nginx配置。希望它能帮助其他人。

location ^~ /craft {
            index index.html index.htm index.php;
            alias /home/user/www/craft/web;
            if (!-e $request_filename) { rewrite ^ /craft/index.php last; }
            location ~ \.php$ {
                    try_files $uri =404;
                    fastcgi_split_path_info ^(.+\.php)(.*)$;
                    fastcgi_pass unix:/var/run/php-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
            }
    }

您需要修改别名子文件夹和PHP套接字或TCP侦听器。

相关问题