无法在 nginx 上使用 .html 名称重命名特定的 .php 文件

Can't rename specific .php file with .html names on nginx

我有一个 Apache 服务器,我在其中 运行 我的 php 应用程序。它有这些 htaccess 规则:

php_flag log_errors Off
RewriteEngine on
RewriteRule thankyou.html /thankyou.php
RewriteRule ^([^/]*)\.html$ /user.php?username= [L]


RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

现在我正在迁移到 nginx 服务器,我需要能够将 thankyou.php 和 user.php 页面显示为 url 中的 .html 页面因为它在 apache 服务器上(对于 user.php 特别是它必须转换为 [name-of-the-user].html name)。

这是 nginx 文件:

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/mydomain.com/before/*;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mydomain.com;
    root /home/forge/mydomain.com;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/mydomain.com/480288/server.crt;
    ssl_certificate_key /etc/nginx/ssl/mydomain.com/480288/server.key;

    ssl_protocols TLSv1.2;
    ssl_ciphers MY-SSL-CIPHER;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/mydomain.com/server/*;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/mydomain.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/mydomain.com/after/*;

我尝试添加

rewrite thankyou.html /thankyou.php;
rewrite ^/([^/]*)\.html$ /user.php?username= break;

在 nginx 配置文件的不同部分(例如在位置 /、外部、位置 /thankyou.php 下)但它不能正常工作。

URI 在内部从 foo.html 重写为 foo.php

rewrite...break 语句终止重写引擎并继续处理 same location 块中的新 URI。

但是,foo.php需要在location \.php$块中处理。

rewrite...last 语句终止重写引擎,然后重新开始搜索 location 以处理新的 URI。

例如:

rewrite ^/([^/]*)\.html$ /user.php?username= last;

详情见this document