nginx 与子目录中的 symfony 配合使用时出现问题

nginx 与子目录中的 symfony 配合使用时出现问题

这个问题是从 SO 导入的。我保留了对评论所做的编辑,这样我们就不必再重复一遍了 :)。原始帖子的链接:https://stackoverflow.com/questions/57503107/troub-making-nginx-works-with-symfony-in-subdirectory

我正在尝试使用 nginx 在已经运行的 wordpress 站点旁边设置 Symfony 4。Wordpress 应该管理主页

https://www.my.域名

以及下面的博客文章

https://www.my.domain/conseils

symfony 应用程序应该接管以下所有操作

https://www.my.domain/app

我已阅读默认文档机器人这里这里以及一些故障排除这里。此外,它看起来确实与那个已回答的问题非常接近,但是使用 nginx 而不是 apache:https://stackoverflow.com/questions/53895202/symfony4-routing-inside-a-subfolder

事实上,我仍然无法让它工作。这是我当前的 site.conf,它给出了 404,但即使在 nginx 中启用了调试选项,我也找不到一些有用的日志。

    server {
    listen 80 default_server;
    listen [::]:80 default_server;


    server_name www.my.domain;
    root /var/www/my.domain/html;


    #symfony location block
    location /app {
        alias /var/www/my.domain/app/public;
        index index.php;

        try_files $uri /app/public/index.php/$1 last;

        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;


        error_log /var/log/nginx/project_error.log;
        access_log /var/log/nginx/project_access.log;
    }

    #Wordpress location block
    location / {
        index index.php index.html index.htm;



        try_files $uri $uri/ /index.php?$args;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

        location ~* \.(css|js|ico|gif|jpeg|jpg|webp|png|svg|eot|otf|woff|woff2|ttf|ogg)$ {
         expires max;
        }

        location ~ /\.ht {
            deny all;
        }

    }

}

最后我尝试了一些发现这里。这次,它给了我以下 symfony 路由错误:未找到“GET /app”的路由。在 conf 文件下面:

server {
    listen 80 default_server;
    listen [::]:80 default_server;


    server_name www.my.domain;
    root /var/www/my.domain/html;


    # Symfony location block
    location /app {
        alias /var/www/my.domain/app/public;
        index index.php;

        rewrite ^/app/(.*)$ /$1 break;
        try_files $uri @symfonyFront;


    }
        set $symfonyRoot /var/www/my.domain/app/public;
        set $symfonyScript index.php;
        location @symfonyFront {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                include /etc/nginx/fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $symfonyRoot/$symfonyScript;
                fastcgi_param SCRIPT_NAME /app/$symfonyScript;
                fastcgi_param REQUEST_URI /app$uri?$args;
        }

    #Wordpress location block
    location / {
        index index.php index.html index.htm;



        try_files $uri $uri/ /index.php?$args;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

        location ~* \.(css|js|ico|gif|jpeg|jpg|webp|png|svg|eot|otf|woff|woff2|ttf|ogg)$ {
         expires max;
        }

        location ~ /\.ht {
            deny all;
        }

    }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/www.my.domain/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.my.domain/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

在这两种情况下,wordpress 都按预期工作。主页和博客文章按预期显示。但 symfony 应用程序却不是这样。对于前一种情况,我在访问 my.domain/app 时得到了 404,而后一种情况则给了我 symfony 路由错误。这意味着我实际上正在访问 SF 前端控制器。我得到了内核堆栈跟踪,说在 symfony 中找不到路由 app/。这是完全正确的,因为我希望 symfony 在该目录下工作,并且它不应该是路由的用户。

目前我甚至不确定哪一个能让我接近期望的结果。任何建议都将不胜感激!

编辑:etc/nginx/error.log 激活了调试后显示很多行,但这一行引起了我的注意:

2019/08/15 18:11:42 [警报] 6929#6929:*5 “别名”不能在 URI 重写的位置“/app”中使用,客户端:86.252.250.94,服务器:www.my.domain,请求:“GET /app/HTTP/1.1”,主机:“www.my.domain”

当我收到“未找到路由”错误时,我在 symfony 日志中看到了以下信息(这是预期的):

[2019-08-15 18:26:00] request.ERROR:未捕获的 PHP 异常 Symfony\Component\HttpKernel\Exception\NotFoundHttpException:“未找到“GET /app”的路由”,位于 /var/www/my.domain/app/vendor/symfony/http-kernel/EventListener/RouterListener.php 第 141 行 {“exception”:“[object](Symfony\Component\HttpKernel\Exception\NotFoundHttpException(code:0):未找到 \“GET /app\”的路由,位于 /var/www/my.domain/app/vendor/symfony/http-kernel/EventListener/RouterListener.php:141,Symfony\Component\Routing\Exception\ResourceNotFoundException(code:0):未找到 \“/app/\”的路由。在/var/www/my.domain/app/vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php:70)”} []

EDIT2:我添加了

fastcgi_split_path_info ^/app(.+.php)(/.+)$;

没有明显的变化。有趣的是,当我用“last”而不是“break”替换重写指令时,它会显示来自 wordpress 的 404 页面。

编辑:fastcgi-php.conf

@www:~$ sudo cat /etc/nginx/snippets/fastcgi-php.conf
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

答案1

第一个“错误”是位置前缀 ( location /something) 没有尾部斜杠。它必须是location /app/ {,否则位置将匹配app123

应用程序中的位置try_files过于复杂。保持简单:

try_files $uri /index.php$is_args$args;

考虑到您保留当前的文件结构:

  • /var/www/my.domain/html——WordPress
  • /var/www/my.domain/app——Symfony

然后:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name www.my.domain;
    root /var/www/my.domain/html;

    #Wordpress location block
    location / {
        index index.php index.html index.htm;

        try_files $uri $uri/ /index.php?$args;

        location ~* \.(css|js|ico|gif|jpeg|jpg|webp|png|svg|eot|otf|woff|woff2|ttf|ogg)$ {
         expires max;
        }

        location ~ /\.ht {
            deny all;
        }
    }

    #symfony location block
    location ^~ /app/ {
        index index.php;
        error_log /var/log/nginx/project_error.log;
        access_log /var/log/nginx/project_access.log;
        alias /var/www/my.domain/app/public/;
        try_files $uri /index.php$is_args$args;
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock; 
            # important to use $request_filename with alias
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }

    # redirect /app to app/
    location = /app {
        return 301 /app/;
    }   

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock; 
    }

}

相关内容