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 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题