nginx 加载 php,但不加载 html

nginx 加载 php,但不加载 html

首先,是的,我已经找到了解决方案,但找不到。我知道问题出在我的重写代码上,但我不知道该如何修复它。我在 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 文件正常加载。

答案1

本节

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;
}

相关内容