Nginx 配置无法从 url 中删除 .php

问题描述 投票:0回答:1

我的网站的 Nginx 默认配置遇到问题。我的 PHP 网站在 Linux azure 机器上运行,该机器使用 nginx。这是我当前的配置:

server {
    #proxy_cache cache;
        #proxy_cache_valid 200 1s;
    listen 8080;
    listen [::]:8080;
    root /home/site/wwwroot;
    index  index.php;
    server_name  example.com www.example.com
    port_in_redirect off;

    location / {
        index index.php;
        try_files $uri $uri/ @extensionless-php;
    }

    # dont show /index, index.php
    if ( $request_uri ~ "/index" ) {
        rewrite ^(.*)/ $1/ permanent;
    }

    # remove .php from url
    if ($request_uri ~ \.php($|\?))
    {
        rewrite ^(.*)\.php$ $1 permanent;
    }

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

    # remove www. from url
    if ($host ~ '^www\.') {
        return 301 https://exmaple.com$request_uri;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /html/;
    }

    # Disable .git directory
    location ~ /\.git {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Add locations of phpmyadmin here.
    location ~* [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.[Pp][Hh][Pp])(|/.*)$;
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_connect_timeout         300;
        fastcgi_send_timeout           3600;
        fastcgi_read_timeout           3600;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }
}

我试图实现的目标是从正在使用此配置的网址中删除 .php。至少我是这么想的.. 当我访问 mysite.com/register.php 时,它显示了该页面,并且 url 看起来像 mysite.com/register,很好。但我的形式:

<form action="register.php" method="post"

这不起作用。操作 register.php 或仅注册都不起作用。当我删除 .php 重写时,它工作正常。有人知道我的配置出了什么问题吗?预先感谢。

php azure nginx url url-rewriting
1个回答
0
投票

这部分:

if ($request_uri ~ \.php($|\?))
{
    rewrite ^(.*)\.php$ $1 permanent;
}

使用 HTTP 301 状态删除原始请求的

.php
部分。大多数浏览器会使用修改后的 URL 重试请求,但需要将 POST 更改为 GET。

HTTP 307 状态类似,但浏览器应使用另一个 POST 请求重试 POST 请求。

尝试将

if
块更改为:

if ($request_uri ~ ^(.*)\.php(\?.*)?$)
{
    return 307 $1$2;
}
© www.soinside.com 2019 - 2024. All rights reserved.