因此,我一直在尝试以如下方式运行我的网站:
example.com 显示 example.com/index.html
example.com/test 显示 example.com/test.html
等等。
所以我当前的配置文件是这样的:
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/html;
index index.html index.php;
location / {
try_files $uri.html $uri $uri/ @htmlext;
}
location ~ \.html$ {
try_files $uri =404;
}
location @htmlext {
rewrite ^(.*)$ $1.html last;
}
error_page 404 /404.html;
}
所以我昨天一直在谷歌搜索,但没能找到解决办法。提前谢谢大家!
答案1
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/html;
index index.html index.php;
location / {
try_files $uri $uri.html $uri/ /index.html;
}
error_page 404 /404.html;
}
答案2
下面的配置为您提供以下内容:
curl http://example.com/test
--> 输出:test.html 的内容
curl http://example.com/
--> 输出:index.html 的内容
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/html;
index index.html index.php;
location /test {
try_files $uri.html $uri $uri/;
}
location / {
try_files $uri.html $uri $uri/;
}
location ~ \.html$ {
try_files $uri =404;
}
error_page 404 /404.html;
}