【问题标题】:Symfony 3 and nginxSymfony 3 和 nginx
【发布时间】:2017-04-25 03:59:24
【问题描述】:

我有 SF3 应用程序,它在 apache2 上运行良好,但是当我使用 nginx http://localhost/SF3-REST-USER-JWT/web/app_dev.php 运行时也可以很好地生成起始页。当我尝试获取像 /api 这样的任何其他路由时,nginx 给了我 404。似乎我的配置的 nginx 根本不喜欢漂亮的路由。它寻找 /api 目录

2017/04/24 21:51:42 [错误] 23683#23683: *2 open() "/var/www/html/SF3-REST-USER-JWT/web/app_dev.php/api" 失败(20:不是目录),客户端:127.0.0.1,服务器:_,请求:“GET /SF3-REST-USER-JWT/web/app_dev.php/api HTTP/1.1”,主机:“localhost”

为什么会这样?我该怎么办?

监听 80 default_server; 听 [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

server_name _;


 location ~ ^/(app_dev|config)\.php(/|$) {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/var/run/php7.1-fpm.sock;
      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
    # for more information).
      fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
      fastcgi_param DOCUMENT_ROOT $realpath_root;
}

nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
   worker_connections 768;
   # multi_accept on;
}

 http {

    ##
    # Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

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

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json                            application/javascript text/xml application/xml application/xml+rss      text/javascript;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

【问题讨论】:

  • 你试过这个指南吗? symfony.com/doc/current/setup/…
  • 这正是我所做的。
  • 这只是 sf 的问题,因为我的其他 wp 应用程序在 nginx 下工作
  • 是 nginx 的 404 错误页面还是 Symfony 的?如果是nginx的,请发布它的配置。也许你有什么错别字。
  • 当我在 app_dev.php 中放了一些转储时,似乎是 nginx 的 404 错误页面。它适用于起始页,不适用于 404。

标签: symfony nginx


【解决方案1】:

正如您在Symfony's doc 中看到的,root 应该指向web 目录。

我猜,/var/www/html 是您的整个 localhost 服务器根目录,您可能无法更改它,因为那里还有其他项目,您将无法访问它们。

在不更改此 root 值的情况下,您有两种选择。

第一个是修复location部分,而不是:

位置 ~ ^/(app_dev|config).php(/|$) {

应该是这样的:

location ~ ^/{path_relative_to_root}/(app_dev|config)\.php(/|$) {

根据你试过的urlhttp://localhost/SF3-REST-USER-JWT/web/app_dev.php的信息,这个路径大概是/SF3-REST-USER-JWT/web/,所以应该是:

location ~ ^/SF3-REST-USER-JWT/web/(app_dev|config)\.php(/|$) {

其次是为这个项目创建单独的虚拟主机,所以它可以有完全独立的域(例如 symfonyapp.local 或任何其他)。

为此,您应该在 /etc/nginx/sites-available/ 中创建单独的配置文件,其内容类似于 Symfony 文档中示例中所示的内容。只需要更改根路径和域名。当然,您需要通过在sites_enabled 中创建符号链接来启用此 nginx 站点。

那么您还需要将此域添加到您操作系统中的hosts 文件中。

在这种情况下,您将能够通过简单的http://symfonyapp.local/app_dev.php 访问您的网站。

【讨论】:

  • 谢谢,但位置 ~ ^/SF3-REST-USER-JWT/web/(app_dev|config)\.php(/|$) 它给了我 502 Bad Gateway 即使在第一个现在办案
  • 我不知道你的确切服务器配置,所以我不能给你100%准备好的解决方案。
【解决方案2】:
Symfony doc gives us  fastcgi_pass unix:/var/run/php5-fpm.sock

但在 Ubuntu 16.04 中应该是 fastcgi_pass unix:/run/php/php5-fpm.sock。那是我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    相关资源
    最近更新 更多