douyu3145
douyu3145
2016-12-27 20:48
浏览 54
已采纳

NGINX服务器配置

I am trying to configure nginx and have the following configuration in:

/etc/nginx/sites-enabled/default

server {
    server_name firstproj.dev www.firstproj.dev;
    root /var/www/firstProj/web;

    location / {
        try_files $uri /app.php$is_args$args;
    }

    location ~ ^/(app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }

    location ~ ^/app\.php(/|$) {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;
    }

    location ~ \.php$ {
      return 404;
    }

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

where both app.php and app_dev.php are files in /var/www/firstProj/web/ folder created by symfony with no further modifications.

firstProj is a symfony project that I am currently working on and what I fail to understand here is:

  1. Why when i access www.firstproj.dev I get

    Oops! An Error Occurred The server returned a "500 Internal Server Error".

  2. Why when I access www.firstproj.dev/app_dev.php everything is ok and I can also access my routes (eg: www.firstproj.dev/app_dev.php/homepage works perfectly fine and returns the expected response from the action in my controller)

  3. Why when I access www.firstproj.dev/app.php I get 404 error.

I want it to work with www.firstproj.dev/homepage with no app_dev.php there but I cannot make it work that way. Furthermore, if I access

http://localhost

I get:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com.

Thank you for using nginx.

I apologize if this is a rather dumb question but I'm a beginner as far server configuration is concerned.

Thank you!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsvd407787736
    dsvd407787736 2016-12-28 00:04
    已采纳

    So you're finding that your app_dev.php works but app.php does not work?

    I've had this happen in the past and there are a few things worth checking.

    1. Copy the contents of app_dev.php into app.php and see if www.firstproj.dev/app.php or www.firstproj.dev work. If it works then you know it's not a problem with NGINX but instead a problem with your Symfony Setup.

    2. Have you created a cache or logs directory for prod or only for dev? Are they writable by www-data?

    3. Have you checked /var/log/nginx/project_error.log to see if any error is showing up when trying to access app.php?

    4. At the top of the app.php file add in the line error_reporting(E_ALL);. Now when you try to access the production version do you see any error?

    5. Try clearing the cache for production. Depending how you have you server setup (Are you using ACL or CHOWN/CHMOD for your cache/log files?) You can run something like sudo su -u www-data php bin/console cache:clear --env prod

    点赞 评论

相关推荐