0

so, to start off with, yes, I have looked for a solution, but I can't find it. I know the issue is with my rewrite code, but I don't know enough to fix it. I'm using nginx and php-fpm on debian 9.5.

php loads just fine, but .html doesn't work anymore.

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

thanks for listening. I look forward to hearing back.

edit: just for clarification, my intention is for python files not to have the .php show in the url, but for html files to load normally.

2

This section

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

looks like the culprit. The rewrite directive simply catches all URIs and rewrites them to a .php file.

Edit as per comments

Based on a similar question here, I think what you need is something like:

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

location @rules { 
    rewrite ^(.*)$ $1.php last;
}
  • sorry, I think you misunderstood. my intention is for python files not to have the .php show in the url, but for html files to load normally. – Tomo-Nyan Sep 4 '18 at 16:43
  • @Tomo-Nyan I think you can try with the @rules snippet above. Leave location / as is and modify the rewrite in the location @rules. – Mr Shunz Sep 4 '18 at 18:50
  • this is essentially my question. how do I do the rewrite without affecting access to html files? – Tomo-Nyan Sep 4 '18 at 19:18
  • @Tomo-Nyan I've updated my answer – Mr Shunz Sep 4 '18 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.