无法使用nginx默认打开index.php文件

25
我的服务器定义出了什么问题?如果我尝试访问“www.testing.com”,我得到的是一个要下载的二进制文件,而不是index.php。但是如果我尝试访问“testing.com”,我得到的是index.php页面。
我已经尝试将servername设置为:
servername testing.com;
servername testing.com www.testing.com;
servername testing.com www.testing.com *.testing.com;

同样的情况:我无法通过"www.testing.com"获取到index.php,只能通过"testing.com"获得(当然,testing.com不是我的网站,仅为示例)。

    user              nginx;
    worker_processes  4;
    error_log         /var/log/nginx/error.log warn;
    pid               /var/run/nginx.pid;

    events {
         worker_connections  1024;
    }


    http {
         include      /etc/nginx/mime.types;
         default_type  text/plain;

         log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                            '$status $body_bytes_sent "$http_referer" '
                            '"$http_user_agent" "$http_x_forwarded_for"';

         access_log  /var/log/nginx/access.log  main;

         fastcgi_intercept_errors    on;
         sendfile                    on;
         keepalive_timeout           65;
         gzip                        on;
         index                       index.php index.html index.htm;

         server {
              listen 80;
              server_name www.testing.com;
              root /home/vhosts/testing;

              location / {
                  try_files $uri $uri/ /index.php index.php;
              }

        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
                  expires max;
                  add_header Pragma public;
                  add_header Cache-Control "public, must-revalidate, proxy-revalidate";
              }

        location ~* \.php$ {
                 try_files $uri =404;
                 include fastcgi.conf;
                 fastcgi_pass  127.0.0.1:9000;
              }
         }
    }
5个回答

24

首先,您需要检查您的php-fpm设置(可能在您的php-fpm配置中使用了套接字连接而不是端口),并将默认索引添加到您的位置“/”中。

location / {
    index index.php index.html index.htm;
    try_files $uri $uri/ =404;
}

2
没有生效。浏览器仍然尝试下载文件(nginx 重启了吗?) - vsync
我在try_files中缺少了这个**$uri/**。 - arsenik

10
location ~* \.php$中添加fastcgi_index index.php;:
location ~* \.php$ {
    try_files $uri =404;
    include fastcgi.conf;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index  index.php;
}

2
这个对我有用:
location = / {
    index index.php index.html index.htm;
    try_files $uri /index.html;
}

整个使用代理的位置配置如下:
location = / {
    index index.php index.html index.htm;
    try_files $uri /index.html;
    proxy_pass http://localhost:8081;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

2

-3

你可以有多个servername行,这将在所有服务器上设置一个虚拟主机。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接