dtwvr26066 2019-08-14 23:10
浏览 208

麻烦制作nginx与子目录中的symfony一起使用

I'm trying to setup Symfony 4 next to an already working wordpress site with nginx. Wordpress should manage the homepage at

https://www.my.domain

and the blog posts under

https://www.my.domain/conseils

The symfony application should take over for anything under

https://www.my.domain/app

I've read the default documentation bot here and here aswell as some troubleshooting made here. Furthermore, it seems REALLY close to that answered question but with nginx instead of apache: Symfony4 routing inside a subfolder

The fact is i still cant manage to make it working. Here's my current site.conf which gives me a 404, but i cant really find some helpful log even with the debug option enabled in 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;
        }

    }

}

Lastly i tried something found here. This times, it gives me the following symfony routing error: No route found for "GET /app". Below the conf file:

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

}

In both cases, the wordpress is working as intended. Homepage and blog posts are showing when expected. The symfony app however isnt. For the former cas, i got a 404 when visiting my.domain/app and the latter gives me a symfony routing error. That means i'm actually hitting SF front controller. And i got the kernel stack trace saying the route app/ is not found within symfony. And that's absolutely correct since i expect symfony to work under that directory and it should not be user for the routing.

At this point i'm not even sure which one makes me close to the desired result. Any tip would be appreciated!

EDIT: etc/nginx/error.log with debug activated show many lines but this one caught my attention:

2019/08/15 18:11:42 [alert] 6929#6929: *5 "alias" cannot be used in location "/app" where URI was rewritten, client: 86.252.250.94, server: www.my.domain, request: "GET /app/ HTTP/1.1", host: "www.my.domain"

And when i got the route not found error, i got that (which is expected) in the symfony log:

[2019-08-15 18:26:00] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /app"" at /var/www/my.domain/app/vendor/symfony/http-kernel/EventListener/RouterListener.php line 141 {"exception":"[object] (Symfony\Component\HttpKernel\Exception\NotFoundHttpException(code: 0): No route found for \"GET /app\" at /var/www/my.domain/app/vendor/symfony/http-kernel/EventListener/RouterListener.php:141, Symfony\Component\Routing\Exception\ResourceNotFoundException(code: 0): No routes found for \"/app/\". at /var/www/my.domain/app/vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php:70)"} []

EDIT2: i've added the

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

without noticeable change. Interestingly enough, when i replaced the rewrite directive with "last" instead of "break" it displays the 404 page from wordpress.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stata安慰剂检验作图但是真实值不出现在图上
    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题