NGINX下载php文件,但是当我使用https时文件会执行

时间:2019-01-26 16:47:13

标签: php nginx ssl-certificate

我使用https://时,我服务器上的

php文件执行良好 但是当我在浏览器中使用http://时,它开始下载.php文件作为代码。 我已经安装了让服务器上的SSL加密。

这是我的/etc/nginx/conf.d/default.conf文件。

server {
    server_name  localhost;


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

 location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  include fastcgi.conf;
 }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/localhost/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/localhost/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot




}
server {
    if ($host = localhost) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = localhost) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


	
    listen       80;
    server_name  localhost;
    return 404; # managed by Certbot




}
server {
    if ($host = localhost) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


	if ($host = localhost) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    listen 80;
    server_name  localhost;
    return 404; # managed by Certbot


}

由于安全原因,我已将域更改为localhost。 有人能告诉我是怎么回事。没有解决方案。 :(

2 个答案:

答案 0 :(得分:0)

HTTP是端口80,HTTPS是端口443。您需要具备

listen 80;

在告诉nginx将请求传递给PHP的服务器块中。以下配置位于您服务器的listen 443块中,这就是HTTPS执行php文件而不是下载它的原因

 location ~ \.php$ {
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  include fastcgi.conf;
 }

答案 1 :(得分:-2)

没有解决方案,所以我强制将所有http://流量重定向到https://