我在使用我网站的 Nginx 默认配置时遇到了问题。我的 PHP 网站在使用 nginx 的 Linux Azure 机器上运行。这是我当前的配置:
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;
}
}
我所尝试实现的目标如下:
- 我希望人们能够访问 mywebsite.com/register 1b. 我希望人们能够访问 mywebsite.com/register.php
- URL 应该始终类似于 mywebsite.com/register
这个配置就是这样的!所以我很高兴。但我欢呼得太早了。
<form action="register.php" method="post"
现在我的表单不再起作用了,当我单击注册按钮时,页面只会刷新,什么也不做。如果我从配置中删除此部分:
# remove .php from url
if ($request_uri ~ \.php($|\?))
{
rewrite ^(.*)\.php$ $1 permanent;
}
表单又能正常工作了!但 .php 在 URL 中又可见了。所以我的问题是,我的配置有什么问题,我该如何实现上面描述的内容?