Nginx

nginx php文件自動下載而不是在http上執行並且在https上找不到404

  • July 24, 2022

問題 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

問題是由這部分引起的:

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

這使得 nginx 會/index.php/在您訪問/網站時以及/index.php/test訪問網站時進行查找/test

.php您的 PHP 位置僅匹配以( \.php$regex)結尾的文件。因此請求不會傳遞給 PHP 解釋器。

如何實際修復它取決於您的應用程序希望接收請求的確切程度。例如,使用 Wordpress 只需將所有請求傳遞給index.php它並處理其餘的 URL 解析:

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

您的 HTTPS 配置的問題是您server_name不包含您的域名。因此 nginx 使用預設的虛擬主機進行請求。

您的 HTTPS 配置也缺少ssl_certificatessl_certificate_key指令。

引用自:https://serverfault.com/questions/1106440