Symfony 3和Nginx在生产.php路线上投掷404

时间:2016-10-31 18:07:51

标签: symfony nginx

我最近搬到了运行nginx而不是Apache的新服务器设置。从外部使用的遗留代码我需要设置路由/assets/js/wiget_load.js.php。

这曾经用于Apache,但现在不在nginx上。我是新手,所以我无法弄清楚这是怎么回事以及为什么它会抛出一个nginx 404.如果我访问/app_dev.php/assets/js/wiget_load也可以。 .js.php但不是prod。这是我的.conf文件:

server {

    listen 443 ssl;
    ssl_certificate    /etc/ssl/mydomain.chained.crt;
    ssl_certificate_key    /etc/ssl/mydomain.key;

    server_name mydomain.com;
    root /usr/share/nginx/html/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 php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }
    # PROD
    location ~ ^/app\.php(/|$) {
        fastcgi_pass php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        # 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;
    }

    location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
        access_log        off;
        log_not_found     off;
        expires           30d;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
      return 404;
    }

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

1 个答案:

答案 0 :(得分:1)

/assets/js/wiget_load.js.php的请求与以下位置匹配:

location ~ \.php$ {
  return 404;
}
  

如果我访问/app_dev.php/assets/js/wiget_load.js.php而不是prod

,这也可以正常工作

是的,因为对于dev,此URI与location ~ ^/(app_dev|config)\.php(/|$)location ~ \.php$匹配,并且首先使用nginx(如何在配置文件中写入)。

所以,现在如何解决......我的建议是

location ~ \.php$ {
  try_files /never_exists_filename /app.php$is_args$args;
}

所有.php文件都将路由到/app.php,即使存在于磁盘上,也不会转发到客户端。 try_files $uri /app.php$is_args$args;这里不安全,如果.php文件存在于磁盘上 - 它将作为静态文件传递给浏览器。