关于在Nginx中正确配置静态页面博客路由的问题

Question about correctly configure static page blog routing in Nginx

在静态博客页面配置Nginx路由时遇到问题

我想我应该是犯了一个低级错误,但我对此无能为力,希望能得到这里的帮助。

首先我有两个项目,都是静态页面。 一个用于主页显示欢迎信息,另一个用于博客相关项目。 它们位于不同的文件目录下:

首页在/home/deng/www

# pwd
/home/deng/www
# ll
total 8
-rwxrwxrwx 1 deng deng 4401 Aug 31 16:41 index.html

博客在/var/www/hexo

# pwd
/var/www/hexo
# ll
total 316
drwxrwxrwx  8 root root   4096 Aug 31 22:53 2018
drwxrwxrwx  3 root root   4096 Aug 31 22:53 2020
drwxrwxrwx  5 root root   4096 Aug 31 22:53 2021
drwxrwxrwx  2 root root   4096 Aug 31 22:53 about
drwxrwxrwx  6 root root   4096 Aug 31 22:53 archives
drwxrwxrwx 16 root root   4096 Aug 31 22:53 categories
drwxrwxrwx  2 root root   4096 Aug 31 22:53 css
drwxrwxrwx 17 root root   4096 Aug 31 22:53 images
-rwxrwxrwx  1 root root 271304 Aug 31 22:53 index.html
drwxrwxrwx  3 root root   4096 Aug 31 22:53 js
drwxrwxrwx 16 root root   4096 Aug 31 22:53 lib
drwxrwxrwx  5 root root   4096 Aug 31 22:53 page
drwxrwxrwx 36 root root   4096 Aug 31 22:53 tags

然后我分别为这两个项目index.html配置Nginx路由

/etc/nginx/nginx.conf:这个文件主要是默认配置

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/default.d/*.conf;
}

/etc/nginx/conf.d/nginx.conf:这个文件主要是我自定义的配置

server {
    listen 443 ssl; 
    server_name dengxiaoxu.com; 
    ssl_certificate 1_dengxiaoxu.com_bundle.crt; 
    ssl_certificate_key 2_dengxiaoxu.com.key; 
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
    ssl_prefer_server_ciphers on;

    location  / {
        root         /home/deng/www/;
        index  index.html index.htm;
    }

    location  /blog/ {
        alias /var/www/hexo/;
        index  index.html index.htm;
    }
}

server {
    listen       80;
    listen       [::]:80;
    server_name  dengxiaoxu.com;
    
    return 301 https://$host$request_uri; 
}

除设置SSL外,其他配置均为默认配置。

现在让我们分别访问主页https://dengxiaoxu.com/和/bolg https://dengxiaoxu.com/blog/

好消息是:路由在目标目录中单独找到了index.html文件。

坏消息是:/blog路由下的index.html路由找不到js和cssfiles.From错误日志,好像路由到了首页目录.

2021/09/01 11:16:36 [error] 23010#23010: *2730 open() "/home/deng/www/lib/fancybox/source/jquery.fancybox.css" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /lib/fancybox/source/jquery.fancybox.css?v=2.1.5 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2731 open() "/home/deng/www/lib/font-awesome/css/font-awesome.min.css" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /lib/font-awesome/css/font-awesome.min.css?v=4.6.2 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2730 open() "/home/deng/www/css/main.css" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /css/main.css?v=5.1.4 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2731 open() "/home/deng/www/images/STL/1.png" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /images/STL/1.png HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2731 open() "/home/deng/www/images/avatar.jpg" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /images/avatar.jpg HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2730 open() "/home/deng/www/lib/jquery/index.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /lib/jquery/index.js?v=2.1.3 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2732 open() "/home/deng/www/lib/fastclick/lib/fastclick.min.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /lib/fastclick/lib/fastclick.min.js?v=1.0.6 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2730 open() "/home/deng/www/lib/jquery_lazyload/jquery.lazyload.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2731 open() "/home/deng/www/lib/velocity/velocity.min.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /lib/velocity/velocity.min.js?v=1.2.1 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2732 open() "/home/deng/www/lib/velocity/velocity.ui.min.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /lib/velocity/velocity.ui.min.js?v=1.2.1 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2733 open() "/home/deng/www/lib/fancybox/source/jquery.fancybox.pack.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2734 open() "/home/deng/www/js/src/utils.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /js/src/utils.js?v=5.1.4 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2735 open() "/home/deng/www/js/src/motion.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /js/src/motion.js?v=5.1.4 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2732 open() "/home/deng/www/js/src/affix.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /js/src/affix.js?v=5.1.4 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2730 open() "/home/deng/www/js/src/bootstrap.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /js/src/bootstrap.js?v=5.1.4 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2731 open() "/home/deng/www/js/src/schemes/pisces.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /js/src/schemes/pisces.js?v=5.1.4 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"
2021/09/01 11:16:36 [error] 23010#23010: *2731 open() "/home/deng/www/js/src/bootstrap.js" failed (2: No such file or directory), client: 14.127.248.155, server: dengxiaoxu.com, request: "GET /js/src/bootstrap.js?v=5.1.4 HTTP/1.1", host: "dengxiaoxu.com", referrer: "https://dengxiaoxu.com/blog/"

我觉得我的路由匹配配置不完善(可能在文件/etc/nginx/conf.d/nginx.conf中),但是我目前没有解决办法,希望大家能给我一些建议

通过网上查找资料,成功解决了这个问题。现在分享给大家。

我主要参考这个网站:https://www.jianshu.com/p/62acf17b1710

修改hexo默认配置文件_config.yml

  # URL
  ## If your site is put in a subdirectory, set url as 
  'http://yoursite.com/child' and root as '/child/'
+ url: http://www.dengxiaoxu.com/blog
+ root: /blog
- # url: http://yoursite.com
- # root: /
  permalink: :year/:month/:day/:title/
  permalink_defaults:

然后执行命令hexo ghexo d

nginx的配置文件不用修改

那么问题就解决了

我的问题原因是:Hexo自动生成的css和js文件默认路径在根目录下,而我的静态博客部署在二级域名下,这将导致无法路由到 css 个文件和静态文件。

感谢阅读。^^