nginx localhost是在浏览器中下载php文件,而不是为它们提供服务?

时间:2014-06-10 20:05:12

标签: php nginx localhost

有一件非常奇怪的事情,我从未见过NGINX,并且不知道为什么会这样做。我在SO上已经多次讨论过这个问题,但是我找不到一个对我的conf文件有意义的解决方案。

基本上,当我尝试通过浏览器(任何浏览器)中的localhost访问php文件时,它会下载文件,而不是显示它。我读过人们认为这是一个MIME问题,但我看不出它在conf中的反映。另外,有几个人说/etc/nginx/conf.d中有一个隐藏文件覆盖了设置。据我所知,情况并非如此。快速ls -al显示该文件夹中没有任何内容。请帮忙? :d

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
}

# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
#   proxy_pass http://127.0.0.1:8080;    
#}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
#   root /usr/share/nginx/html;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#   fastcgi_split_path_info ^(.+\.php)(/.+)$;
#   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
#   # With php5-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;
#   # With php5-fpm:
#   fastcgi_pass unix:/var/run/php5-fpm.sock;
#   fastcgi_index index.php;
#   include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#   deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#   listen 8000;
#   listen somename:8080;
#   server_name somename alias another.alias;
#   root html;
#   index index.html index.htm;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}


# HTTPS server
#
#server {
#   listen 443;
#   server_name localhost;
#
#   root html;
#   index index.html index.htm;
#
#   ssl on;
#   ssl_certificate cert.pem;
#   ssl_certificate_key cert.key;
#
#   ssl_session_timeout 5m;
#
#   ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
#   ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
#   ssl_prefer_server_ciphers on;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

3 个答案:

答案 0 :(得分:2)

该文件的这一部分看起来很相关

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#   fastcgi_split_path_info ^(.+\.php)(/.+)$;
#   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
#   # With php5-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;
#   # With php5-fpm:
#   fastcgi_pass unix:/var/run/php5-fpm.sock;
#   fastcgi_index index.php;
#   include fastcgi_params;
#}

尝试取消注释

http://blog.martinfjordvald.com/2010/07/nginx-primer/

http://wiki.nginx.org/Pitfalls

http://eksith.wordpress.com/2008/12/08/nginx-php-on-windows/

答案 1 :(得分:2)

您已经注释掉了所有针对PHP的FastCGI处理。 Nginx并不知道如何自己处理PHP。

首先取消注释该块。

答案 2 :(得分:1)

不要取消注释整个块。由于具有重复的fastcgi_pass指令,它不仅会失败,而且无论如何都不会起作用。它将继续下载您要求的文件,而不是将它们提供给浏览器。

派生自http://blog.martinfjordvald.com/2010/07/nginx-primer/

location ~ \.php$ {
    # fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-cgi alone:
    # fastcgi_pass 127.0.0.1:9000;
    # With php5-fpm:

    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include fastcgi.conf;

    # fastcgi_index index.php;
    # include fastcgi_params;
}

应用此功能后,我能够成功访问phpMyAdmin。