【问题标题】:Nginx with symfony2 File not found找不到带有 symfony2 文件的 Nginx
【发布时间】:2015-07-18 17:59:13
【问题描述】:

我正在使用 nginx 配置虚拟主机。当我输入地址时,页面返回此错误:找不到文件。 我的配置是下一个:

server {
listen 80;
server_name vcarlos.lan;

root /home/tfc_dev/tfc/web;

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

index app.php index.html index.htm;

try_files $uri $uri/ @rewrite;

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

location ~ \.php(/|$) {
    # try_files $uri =404;

    fastcgi_index app.php;
    fastcgi_pass unix:/var/run/php5-fpm.sock;

    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_buffer_size   1280k;
    fastcgi_buffers   4 2560k;
    fastcgi_busy_buffers_size   2560k;
}

location ~ /\.ht {
    deny all;
}

}

编辑 #1 我已经修改了代码,我把下一个:

    server {
    server_name vcarlos.lan;
    root /home/sierra/tfc_dev/tfc/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }
    # DEV
    # This rule should only be placed on your development environment
    # In production, don't include this and don't deploy app_dev.php or config.php
    location ~ ^/(app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }
    # PROD
    location ~ ^/app\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fasserver {
    server_name vcarlos.lan;
    root /home/sierra/tfc_dev/tfc/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }
    # DEV
    # This rule should only be placed on your development environment
    # In production, don't include this and don't deploy app_dev.php or config.php
    location ~ ^/(app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }
    # PROD
    location ~ ^/app\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/app.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}tcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/app.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

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

现在它可以识别 Symfony2 但它返回此错误:

哎呀!发生错误 服务器返回“404 Not Found”。 东西坏了。请让我们知道发生此错误时您在做什么。我们会尽快修复它。给您带来的不便,我们深表歉意。*

【问题讨论】:

    标签: symfony nginx


    【解决方案1】:

    请替换:

    rewrite ^/(.*)$ /app.php/$1;
    

    与:

    rewrite ^/(.*)$ /app.php?query_string;
    

    UPD:试试这个:

    server {
            listen 0.0.0.0:80;
            server_name vcarlos.lan;
            root /home/tfc_dev/tfc/web;
    
            index app.php;
            try_files $uri $uri/ /app.php?$query_string;
    
            location ~ \.php$ {
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_pass php-fpm;
                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                    fastcgi_index index.php;
                    include fastcgi_params;
            }
    }
    

    【讨论】:

    • 你好,我已经替换了它,现在它返回这个错误:找不到文件
    • 我更新了答案。还要确保 app.php 存在于 /web 目录中。
    • 可能你没有根路由。尝试打开vcarlos.lan/app_dev.php 另见日志tail ./app/logs/prod.log tail ./app/logs/dev.log
    • 如果我放 vcarlos/app_dev.php,它会返回 Symfony2 的错误:NotFoundHttpException: No route found for "GET /"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2016-06-30
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    相关资源
    最近更新 更多