默认情况下无法使用nginx打开index.php

时间:2012-01-25 11:52:04

标签: nginx

我的服务器定义有什么问题?如果我尝试访问“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;

相同的行为:我无法通过“testing.com”获得带有“www.testing.com”的index.php。 (当然,测试网不仅仅是我的。)

    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 个答案:

答案 0 :(得分:20)

首先你需要检查你的php-fpm设置(也许你在php-fpm配置中使用套接字连接而不是端口)并在你的位置默认添加索引“/”

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

答案 1 :(得分:8)

fastcgi_index index.php;中添加location ~* \.php$

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

答案 2 :(得分:1)

检查您的fpm是否正在运行,它位于127.0.0.1:9000

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

还记录错误并检查

 error_log  /var/log/nginx/error.log  debug;

用于样本配置检查https://github.com/rtCamp/easyengine/blob/master/conf/nginx/singlesite/basic.conf

答案 3 :(得分:1)

这个对我有用:

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;
}

答案 4 :(得分:-1)

您可以拥有多个服务器名称行,它将在所有服务器名称行上设置VHOST。