nginx的本地主机正在下载的浏览器的PHP文件,而不是为他们服务?(nginx localhost

2019-10-29 03:05发布

有打算对我从来没有见过NGINX做的,不知道为什么它这样一个非常奇怪的事情。 我已经看到这个问题的SO多次讨论过,但我还没有找到一个解决方案,是有道理的,我的conf文件。

基本上,当我试图访问本地主机通过一个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;
#   }
#}

Answer 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的。



Answer 2:

注释掉所有的FastCGI处理对于PHP的。 Nginx的不知道如何处理PHP自身。

通过取消注释该块开始。



Answer 3:

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

# 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/



文章来源: nginx localhost is downloading php files in browser, instead of serving them?