0

I encountered a problem when configuring Nginx routing on a static blog page.

I think I should have made a low-level mistake, but I can’t do anything about it and hope to get help here.

First of all, I have two projects, both of them are static pages. One is for the homepage to display welcome messages, and the other is for blog-related items. They are located under different file directories:

The homepage in /home/deng/www

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

The Blog in /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

Then I configure Nginx routing for the index.html of these two projects separately.

/etc/nginx/nginx.conf:This file is mainly the default configuration

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:This file is mainly my customized configuration

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; 
}

Other configurations are the default configuration except for setting up SSL.

Now let us visit the homepage https://dengxiaoxu.com/ and /bolg https://dengxiaoxu.com/blog/ respectively.

The good news is: the route finds the index.html file in the target directory separately.

The bad news is: the index.html route under the /blog route can't find the js and css files.From the error log, it seems to be routed to the homepage directory.

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/"

I think my routing matching configuration is not perfect(maybe in the file /etc/nginx/conf.d/nginx.conf), but I currently have no solution, I hope you can give me some suggestions

1 Answer 1

0

By searching for information on the Internet, I successfully solved this problem. Let me share with you now.

I mainly refer to this website:https://www.jianshu.com/p/62acf17b1710

Modified the default configuration file of 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:

Then execute the command hexo g and hexo d

The configuration file of nginx does not need to be modified

Then the problem is solved

The reason for my problem is: the default path of the css and js files automatically generated by Hexo is in the root directory, but my static blog is deployed under the second-level domain name, which will lead to failure to route to css files and static files.

Thanks for reading.^ ^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.