mardi 28 novembre 2017

Nginx error with location block selection

So, I was building a site with Gitbook. It came across with the Nginx configuration problem. Basically, I want to form a standard in which when people are visiting: http://ift.tt/2zxsfwv, it will redirect to that specific book. However, I am very new to Nginx and the location block that I wrote is not correct. I will have the directory tree below.

book/
├── [root     4.0K]  _book/
│   ├── [root     4.0K]  gitbook/
│   ├── [root     5.9K]  index.html
│   └── [root      970]  search_index.json
├── [root     4.0K]  book_name
│   ├── [root     4.0K]  _book/
│   │   ├── [root     4.0K]  gitbook/
│   │   ├── [root     5.9K]  index.html
│   │   └── [root      568]  search_index.json
│   ├── [root       16]  README.md
│   └── [root       40]  SUMMARY.md
├── [root       45]  README.md
└── [root       40]  SUMMARY.md

Worth to mention the _book under the book is the main directory for the site and I will have the hyperlink to redirect to other book. The _book under book_name will be the book.

And here is the configuration for Nginx, I thought the $uri contains the book_name in it, so I can just access the _book folder under the $uri and get the index.html file. This will give a 500 error.

server {
    listen 80;
    listen [::]:80;
    server_name book.domain.com;

    location /.well-known/acme-challenge {
        root /var/www/http://ift.tt/2nbMHS1;
    }

    return 301 http://ift.tt/2zxsg3x;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name book.domain.com;

    ssl_certificate /etc/letsencrypt/live/http://ift.tt/2ni1Lh8;
    ssl_certificate_key /etc/letsencrypt/live/http://ift.tt/2zxsh7B;

    root /var/www/domain.com/book;
    index index.html;

    location / {
        try_files $uri $uri/_book;
    }
}

However, I also tried another way to write it by adding another location block, but it is also not working. This will give me a 404 error.

location /book_name {
    root /var/www/domain.com/book_name;
}

I was thinking maybe rewrite is the correct way to do it ? and was really confused when writing the location block. What is the correct way to do it?




Aucun commentaire:

Enregistrer un commentaire