【问题标题】:Symfony2 + Nginx 404 not foundSymfony2 + Nginx 404 未找到
【发布时间】:2014-09-04 16:09:34
【问题描述】:

我在使用 nginx 配置 Symfony2 时遇到问题

我是这样安装symfony2的

php composer.phar create-project symfony/framework-standard-edition /var/www/symf

一切都很顺利,当我在配置页面上时没有错误

但是当我点击在线配置或 Buypass 配置时,我登陆 nginx 404 NOT FOUND

这里是配置文件

upstream phpfcgi {
server 127.0.0.1:9000;
}

server {
listen 80;

server_name localhost;
root /var/www/symf/web;

error_log /var/log/nginx/symfony2.error.log;
access_log /var/log/nginx/symfony2.access.log;

# strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;

location / {
    index app.php;
    try_files $uri @rewriteapp;
}

location @rewriteapp {
    rewrite ^(.*)$ /app.php/$1 last;
}

# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(app|app_dev|config)\.php(/|$) {
    fastcgi_pass phpfcgi;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param  HTTPS off;
}
}

注意:我注意到当我点击 buypass 时,我登陆了一个类似 ....symf/web/app_dev.php/ 的链接 删除最后/允许我到达页面但得到和错误 点击确定 -> 404 未找到

我错过了什么?

parameters.yml

    database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: null
database_name: symfony
database_user: root
database_password: *****
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: fr
secret: ******
debug_toolbar: true
debug_redirects: false
use_assetic_controller: true

【问题讨论】:

    标签: php symfony nginx


    【解决方案1】:

    删除那部分

    # strip app.php/ prefix if it is present
    rewrite ^/app\.php/?(.*)$ /$1 permanent;
    

    您已经在try file 之后在此处重写,这是应该这样做的

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }
    

    并且 ofc 确保你重新加载你的 nginx 服务

    【讨论】:

    • 谢谢您,但仍然无法访问该页面,找不到 404
    • 您是在本地计算机上安装而不是在服务器上?否则,您应该尝试从 server_name 指令中删除 locahost 引用,并将其替换为默认的 'your-domain.com' 或 ''。如果这不起作用,我们需要有关您的 php 设置的更多信息(即:php5-fpm)。
    • 在服务器上是的,不是在本地,尝试将 server_name 设置为默认值,同样,您到底需要什么? php-fpm.conf 文件?
    • @MuiMui 不在本地??它不会工作。如果查看配置文件夹,则有 routing.ymlrouting_dev.yml 负责项目的本地和生产实例。此外,在 prod 版本中,您可以简单地在 parameters.yml 文件中设置配置
    • @xurshid29 我的 parameters.yml 文件是在安装过程中配置的,我编辑了第一篇文章以向您展示配置。
    【解决方案2】:

    在不了解您的服务器配置的情况下很难提出建议,但这是我的工作配置,它工作得非常好(对于 nginx-php-fpm),可能对您有帮助:

    server {
        listen   80;
        server_name my_site.dev *.my_site.dev;
        root /var/www/my_site.dev/public_html/web;
    
        error_log /var/log/nginx/my_site_error.log;
        access_log /var/log/nginx/my_site_access.log;
    
        rewrite ^/app\.php/?(.*)$ /$1 permanent;
    
        try_files $uri @rewriteapp;
    
        location @rewriteapp {
            rewrite ^(.*)$ /app.php/$1 last;
        }
    
        # Deny all . files
        location ~ /\. {
            deny all;
        }
    
        location ~ ^/(app|app_dev)\.php(/|$) {
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_index app.php;
            send_timeout 1800;
            fastcgi_read_timeout 1800;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    
        # Statics
        location /(bundles|media) {
            access_log off;
            expires 30d;
    
            # Font files
            #if ($filename ~* ^.*?\.(eot)|(ttf)|(woff)$){
            #       add_header Access-Control-Allow-Origin *;
            #}
    
            try_files $uri @rewriteapp;
        }
    }
    

    【讨论】:

      【解决方案3】:

      看起来问题出在 nginx 上,切换到 apache2 并立即工作

      编辑:如果有人知道 nginx 发生了什么,是 conf 文件错误吗?我想知道

      【讨论】:

      • 这听起来可能很愚蠢,但是您是否碰巧在更改配置文件后尝试重新启动 nginx?
      猜你喜欢
      • 2014-09-12
      • 1970-01-01
      • 2016-06-23
      • 2016-12-27
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      相关资源
      最近更新 更多