【问题标题】:Nginx sites-available doesn't workNginx 可用站点不起作用
【发布时间】:2014-08-31 10:08:34
【问题描述】:

我在 sites-available 下有一个非常简单的 server 块。

问题:当我尝试访问 mydomain.com 时,Nginx 会返回“404 Not Found”,但如果我尝试访问特别是某个文件,它工作正常,如 mydomain.com/index.php

server {

    listen          80;
    index           index.php;
    server_name     mydomain.com;
    root            /home/myusername/sites/mydomain.com/htdocs;

    access_log      /home/myusername/sites/mydomain.com/logs/access.log;
    error_log       /home/myusername/sites/mydomain.com/logs/error.log;

    location / {
            try_files $uri =404;
    }

    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

}

请注意:

  • 我的 hosts 文件已配置;
  • 每次编辑后我都会重新启动 Nginx;
  • 访问权限、用户和组正确;
  • error.log 文件为空,access.log 向我返回所有 404;
  • 我尝试通过添加/删除一些行来更改配置,但仍然没有更改;
  • 该站点已在 sites-enabled 中启用,并带有正确的符号链接(我尝试对其进行编辑并打开了正确的文件);
  • 我在同一台服务器上有几个运行良好的站点(因此包括 sites-availablesites-enabled 是可以的,并且 Nginx 工作正常)。

【问题讨论】:

    标签: nginx


    【解决方案1】:

    所以,答案是ServerFaultAlexey Ten 给我的,这是答案的副本


    您的 try_files 指令限制性太强,我猜是放错地方了。

    要么完全删除location /,它没有多大意义,或者,至少添加$uri/ 这样index 指令才能工作。

    try_files $uri $uri/ =404;
    

    但我的猜测是,您需要将这个try_files 移动到location ~ \.php$,这将确保 php 文件存在,然后再将其传递给 PHP-FPM 进行处理。所有其他文件将由 nginx 提供,并正确使用 index 指令。

    server {
        listen          80;
        index           index.php;
        server_name     mydomain.com;
        root            /home/myusername/sites/mydomain.com/htdocs;
    
        access_log      /home/myusername/sites/mydomain.com/logs/access.log;
        error_log       /home/myusername/sites/mydomain.com/logs/error.log;
    
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 2015-10-07
      • 2013-06-29
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多