nginx php 文件自动下载,而不是在 http 上执行,而在 https 上则出现 404 Not Found

nginx php 文件自动下载,而不是在 http 上执行,而在 https 上则出现 404 Not Found

问题 1:我尝试使用 http 在 ip 本地地址下访问我的网站,每次访问时,它都会下载 php 文件,这意味着无法执行。问题 2:我尝试使用 https 在我的域下访问我的网站,但它返回结果“404 Not Found”

以下是我的 nginx 配置:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html/nextcloud;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
    rewrite ^ /index.php$uri;
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

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

location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              fastcgi_pass unix:/run/php/php8.1-fpm.sock;
       }
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}


server {

    listen 443;
    server_name _;

root         /var/www/html/nextcloud;

# Add index.php to the list if you are using PHP
#   index index.php index.html index.htm index.nginx-debian.html;

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

    location / {

      proxy_set_header        Host $host;
      proxy_set_header        X-Real-IP $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_ssl_server_name on;


      #proxy_pass          http://127.0.0.1:8080;
      proxy_read_timeout  90;

      proxy_redirect      http://localhost:8080 https://user.mysite.ml;
    }
location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              fastcgi_pass unix:/run/php/php8.1-fpm.sock;
       }    
}

有人可以帮我配置吗?

  • 我托管的文件放在 /var/www/html/nextcloud。
  • 对于 php 我使用 php8.1-fpm
  • 对于 nginx,我使用版本 nginx/1.18.0
  • 我的操作系统是 debian
  • 我托管的文件是 Nextcloud Database Mariadb

答案1

问题是由这部分引起的:

location / {
    rewrite ^ /index.php$uri;
    try_files $uri $uri/ ?404;
}

这使得 nginx 查找/index.php/您何时访问/网站的,以及/index.php/test您何时访问/test网站的。

.php您的 PHP 位置仅匹配以( regex)结尾的文件\.php$。因此请求不会传递到 PHP 解释器。

如何真正解决这个问题取决于你的应用程序到底想如何接收请求。例如,使用 Wordpress 时,只需将所有请求传递给它,index.php它就会处理其余的 URL 解析:

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

您的 HTTPS 配置存在问题,因为您的server_nameURL 中不包含您的域名。因此,nginx 使用默认虚拟主机来处理请求。

ssl_certificate您的 HTTPS 配置和ssl_certificate_key指令也缺失。

相关内容