Nginx

nginx 載入 php,但不載入 html

  • September 4, 2018

所以,首先,是的,我一直在尋找解決方案,但我找不到。我知道問題出在我的重寫程式碼上,但我沒有足夠的知識來修復它。我在 debian 9.5 上使用 nginx 和 php-fpm。

php 載入得很好,但 .html 不再工作了。

server {
   # SSL configuration
   #
   listen 443 ssl default_server;
   listen [::]:443 ssl default_server;

   root /var/www/example.com;

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

   server_name example.com;

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

   location /media {
       autoindex on;
       autoindex_exact_size off;
   }

   # pass PHP scripts to FastCGI server
   #
   location ~ \.php$ {
       include snippets/fastcgi-php.conf;
   #
   #   # With php-fpm (or other unix sockets):
       fastcgi_pass unix:/var/run/php/php7.0-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;
   }
}

感謝收聽。我期待著回音。

編輯:只是為了澄清,我的意圖是讓python文件不在url中顯示.php,而是讓html文件正常載入。

本節

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

看起來像罪魁禍首。該rewrite指令簡單地擷取所有 URI 並將它們重寫到.php文件中。

根據評論編輯

基於此處的類似問題,我認為您需要的是:

location / { 
   try_files $uri $uri/ @rules; 
} 

location @rules { 
   rewrite ^(.*)$ $1.php last;
}

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