跳到主要内容

配置nginx时添加try_files否则刷新url会报404

最后更新于:

配置nginx时添加try_files否则刷新url会报404

目录

[[toc]]

[toc]

环境

2025年3月19日记录,已解决。

配置环境:

Youbg Kbt》大佬开源的《vitepress-theme-teek》一个博客项目(知识库+博客 二合一),它是一个轻量、简易的VitePress主题框架,非常简约唯美,且也在持续迭代更新,感谢大佬开源的这款优秀产品,大佬威武。💖💖💖

1、故障现象

线上博客,正常访问页面是可以的,但是刷新页面后就会出现404错误。

2、问题分析

https://router.vuejs.org/zh/guide/essentials/history-mode.html

3、解决办法

  • 参考官网文档配置nginx

https://vitepress.dev/zh/guide/deploy#nginx

 1server {
 2    gzip on;
 3    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
 4
 5    listen 80;
 6    server_name _;
 7    index index.html;
 8
 9    location / {
10        # content location
11        root /app;
12
13        # exact matches -> reverse clean urls -> folders -> not found
14        try_files $uri $uri.html $uri/ =404;
15
16        # non existent pages
17        error_page 404 /404.html;
18
19        # a folder without index.html raises 403 in this setup
20        error_page 403 /404.html;
21
22        # adjust caching headers
23        # files in the assets folder have hashes filenames
24        location ~* ^/assets/ {
25            expires 1y;
26            add_header Cache-Control "public, immutable";
27        }
28    }
29}

  • 亲自配置

[root@wiki conf.d]# cat onedayxyy.cn.conf

 1server {
 2    listen 80;
 3    server_name onedayxyy.cn;
 4    #配置https重定向
 5    return 301 https://$host$request_uri;
 6}
 7
 8server {
 9    gzip on;
10    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
11
12
13    listen  443 ssl;
14    server_name  onedayxyy.cn;
15
16    root /root/rsync/rsync-vitepress/dist;    
17    location / {
18        index index.html index.htm;
19        try_files $uri $uri.html $uri/ =404;
20
21        # non existent pages
22        error_page 404 /404.html;
23
24        # a folder without index.html raises 403 in this setup
25        error_page 403 /404.html;
26
27        # adjust caching headers
28        # files in the assets folder have hashes filenames
29        location ~* ^/assets/ {
30            expires 1y;
31            add_header Cache-Control "public, immutable";
32        }
33    } 
34
35    location /images {
36        alias /images;
37        index index.html;
38      valid_referers none blocked *.onedayxyy.cn onedayxyy.cn *.gitee.com gitee.com localhost 127.0.0.1;
39      if ( $invalid_referer ) {
40           # 如不满足,指定访问如下资源
41           rewrite ^/ https://onedayxyy.cn/error/1.avif;
42           return 403;
43      }
44    }
45    
46    
47    location /home {
48    alias /root/home3.0/;
49        index index.html index.htm;
50    }
51
52
53
54    location /wiki {
55    alias /root/rsync/rsync-docusaurus/build;
56        index index.html index.htm;
57    }
58
59    location /newyear {
60    alias /root/rsync/rsync-qianduan-demo/qianduan-demo/newyear;
61        index index.html index.htm;
62    }
63
64
65    location /error {
66        autoindex on; # 启用目录索引
67        alias /FdangDaoLianImages;
68        index index.html;   # 默认显示index.html文件,如果没有则列出目录内容
69    }   
70    
71
72    ssl_certificate             /etc/letsencrypt/live/onedayxyy.cn/fullchain.pem;
73    ssl_certificate_key         /etc/letsencrypt/live/onedayxyy.cn/privkey.pem; 
74
75    ssl_session_timeout 5m;
76    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
77    ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
78    ssl_prefer_server_ciphers on;
79    add_header Strict-Transport-Security "max-age=31536000";
80    
81    access_log /var/log/nginx/onedayxyy.cn.https.log;
82}
  • 配置后重启nginx,测试效果
1systemctl restart nginx

测试,发现以上问题是被解决了的,完美。😂

最新文章

文档导航